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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 莫嘉伟 中级黑马   /  2013-3-2 14:06  /  1868 人查看  /  7 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

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()除了关闭流以外还有其他作用吗?不然怎么会这样

7 个回复

倒序浏览
它的作用是
关闭资源,关闭前会刷新一次,将数据刷新到目的地。

他跟flush的区别是flush刷完了还能用。但close刷新后就直接关闭了
回复 使用道具 举报
close()语句在执行关闭流的动作之前会执行一起flush(),,然后再关闭流资源
而flush()则仅仅是刷新一下流;前者多了一个关闭动作
回复 使用道具 举报
所格原来close()还有刷新作用,谢谢楼上的哥们
回复 使用道具 举报
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();//将缓冲区里面的内容刷到文件里,释放关联的文件资源
}
回复 使用道具 举报
java API 里的解释为
public void close()
           throws IOException
从类 Writer 复制的描述
关闭此流,但要先刷新它。在关闭该流之后,再调用 write() 或 flush() 将导致抛出 IOException。关闭以前关闭的流无效。
回复 使用道具 举报
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("文件复制成功!");
        }
}
回复 使用道具 举报
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("文件复制成功!");
        }
}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马