• 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:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

BufferedOutputStream does not take my input from input file.

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Marshal
Posts: 76861
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76861
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 27451
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Rancher
Posts: 4801
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
We can walk to school together. And we can both read this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic