Java输入输出流练习

编写TextRw.java的Java应用程序,程序完成的功能是:首先向TextRw.txt中写入自己的学号和姓名,读取TextRw.txt中信息并将其显示在屏幕上。

package 作业;

import java.io.*;
public class 编写学号输出 {

	public static void main(String[] args) {
		
		try 
		{
			File file = new File("G:/Java/引用文件/Test3.txt");
			if (!file.exists()) 
			{
				file.createNewFile();
			}
		FileWriter xh=new FileWriter(file);
		xh.write("姓名:于洋   学号:20132904425");
		xh.close();
		System.out.println("文字录入成功");
		FileReader xhd=new FileReader(file);
		int i=0;
		String str="";
		char[]c=new char[1024];
		while((i=xhd.read(c))>0)
		{
			str+=new String(c,0,i);
		}
		xhd.close();
		System.out.println("读取内容是:"+str);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}

}

  

运行结果

posted @ 2016-06-01 08:26  木头鸽子  阅读(251)  评论(0编辑  收藏  举报