BufferedWriter not writing everything to its output file

前端 未结 9 1983
悲哀的现实
悲哀的现实 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:19

    Since you're using BufferedWriter you can also flush the buffer when appropriate:

    out.flush()
    

    This will write the rest of the buffer to the actual file. Close-method also flushes the buffer and closes the file.

    out.close()
    

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

    You can also use BuffredWriter's newline-method instead of adding \n to the end of the line. Newline-method uses system specific line separator so your code works on different platforms.

    out.newLine()
    

提交回复
热议问题