There is one problem that you need to keep in mind: the call to write to flush the buffer could fail. If it was issued from a garbage collection, the program won't get notified of the problem. For that reason, you should always close your stream and you should also make sure that the call to close succeeds. :-)
, Wed Apr 25 19:35:57 2007
Mmm. This test always fails when I comment the testfile.close().
import java.io.*;
public class Test
{
public static void main (String args[])
{
BufferedWriter testfile;
try
{
testfile = new BufferedWriter (new FileWriter ("/tmp/test"));
}
catch (IOException e)
{
e.printStackTrace ();
return;
}
try
{
testfile.write ("Heya");
testfile.newLine ();
testfile.close(); // We do need this line!
} catch (IOException e)
{
e.printStackTrace ();
}
}
}
, Wed Apr 25 19:48:57 2007
Yes. But even if you didn't *need* the explicit close (ie. even if the GC flushed your buffers), you should still include it. :-)
, Wed Apr 25 19:53:31 2007
For that reason, you should always close your stream and you should also make sure that the call to close succeeds. :-)
Yes, I agree. Somehow I don't honor this rule (checking most return values) when I'm not coding in C. (Let's not talk about exceptions now).
, Wed Apr 25 19:56:41 2007
I think you are expecting BufferedWriter to behave like PrintWriter. Use PrintWriter with automatic flushing instead.
, Wed Apr 25 20:28:47 2007
Thanks! :)
, Wed Apr 25 20:35:57 2007
Last update: 2008-11-13 (Rev 14708)

