BufferedWriter not writing everything to its output file

前端 未结 9 1769
悲哀的现实
悲哀的现实 2020-11-22 03:35

I have a Java program that reads some text from a file, line by line, and writes new text to an output file. But not all the text I write to my BufferedWriter a

相关标签:
9条回答
  • 2020-11-22 04:25

    You need to close your OutputStream which will flush the remainder of your data:

    out.close();
    

    The default buffer size for BufferedWriter is 8192 characters, large enough to easily hold hundreds of lines of unwritten data.

    0 讨论(0)
  • 2020-11-22 04:26

    Always close your resources (not just files) when you're done with them.

     finally {
        out.close();//this would resolve the issue
        }
    

    There might be situations when you want to flush the buffer without closing the file. In these situations you can use the flush-method.

    0 讨论(0)
  • 2020-11-22 04:30

    Your code does not appear to be closing the writer after you've finished writing to it. Add an out.close() (preferably in a finally block) and it should work properly.

    0 讨论(0)
提交回复
热议问题