A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 清风有意 中级黑马   /  2014-4-16 11:17  /  1088 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

IO流中,我想问一下,到底字节流字符流哪些需要刷新,带缓冲区的字节流字符流哪些需要刷新。各自的close()哪些可以具有刷新功能,哪些不可以。

下面的例子是不带缓冲区的字符流,当不close()时,在write()后flush()才有,当close()时,不刷新也有。
请举例证明其他IO流的例子。

  1. package exercise;



  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileReader;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.io.Reader;
  8. import java.io.Writer;

  9. public class IOStream_flush {
  10.         public static void main(String[] args)
  11.         {       
  12.                 Reader bis=null;
  13.                 Writer bos=null;
  14.                 try {
  15.                 File file1=new File("src\\exercise\\Test2.java");
  16.                         File file2=new File("src\\exercise\\NoneBufferedTest2_copy_zifu.txt");
  17.                         bis=new FileReader(file1.getAbsolutePath());
  18.                         bos=new FileWriter(file2.getPath());
  19.                         char[] buf=new char[1024];
  20.                         int len=0;
  21.                         while((len=bis.read(buf))!=-1)
  22.                         {
  23.                                 bos.write(buf,0,len);
  24.                         }
  25.                        
  26.         } catch (FileNotFoundException e) {
  27.                 // TODO Auto-generated catch block
  28.                 e.printStackTrace();
  29.         } catch (IOException e) {
  30.                 // TODO Auto-generated catch block
  31.                 e.printStackTrace();
  32.         }
  33.                 finally{
  34.                         try {
  35.                                 bis.close();
  36.                                 bos.close();
  37.                         } catch (IOException e) {
  38.                                 // TODO Auto-generated catch block
  39.                                 e.printStackTrace();
  40.                         }
  41.                 }
  42.         }
  43. }
  44.        

  45.                

  46.        
复制代码

评分

参与人数 1技术分 +1 收起 理由
枫儿 + 1 神马都是浮云

查看全部评分

1 个回复

倒序浏览
printStream和printWriter可以在创建时指定是否自动刷新,其他的可以查阅API
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马