1 // 编写TextRw.java的Java应用程序,程序完成的功能是:首先向TextRw.txt中写入自己的学号和姓名,读取TextRw.txt中信息并将其显示在屏幕上。
2 File file = new File("d:/TextRw.txt");
3 try {
4 if (!file.exists()) {
5 file.createNewFile();
6 System.out.println("文件创建成功");
7 }
8 // 写入
9 FileWriter fw = new FileWriter(file);
10 fw.write("学号:10001,姓名:唐枫");
11 fw.close();
12 // 读取
13 FileReader fr = new FileReader(file);
14 char[] ch = new char[1024];
15 int i = fr.read(ch);
16 String str = new String(ch, 0, i);
17 fr.close();
18 System.out.println("文件内容:" + str);
19
20 } catch (Exception e) {
21 e.printStackTrace();
22 }