曦~妍  

 

 

 1 package shurushuchuliu;
 2 import java.io.*;
 3 public class Test4 {
 4 
 5     public static void main(String[] args) {
 6         // 字符输出流
 7         try{
 8         //准备File对象
 9         File file = new File("d:/test.txt");
10         
11 
12         // 如果不存在
13         if (!file.exists()) {
14             //创建文件
15             file.createNewFile();
16             System.out.println("创建文件成功");
17         }
18         
19         //构造输出流
20         //覆盖写入
21         FileWriter fw=new FileWriter(file);
22         
23         fw.write("写入字符输出流");
24         fw.write("写入字符输出流");
25         fw.write("写入字符输出流");
26     
27         fw.append("追加的文本\n");
28         fw.close();
29         //字符输入流
30         FileReader fr=new FileReader(file);
31         char[] c=new char[1024];
32         String str="";
33         int i=0;
34         while((i=fr.read(c))>0)
35         {
36             str+=new String(c,0,i);
37         }
38         fr.close();
39         System.out.println("str="+str);
40         }
41         catch(Exception e)
42         {
43             e.printStackTrace();
44             }
45         }
46 
47     }

 

posted on 2016-06-01 09:39  曦~妍  阅读(146)  评论(0编辑  收藏  举报