| Author |
I/O: flush() and close()
|
Matthew Alesi
Ranch Hand
Joined: Sep 13, 2006
Posts: 38
|
|
Learning file I/O from K&B, and was a little confused on one point: the book goes out of its way to mention that a FileWriter must be flushed and closed when you are done using it, but I read somewhere else that the close() method automatically calls flush(). If this is true, why the unnecessary emphasis on calling both flush() and close() in the examples in the book? Just want to make sure I'm not missing something important. Thanks!!
|
-Matt
Current CS undergrad
SCJP 5.0
|
 |
Deepak Bala
Bartender
Joined: Feb 24, 2006
Posts: 6603
|
|
Its likely that anything that is a closeable implements a Closeable interface. Have a look at this interface... http://java.sun.com/j2se/1.5.0/docs/api/java/io/Closeable.html It is not mandatory for the close() function to call flush(). So its better to call flush() before you call close() to clear out the buffer. Also, have a look at the Flushable interface. Anything with a flush() is likely to implement Flushable http://java.sun.com/j2se/1.5.0/docs/api/java/io/Flushable.html Both these interfaces have only one method and do not depend on each other.
|
SCJP 6 articles - SCJP 5/6 mock exams - SCJP Mocks - SCJP 5 Mock exam (Word document ) - SCJP 5 Mock exam in Java.Inquisition format
|
 |
 |
|
|
subject: I/O: flush() and close()
|
|
|