Results 1 to 4 of 4
Thread: java.io help
- 09-21-2011, 09:04 PM #1
java.io help
I am looking for classes in java.io that can read a line(and store that line in a string) from a binary file(example a avi video, png image or exectuable file), and then write this line to a file, without loosing the binary format.
So if I did like this:
If the input file was, example somemovie.avi, the output file would be an exact copy, and playable in media players like the orginal was.Java Code://Objects String s = in.readLine (); while (s != null) { out.write (s); out.newLine (); s = in.readLine (); } in.close (); out.close ();
I tried myself some stuff:
these did not work. The binary format was lost, and my test image was corrupted.Java Code:File fil = new File ("C:\\img.png"); FileInputStream fis = new FileInputStream (fil); InputStreamReader fin = new InputStreamReader (fis); BufferedReader in = new BufferedReader (fin); File fol = new File ("C:\\imgout.png"); FileOutputStream fo = new FileOutputStream (fol); DataOutputStream out = new DataOutputStream (fo);
What classes should I use?
- 09-21-2011, 09:20 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: java.io help
Don't use Readers for that; they are character based streams and they convert bytes to characters according to an decoding scheme; that's why your output isn't the same as your input. Use InputStreams instead; they simply read bytes and don't play tricks on them. For binary files 'lines' don't have any meaning; you have to read 'blocks' of bytes and process them (such as writing them with OutputStream objects).
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 09-21-2011, 09:29 PM #3
Re: java.io help
InputStream and OutputStream are abstract classes, so which ones if its sub classes should I use?
FileInputStream may work for reading, and FileOutputStream for output?
- 09-21-2011, 09:43 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: java.io help
Learn how to read the API documentation; it mentions in one of its headers near the top 'Direct Known Subclasses'; that section lists the subclasses of the InputStream class (for your particular example). Those are links; you can click on them and see their API page. And yes, a FileInputStream and FileOutputStream are what you need. You can even wrap them in buffered streams if you want.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks