输入输出I/O流(字符的写入与写出)

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

 

posted @ 2016-05-31 15:58  明天会更好!!!!  阅读(201)  评论(0)    收藏  举报