黑马程序员技术交流社区

标题: 问个关于输入输出流的问题 [打印本页]

作者: 莫嘉伟    时间: 2013-3-2 14:06
标题: 问个关于输入输出流的问题
public class Test7 {
public static void main(String[] args) throws Exception{
  FileReader fr=new FileReader("a.txt");
  FileWriter fw=new FileWriter("b.txt");
  
  

  char[] buf=new char[64];
  while((fr.read(buf))>0){
   for(int i=0;i<buf.length;i++){
                       fw.write(buf[i]);
   
   
   }
  }
  fr.close();
fw.close();
}
一段将文件内容复制到另一个文件的小代码,我发现如果没有fw.close();这句的话新文件会创建,但里面什么内容都没有,如果加下的话就正常了,但是不加也不会报错,请问这fw.close()除了关闭流以外还有其他作用吗?不然怎么会这样

作者: Gaara    时间: 2013-3-2 14:15
它的作用是
关闭资源,关闭前会刷新一次,将数据刷新到目的地。

他跟flush的区别是flush刷完了还能用。但close刷新后就直接关闭了
作者: 张豪杰    时间: 2013-3-2 14:23
close()语句在执行关闭流的动作之前会执行一起flush(),,然后再关闭流资源
而flush()则仅仅是刷新一下流;前者多了一个关闭动作
作者: 莫嘉伟    时间: 2013-3-2 14:41
所格原来close()还有刷新作用,谢谢楼上的哥们
作者: 刘圣繁    时间: 2013-3-2 18:49
public class Test7 {
public static void main(String[] args) throws Exception{
  FileReader fr=new FileReader("a.txt");
  FileWriter fw=new FileWriter("b.txt");//执行到这里 b.txt文件如果不存在的话 系统就会自动创建
  
  

  char[] buf=new char[64];
  while((fr.read(buf))>0){
   for(int i=0;i<buf.length;i++){
                       fw.write(buf[i]);
   
   
   }
  }
  fr.close();
fw.close();//将缓冲区里面的内容刷到文件里,释放关联的文件资源
}

作者: Benwolf0818    时间: 2013-3-2 19:10
java API 里的解释为
public void close()
           throws IOException
从类 Writer 复制的描述
关闭此流,但要先刷新它。在关闭该流之后,再调用 write() 或 flush() 将导致抛出 IOException。关闭以前关闭的流无效。
作者: 明锦添    时间: 2013-3-4 22:29
public class Test {

        public static void main(String[] args) throws IOException {
                String sourcePath = "D:/exam.doc";
                String destPath = "E:/exam.doc";
                int length = -1;
                byte[] arrs = new byte[1024];
                File sourFile = new File(sourcePath);
                FileInputStream inputStream = new FileInputStream(sourFile);
                BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);

                File destFile = new File(destPath);
                FileOutputStream outputStream = new FileOutputStream(destFile);
                BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);

                while ((length = bufferedInputStream.read(arrs)) != -1) {
                        bufferedOutputStream.write(arrs, 0, length);
                }
                bufferedOutputStream.close();
                outputStream.close();
                bufferedInputStream.close();
                inputStream.close();
                System.out.println("文件复制成功!");
        }
}
作者: 明锦添    时间: 2013-3-4 22:30
public class Test {

        public static void main(String[] args) throws IOException {
                String sourcePath = "D:/exam.doc";
                String destPath = "E:/exam.doc";
                int length = -1;
                byte[] arrs = new byte[1024];
                File sourFile = new File(sourcePath);
                FileInputStream inputStream = new FileInputStream(sourFile);
                BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);

                File destFile = new File(destPath);
                FileOutputStream outputStream = new FileOutputStream(destFile);
                BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);

                while ((length = bufferedInputStream.read(arrs)) != -1) {
                        bufferedOutputStream.write(arrs, 0, length);
                }
                bufferedOutputStream.close();
                outputStream.close();
                bufferedInputStream.close();
                inputStream.close();
                System.out.println("文件复制成功!");
        }
}




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2