Program to Write byteArray to file using FileOutputStream in java file IO







Program to Write byteArray to file using FileOutputStream in java file IO >





import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/** JavaMadeSoEasy.com */
public class WriteByteArrayToFile {
   public static void main(String... args) {
          FileInputStream fis = null;
          FileOutputStream fos = null;
          try {
                
                 //First let's create byteArray by reading file in byteArray
                 fis = new FileInputStream("c:/myFile.txt");
                 byte[] bytes = new byte[fis.available()];
                 fis.read(bytes); //Read file in byteArray
                
                 //Now, let's Write byteArray to file
                 fos = new FileOutputStream("c:/myFile1.txt");
                 fos.write(bytes); //write byteArray into file
                 System.out.println("byteArray has been written to file");
                
          } catch (IOException e) {
                 e.printStackTrace();
          } finally {
                 try {
                       if (fis != null)
                              fis.close(); //close FileInputStream
                       if (fos != null){
                           fos.close(); //close FileOutputStream
                       }
                 } catch (IOException e) {
                       e.printStackTrace();
                 }
          }
   }
}
/* OUTPUT
byteArray has been written to file
*/

No comments:

Post a Comment