Bufferedwriter工作,但文件为空? [英] Bufferedwriter works, but file empty?

查看:492
本文介绍了Bufferedwriter工作,但文件为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

CSVmaker(LinkedList data) {
    String [] myLines = makeStrings(data);
  //  for (int k = 0; k<myLines.length; k++)
  //  System.out.println(myLines[]);




    this.file = new File("rawdata.csv");
        try {
            BufferedWriter buff = new BufferedWriter(new FileWriter(file));
            for (int i = 0; i<myLines.length; i++){
                buff.write(myLines[i]);
                buff.newLine();
                System.out.println("done");
            }
        } catch (IOException ex) {
          System.out.println("except");
        }



}

不,我检查了myLines的内容,这些都是正确的。

No, I checked for the contents of myLines, these are correct.

另外,我得到的印刷品就像我应该的那样经常打印完成。
创建了csv。

Also, I get the print which prints "done" just as often as I should. The csv is created.

但是,如果我手动打开它,它就是空的。

However, if I open it manually, it is empty.

这可能是什么原因?

推荐答案

您永远不会刷新缓冲区,也不会关闭BufferedWriter。

You never flush the buffer, or close the BufferedWriter.

在for循环之后,进行以下调用:

After the for loop, make the following calls:

buff.flush();
buff.close();

即使使用其他资源,完成后关闭它们也是一个好主意。

Even with other resources, closing them when done is a good idea.

这篇关于Bufferedwriter工作,但文件为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆