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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© ∏艾力克斯※ 中级黑马   /  2013-11-9 12:30  /  1188 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. /**
  2.          * 从C盘读一个字符,就往D盘写一个字符
  3.          */
  4.         public static void copyFileTest2() {
  5.                 FileWriter fw = null;
  6.                 FileReader fr = null;

  7.                 try {
  8.                         fw = new FileWriter(path + "RuntimeDemo_copy.txt");// 写入
  9.                         fr = new FileReader("C:/" + "demo4.txt"); // 读
  10.                         int ch = 0;
  11.                         while ((ch = fr.read()) != -1) {
  12.                                 fw.write(ch);
  13.                         }
  14.                         System.out.println("写入成功!");
  15.                 } catch (Exception e) {
  16.                         e.printStackTrace();
  17.                 } finally {
  18.                         if (fr != null) {
  19.                                 try {
  20.                                         fr.close();
  21.                                 } catch (IOException e) {
  22.                                         // TODO Auto-generated catch block
  23.                                         e.printStackTrace();
  24.                                 }
  25.                         }

  26.                         if (fr != null) {
  27.                                 try {
  28.                                         fr.close();
  29.                                 } catch (IOException e) {
  30.                                         // TODO Auto-generated catch block
  31.                                         e.printStackTrace();
  32.                                 }
  33.                         }
  34.                 }
  35.         }
复制代码
请问,程序正常结束,文件也创建,但是新建文件没有字符内容,这是怎么回事?求解答

评分

参与人数 1技术分 +1 收起 理由
黄炳期 + 1

查看全部评分

6 个回复

倒序浏览
  1. public static void copyFileTest2() {
  2.                 FileWriter fw = null;
  3.                 FileReader fr = null;

  4.                 try {
  5.                         fw = new FileWriter(path + "RuntimeDemo_copy.txt");// 写入
  6.                         fr = new FileReader("C:/" + "demo4.txt"); // 读
  7.                         int ch = 0;
  8.                         while ((ch = fr.read()) != -1) {
  9.                                 fw.write(ch);
  10. fw.flush();
  11.                         }
  12.                         System.out.println("写入成功!");
  13.                 } catch (Exception e) {
  14.                         e.printStackTrace();
  15.                 } finally {
  16.                         if (fr != null) {
  17.                                 try {
  18.                                         fr.close();
  19.                                 } catch (IOException e) {
  20.                                         // TODO Auto-generated catch block
  21.                                         e.printStackTrace();
  22.                                 }
  23.                         }

  24.                         if (fr != null) {
  25.                                 try {
  26.                                         fr.close();
  27.                                 } catch (IOException e) {
  28.                                         // TODO Auto-generated catch block
  29.                                         e.printStackTrace();
  30.                                 }
  31.                         }
  32.                 }
  33.         }
复制代码
回复 使用道具 举报 1 0
fw.write(ch);这句代码后面应该加上一句:fw.flush();这样你的目的地文件中才会有数据。
因为fw.write(ch);这句代码只是将关联的文件中的数据写入到流中,并没有写入到目标文件中。
你循环调用write()方法,只会将流中的数据循环覆盖,
调用flush()方法,作用是刷新流对象中缓冲的数据,将数据刷到目的地文件中。
你想一个字符一个字符的写入,那么就把fw.flush();写到循环里,读一个字符,写入到目的地文件中一次,你试试,是否成功。

评分

参与人数 1技术分 +1 收起 理由
黄炳期 + 1

查看全部评分

回复 使用道具 举报
fw 没冲流,也没关,数据在缓冲区呆着呢
回复 使用道具 举报
要刷新一次
/**
         * 从C盘读一个字符,就往D盘写一个字符
         */
        public static void copyFileTest2() {
                FileWriter fw = null;
                FileReader fr = null;

                try {
                        fw = new FileWriter(path + "RuntimeDemo_copy.txt");// 写入
                        fr = new FileReader("C:/" + "demo4.txt"); // 读
                        int ch = 0;
                        while ((ch = fr.read()) != -1) {
                                fw.write(ch);
                                fw.flush;
                        }
                        System.out.println("写入成功!");
                } catch (Exception e) {
                        e.printStackTrace();
                } finally {
                        if (fr != null) {
                                try {
                                        fr.close();
                                } catch (IOException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                }
                        }

                        if (fr != null) {
                                try {
                                        fr.close();
                                } catch (IOException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                }
                        }
                }
        }
回复 使用道具 举报
哥们,能把你的流里的数据刷出来到硬盘后再打印写入成功这句话不
回复 使用道具 举报
hubby 中级黑马 2013-11-9 18:28:51
7#
没有刷新,没有关流。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马