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
you dint close your BufferedWriter.close it inside a finally block
finally {
out.close();//this would resolve the issue
}
This works. I have faced the same issue and this worked for me..Good Luck :)
A resource that must be closed when it is no longer needed.
finally {
out.close();//this would resolve the issue
}
Some things to consider:
BufferedWriter.close()
flushes the buffer to the underlying stream, so if you forget to flush()
and don't close, your file may not have all the text you wrote to it. BufferedWriter.close()
also closes the wrapped Writer. When that's a FileWriter, this will ultimately close a FileOutputStream and tell the OS that you're done writing to the file. close()
, not on the BufferedWriter or the wrapped FileWriter, but on the FileOuputStream. So the OS will be happy, but you have to wait for the GC. BufferedWriter.close()
does clear up the internal character buffer, so that memory will be available for garbage collection, even while the BufferedWriter itself remains in scope. So, Always close your resources (not just files) when you're done with them.
If you really want a peek under the covers, most of the Java API's source is available. BufferedWriter is here.
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()
According to documentation it is no use calling flush()
method. If you intent to use FileWriter
then flush()
would help you out.
Basically in this case, you just need to close, BufferedWriter.close()
only. This will flush the remainder of your data.
create finally block and put the close method inside so that it will put all data without missing.
finally {
out.close();
}
you dint close your BufferedWriter.close it inside a finally block
finally {
out.close();//this would resolve the issue
}
You must close() your BufferedWriter
. You must close()
your BufferedWriter
because it IS-A Writer and thus implements AutoCloseable, which means (emphasis added) it is
A resource that must be closed when it is no longer needed.
Some people say you must first call flush() for your BufferedWriter
before calling close()
. They are wrong. The documentation for BufferedWriter.close()
notes that it "Closes the stream, flushing it first" (emphasis added).
The documented semantics of flushing (flush()) are
Flushes this stream by writing any buffered output to the underlying stream
So, you must close
, and close
will flush any buffered output.
Your output file does not include all the text you wrote to your BufferedWriter
because it stored some of that text in a buffer. The BufferedWriter
never emptied that buffer, passing it through to the file, because you never told it to do so.
Since Java 7, the best way to ensure an AutoCloseable
resource, such as a BufferedWriter
, is closed when it is not longer need is to use automatic resource management (ARM), also known as try-with-resources:
try (BufferedWriter out = new BufferedWriter(new FileWriter(file))) {
// writes to out here
} catch (IOException ex) {
// handle ex
}
You must also close
your BufferedReader
when it is no longer need, so you should have nested try-with-resources blocks:
try (BufferedReader in = new BufferedReader(new FileReader("nyccrash.txt")) {
try (BufferedWriter out = new BufferedWriter(new FileWriter("nyccrash.sql"))) {
// your reading and writing code here
}
} catch (IOException ex) {
// handle ex
}
Do not be tempted (as other answers here suggest) just to call close()
at the end of your method, when your code has "finished" using the writer. That will not work if your writing code throws an exception, and in particular if it throws an IOException
.