輸入輸出1,7915輸入輸出差1v
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 }