java程序设计基础_陈国君版第五版_第十章习题

java程序设计基础_陈国君版第五版_第十章习题

/**
 * 利用基本输入输出流实现从键盘上读入一个字符,然后显示在屏幕上
 * @author Richar-gao
 *由题可知用 System.in System.out 来进行操作。
 */
import java.io.*;
public class Main10_4 {
	public static void main(String[] args)throws IOException
	{
		System.out.print("请输入字符: ");
		char ch = (char)System.in.read();
		System.out.print(ch);
	}
}

/**
 * 利用文件输出流创建一个文件file1.txt,写入字符“文件已被成功创建!”,然后用记事本打开该文件,看一下是否正确写入。
 * @author Richard-gao
 *
 */
import java.io.*;
public class Main10_7 {
	public static void main(String[] args){
		FileWriter fos;
		try
		{
			fos = new FileWriter("d:\\file1.txt");
			System.out.println("请输入字符");
			String str = new BufferedReader(new InputStreamReader(System.in)).readLine();
			fos.write(str);
			fos.close();
		}
		catch(IOException e)
		{
			System.out.println("存在输入错误");
		}
	}
}

/**
 * 用文件输入流打开第七题中创建的文件file1.txt,读出其内容并显示在屏幕上
 * @author Richar-gao
 *
 */
import java.io.*;
public class Main10_8 {
	public static void main(String[] args)throws IOException
	{
		char[] c = new char[500];
		FileReader  fr = new FileReader("d:\\file1.txt");
		int num =  fr.read(c);
		String str = new String(c,0,num);
		System.out.println("读取的字符个数为:"+num+",其内容如下:");
		System.out.println(str);
		fr.close();
	}
}

/**
 * 利用文件输入输出流打开第七题创建的文件FILE1.TXT,然后在文件的末尾追加一行字符串“又添加了一行文字!”。
 * @author Richar-gao
 *
 */
import java.io.*;
public class Main10_9 {	
	public static void main(String[] args)throws IOException{
		FileWriter fr = new FileWriter("d:\\file1.txt",true);
		String str = "\r\n又添加了一行文字!";
		fr.write(str);
		fr.close();
		
	}
}

/**
 * 产生15个20-9999之间的随机整数,然后利用BufferedWriter类将其写入文件file2.txt中;之后再读取该文件中的数据并将它们以升序排序。
 * @author Richard-gao
 *
 */
import java.io.*;
public class Main10_10 {
	public static void main(String[] args)throws IOException
	{
		int[] b = new int[15];
		for(int i = 0 ; i<b.length;i++)
			b[i] = (int)(Math.random()*9980)+20;                //使得每个数都是20-9999
		BufferedWriter bw = new BufferedWriter(new FileWriter("d:\\file2.txt"));
		String str = new String();
		for(int i =0 ; i<b.length;i++)
		{
			str = ""+b[i];
			bw.write(str);
			bw.newLine();
		}
		bw.flush();
		bw.close();                                 //将数值输入文件中后关闭文件。
		BufferedReader br = new BufferedReader(new FileReader("d:\\file2.txt"));
		int[] c = new int[b.length];
		int j=0;
		while((str = br.readLine())!=null)
		{
			c[j] = Integer.parseInt(str);
			j++;
		}
		for(int m = 0 ; m<c.length;m++)
			for(int n = m; n<c.length;n++)
			{
				if(c[m]>c[n])						//如果存在比他大的数,则两者互换
				{
					c[m] = c[m]+c[n];
					c[n] = c[m]-c[n];
					c[m] = c[m]-c[n];
				}
			}
		System.out.println("排序后的结果为:");
		for(int i : c)
		{
			System.out.print(i+"\t");
		}
	}
}


  • 7
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值