How to convert array of bytes into File
Published: April 7, 2010 , Updated: March 30, 2010 , Author: mkyong
The Java.io.FileOutputStream can used to convert an array of bytes into a file. In this example, you read a file from “C:\\testing.txt“, and convert it into an array of bytes, and write it into another file “C:\\testing2.txt“.
package com.mkyong.common; 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(); } } }
Any Java questions or problems? please post at this JavaNullPointer.com forum, see you there ~