• Post Reply Bookmark Topic Watch Topic
  • New Topic

BufferedOutputStream does not take my input from input file.  RSS feed

 
sarojni agrwal
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



In this example outputt.txt blank, it did not take any input from inputt.txt why? while if I remove BufferedInput and OutputStream , then directly data goes on perfectly...
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use classes in the java.io package whose names end “Stream” for text files. Use classes whose names end “Reader” or “Writer”. The classes you are using don't work at all well with text files.
Don't use the read() method for text files, except possibly to demonstrate how badly it works. The low‑level classes e.g. file input stream work with one character/int at a time and the buffered classes collect lots of data and they work a line at a time. Buffered classes will therefore give faster performance.

Your code is not indented consistently; please remove some of the blank lines too. The names of your class and variable are also poorly chosen; nobody can work out what tr means.

Actually I would use a Scanner and a Formatter, which are easier to use because they “consume” Exceptions. You can read about the IO classes in the Java™ Tutorials.
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're just copying from one file to another then a simple FileInputStream and FileOutputStream are all you need.
If you don't need the data in it there's no point with the overhead of Scanners or Buffering.

That is, of course, if you are forced to use those classes and not stuff in nio.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't some of the classes have a copy() method? There is bound to be something in this subsection of the Tutorials section which I quoted earlier.
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sarojni agrwal wrote:In this example outputt.txt blank, it did not take any input from inputt.txt why? while if I remove BufferedInput and OutputStream , then directly data goes on perfectly...


Since you didn't close your output stream, the data in the last buffer didn't get written out to the underlying file. I assume the input was small enough so that it fit into a single buffer and therefore nothing got written out. you should always close resources like that, either in a finally block or in a try-with-resources statement.

At least I'm guessing that's what happened. Your statement that no data was read from the input file might have been a conclusion based on the fact that you saw nothing in the output file, but it isn't good practice to draw such a conclusion and then start looking for the problem. It's better to start with the actual observed problem. Or did you actually have some evidence that the input data wasn't being read?


 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Don't some of the classes have a copy() method? There is bound to be something in this subsection of the Tutorials section which I quoted earlier.


Yep, nio.Files has copy functionality, which means you can avoid all this sort of thing.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So just to make this completely obvious:

You are missing one line of code.

The line that closes the outputstream. The close method in java.io.FilterOutputStream
Go read the API :-)

 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote:
You are missing one line of code.

The line that closes the outputstream. The close method in java.io.FilterOutputStream
Go read the API :-)



It's the BufferedOutputStream you want to close.
Closing the FOS underneath the BOS will result in anything left in the buffer being lost.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!