wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes what is going to happend if i don't close streams? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "what is going to happend if i don Watch "what is going to happend if i don New topic
Author

what is going to happend if i don't close streams?

Mihai Lita
Ranch Hand

Joined: Jan 11, 2011
Posts: 36

Hey,
I have a question, suppose i have this code :


what will happend if i comment the oos.close() line?

Thank you.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2152
    
    7
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.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41068
    
  43
... 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.


Ping & DNS - my free Android networking tools app
Mihai Lita
Ranch Hand

Joined: Jan 11, 2011
Posts: 36

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?


The book is Head First Java,2nd ed.
Eric Mission
Greenhorn

Joined: Apr 22, 2009
Posts: 22
The Java I/O API has top-level objects- Reader and Writer which both have some useful methods to inherited classes like BufferedReader or BufferedWriter.

From the Java I/O Streams-

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.


to infinity and beyond
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2152
    
    7
> is there any other way we can force a buffer to write before it`s full?

Check out the FileOutputStream API to see if any method seems appropriate.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what is going to happend if i don't close streams?
 
Similar Threads
serialization: object casting doubt
java.io.EOFException using readObject
Web App HashMap Serilization
What is this ��
what is th use of ByteBuffers ?