Results 1 to 4 of 4

Thread: java.io help

  1. #1
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    200
    Rep Power
    5

    Default 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:

    Java Code:
    		//Objects
    
    		String s = in.readLine ();
    		while (s != null)
    		{
    			out.write (s);
    			out.newLine ();
    			s = in.readLine ();
    		}
    		in.close ();
    		out.close ();
    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.
    I tried myself some stuff:
    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);
    these did not work. The binary format was lost, and my test image was corrupted.

    What classes should I use?

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,044
    Blog Entries
    7
    Rep Power
    23

    Default 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,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  3. #3
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    200
    Rep Power
    5

    Default 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?

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,044
    Blog Entries
    7
    Rep Power
    23

    Default Re: java.io help

    Quote Originally Posted by Pojahn_M View Post
    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?
    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,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •