wood burning stoves*
The moose likes Beginning Java and the fly likes Procuing Output to a text file is showing peculier behaviour 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 "Procuing Output to a text file is showing peculier behaviour" Watch "Procuing Output to a text file is showing peculier behaviour" New topic
Author

Procuing Output to a text file is showing peculier behaviour

naveen shrimal
Greenhorn

Joined: Dec 18, 2010
Posts: 14
HI to al,
I am facing problem while wrapping the FileInputStream into BufferedInputStream. I am not getting the desired putput on b.txt though the console shows the whole text of a.txt.
When I look arounf I finad two ways out:-
1. either i add a line that will forcefully flush the contents of BufferedInputStream
2. I should not close the FileUotputStream in finally clause.

I did not understand the second way....Why so happening? Because Finally executes in last.

one more question is that should I close both FileInputStream oblect and its Wrapper BufferedInputStream object or just the BufferedInputStream object's closing is enough.

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

If you look at the API documentation of class BufferedOutputStream, you'll see that it inherits its close() method from FilteredOutputStream. And the documentation of the close() method for FilteredOutputStream says:

Closes this output stream and releases any system resources associated with the stream.

The close method of FilterOutputStream calls its flush method, and then calls the close method of its underlying output stream.

So you do not need to flush the stream explicitly or call close on on the underlying FileOutputStream.

You get a problem because you close the FileOutputStream in line 29, before your close the BufferedOutputStream in line 30. If you close the FileOutputStream first, then BufferedOutputStream can't flush the data anymore that it still might have in its buffer. So, the solution is to not close the FileOutputStream yourself; just let the BufferedOutputStream do that.

Make the finally-block look like this:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
naveen shrimal
Greenhorn

Joined: Dec 18, 2010
Posts: 14
Jesper de Jong wrote:If you look at the API documentation of class BufferedOutputStream, you'll see that it inherits its close() method from FilteredOutputStream. And the documentation of the close() method for FilteredOutputStream says:

Closes this output stream and releases any system resources associated with the stream.

The close method of FilterOutputStream calls its flush method, and then calls the close method of its underlying output stream.

So you do not need to flush the stream explicitly or call close on on the underlying FileOutputStream.

You get a problem because you close the FileOutputStream in line 29, before your close the BufferedOutputStream in line 30. If you close the FileOutputStream first, then BufferedOutputStream can't flush the data anymore that it still might have in its buffer. So, the solution is to not close the FileOutputStream yourself; just let the BufferedOutputStream do that.

Make the finally-block look like this:




Thank you very much....this foroum is really good and encouragimg me to read a lot because for any trouble I feel like there is some one who will help me like you people.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37913
    
  22
You should avoid the read() method of a Buffered reader; it has much better methods, eg readLine. Also, you should not use input streams and output streams for text files. Use readers and writers. The Java Tutorials has an example (under “buffered streams”) showing you how to create them. You can read like thisIf you keep going through the Java™ tutorials section, you come across Formatter and Scanner, which provide easier ways to read and write, but for text files only.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37913
    
  22
Maybe you should read this Java Tutorials section, too.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Procuing Output to a text file is showing peculier behaviour
 
Similar Threads
IO Streams and Characters.
Please explain me.
File.setReadOnly()
Basic: How IO reads or write ByteStream and CharacterStream
Read a file line by line