Data only gets written to the file when the buffer is full not every time to you invoke a write(...) method. One of the functions of the close() method is to flush the buffer to make sure all the data is written. I'm sure there are other issues as well.
... which means it depends on the Stream (or Writer) implementation: there could well be stream implementations that perform no buffering internally, while others may do so (obviously, BufferedOutputStream does). In both cases I would not necessarily expect to find that mentioned in the javadocs.
Thanks, in the book i`m reading it was mentioned that buffer only write when the buffer is full that was all it was mentioned. I didn`t knew close() flushes the buffer, is there any other way we can force a buffer to write before it`s full?
Some other notable methods in the top-level classes include skip(int), mark(int), reset(), available(), ready() and flush(), these are described below...
...The flush() method simply writes out any buffered characters (or bytes) to the destination (for example, file, or socket).
Closing any type of I/O stream (file or socket) is a good habit to follow for several reasons- freeing up system resources, freeing up the heap for other objects, preventing file locks for other applications or for multi-threaded applications and so many more reasons.