• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Procuing Output to a text file is showing peculier behaviour

 
Greenhorn
Posts: 14
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
naveen shrimal
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Marshal
Posts: 80634
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 80634
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe you should read this Java Tutorials section, too.
reply
    Bookmark Topic Watch Topic
  • New Topic