1 package bao5;
2 import java.io.*;
3 public class TextRw
4 {
5
6 public static void main(String[] args)
7 {
8 File file=new File("d:/TextRw.txt");
9 try
10 {
11 if(!file.exists())
12 {
13 file.createNewFile();
14 System.out.println("文件创建成功");
15 }
16
17 FileWriter fw=new FileWriter(file);
18 fw.write("姓名:小明\n");
19 fw.write("学号:123456");
20 fw.close();
21 FileReader fr=new FileReader(file);
22 char[] c=new char[1024];
23 String str="";
24 int i=0;
25
26 while((i=fr.read(c))>0)
27 {
28 str=str+(new String(c,0,i));
29 }
30
31 System.out.println(str);
32
33 fr.close();
34 }
35 catch (IOException e)
36 {
37 e.printStackTrace();
38 }
39
40 }
41
42 }
43 //编写TextRw.java的Java应用程序,程序完成的功能是:首先向TextRw.txt中写入自己的学号和姓名,读取TextRw.txt中信息并将其显示在屏幕上。