Java – How to save byte[] to file

To save byte[] into a file, try this:


  FileOutputStream fos = new FileOutputStream(fileDest);
  fos.write(bytesArray);
  fos.close();

or NIO


  Path path = Paths.get(fileDest);
  Files.write(path, bytesArray);

Full Example

This Java Example shows you how to read a file into a byte array, and save the byte array back to a new file via the classic try-catch-try-catch, JDK 7 try-resources and Java.NIO solution.

ArrayOfBytesToFile.java

package com.mkyong;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class ArrayOfBytesToFile {

    private static final String UPLOAD_FOLDER = "C:\\temp\\";

    public static void main(String[] args) {

        FileInputStream fileInputStream = null;

        try {

            File file = new File("C:\\temp\\testing1.txt");
            byte[] bFile = new byte[(int) file.length()];

            //read file into bytes[]
            fileInputStream = new FileInputStream(file);
            fileInputStream.read(bFile);

            //save bytes[] into a file
            writeBytesToFile(bFile, UPLOAD_FOLDER + "test1.txt");
            writeBytesToFileClassic(bFile, UPLOAD_FOLDER + "test2.txt");
            writeBytesToFileNio(bFile, UPLOAD_FOLDER + "test3.txt");

            System.out.println("Done");

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }

    //Classic, < JDK7
    private static void writeBytesToFileClassic(byte[] bFile, String fileDest) {

        FileOutputStream fileOuputStream = null;

        try {
            fileOuputStream = new FileOutputStream(fileDest);
            fileOuputStream.write(bFile);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileOuputStream != null) {
                try {
                    fileOuputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    //Since JDK 7 - try resources
    private static void writeBytesToFile(byte[] bFile, String fileDest) {

        try (FileOutputStream fileOuputStream = new FileOutputStream(fileDest)) {
            fileOuputStream.write(bFile);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    //Since JDK 7, NIO
    private static void writeBytesToFileNio(byte[] bFile, String fileDest) {

        try {
            Path path = Paths.get(fileDest);
            Files.write(path, bFile);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

References

  1. How to convert File into an array of bytes
  2. Files JavaDoc
  3. FileOutputStream JavaDoc
author image

mkyong

Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities. Read all published posts by

Comments

avatar
3000
newest oldest most voted
mush
Guest
mush

then how to read it back as byte[] variable??

guest
Guest
guest

thank you

Riaz Akbar Ali
Guest
Riaz Akbar Ali

is there any way or api to convert Image or any type of file byte into PDF

Andy
Guest
Andy

This has a repeated typo – please change “ouput” to “output” throughout your examples.

sunil kore
Guest
sunil kore

How can we do same thing with spring batch?
(byte array to pdf using spring batch)

suresh
Guest
suresh

How to remove or update the metadata/Exif/Property from Base64/byte Array file Android?

cleber augusto de abreu
Guest
cleber augusto de abreu

simples e objetivo… perfeito!!

simple goal … perfect!!

Thank you!

John
Guest
John

thanks ..mykong,…it worked for me..

Pablo
Guest
Pablo

Muchas gracias, yo estaba intentando leer byte a byte y guardar cada byte en una posicion del arreglo de bytes. Pero se tardaba demaciado.

Apollloloo
Guest
Apollloloo

?ngilizce tutoriola kendi dili ile yorum yapan amele !!!!

Manuel Mancipe
Guest
Manuel Mancipe

I need create the file object can not exist on the machine, only in memory, how to do?

Thank you

Melissa
Guest
Melissa

may i ask some help for our case study? thanks in advance ^_^

Ali
Guest
Ali

Can we use the same method for applications also?

yurica
Guest
yurica

Gracias es justo lo que estaba buscando =)