2

I want to read x bytes at a time from a file, (say "testFile.raw"), and output those bytes to another file.

For example:

read 20 bytes from testFile.raw -> dump into outputfile.jpg
...repeat until done...

Is this possible?

J. V. A.
  • 529
  • 3
  • 11
SteelFox
  • 93
  • 1
  • 12

2 Answers2

4

Here is an example using a byte array of size 20 :

    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.InputStream;
    import java.io.OutputStream;

    public class FileInputOutputExample {

        public static void main(String[] args) throws Exception {

            try{
                    byte[] b = new byte[20];
                    InputStream is = new FileInputStream("in.txt");
                    OutputStream os = new FileOutputStream("out.txt");

                    int readBytes = 0;

                    while ((readBytes  = is.read(b)) != -1) {
                      os.write(b, 0, readBytes);
                    }
                    is.close();
                    os.close();

            }catch(IOException ioe){
                System.out.println("Error "+ioe.getMessage());
            }
         }
     }
Arnaud
  • 17,229
  • 3
  • 31
  • 44
  • thank you so much! another quick follow up question. how do i know how many times i've read 20 bytes? you're a real help! – SteelFox Feb 07 '16 at 09:50
  • The _read_ method returns the number of read bytes, or -1 when there is nothing to read anymore (hence the -1 check in the loop). I just edited the post to show you an example. – Arnaud Feb 07 '16 at 16:52
  • A simple, wrong example. Should be `os.write(b, 0, readBytes);` – user207421 Feb 07 '16 at 17:54
-1

Follow like this give a tumbsup if you like my work

public class ImageTest {

public static void main(String[] args) {

    try {

        byte[] imageInByte;
        BufferedImage originalImage = ImageIO.read(new File(
                "c:/imagename.jpg"));

        // convert BufferedImage to byte array
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(originalImage, "jpg", baos);
        baos.flush();
        imageInByte = baos.toByteArray();
        baos.close();

        // convert byte array back to BufferedImage
        InputStream in = new ByteArrayInputStream(imageInByte);
        BufferedImage bImageFromConvert = ImageIO.read(in);

        ImageIO.write(bImageFromConvert, "jpg", new File(
                "c:/imagename.jpg"));

    } catch (IOException e) {
        System.out.println(e.getMessage());
    }
}}
Akhil
  • 78
  • 12
  • I want to read bytes from a .raw file and dump into an image. would this still apply to me? – SteelFox Feb 07 '16 at 09:32
  • I first thought it was file to image this code might not work i would like to suggest a previous post that states similar to your problem here is the link.http://stackoverflow.com/questions/26031746/how-to-convert-raw-file-to-image-file-in-android-rooted-device-programmatically – Akhil Feb 07 '16 at 09:38