Tuesday, April 17, 2012

How to convert array of bytes into File


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
 
public class ArrayOfBytesToFile
{
    public static void main( String[] args )
    {
     FileInputStream fileInputStream=null;
 
        File file = new File("C:\\testing.txt");
 
        byte[] bFile = new byte[(int) file.length()];
 
        try {
            //convert file into array of bytes
     fileInputStream = new FileInputStream(file);
     fileInputStream.read(bFile);
     fileInputStream.close();
 
     //convert array of bytes into file
     FileOutputStream fileOuputStream = 
                  new FileOutputStream("C:\\testing2.txt"); 
     fileOuputStream.write(bFile);
     fileOuputStream.close();
 
     System.out.println("Done");
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

1 comment:

  1. Thanks for the post, I am techno savvy. I believe you hit the nail right on the head. I am highly impressed with your blog. It is very nicely explained. Your article adds best knowledge to our Java Online Training from India. or learn thru Java Online Training from India Students.

    ReplyDelete