8

I have an array of bytes

bytes[] doc

How can I create an instance of new File from those bytes?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Bob
  • 10,427
  • 24
  • 63
  • 71
  • 1
    Are you asking how to write a binary file to the file system? – Dawood ibn Kareem May 08 '14 at 05:50
  • Google for "Java IO tutorial". – JB Nizet May 08 '14 at 05:51
  • [Java Tutorial: Basic IO](http://docs.oracle.com/javase/tutorial/essential/io/) – Oleg Estekhin May 08 '14 at 05:51
  • try with http://www.mkyong.com/java/how-to-convert-array-of-bytes-into-file/ – bNd May 08 '14 at 05:51
  • No, just instance of File in memory. I do not need to write it to filesystem. – Bob May 08 '14 at 05:57
  • 3
    @Bob: a File in memory is not something that exists. A file is on a file system. An instance of File doesn't contain any byte. It's just a a file *path*. And reading fro a file will always read from the file system, not from a byte array in memory. If you want to read from a byte array in memory, then use a ByteArrayInputStream. If not, tell us what you actually want to achieve. – JB Nizet May 08 '14 at 05:59
  • OK, so what are the bytes? Which fields of the intended `File` are reflected in the bytes, and how? Or are you using the standard serialisation mechanism to represent your `File`? Please clarify the question. – Dawood ibn Kareem May 08 '14 at 06:15
  • If you want a file in memory, typically you read the file from disk and write it into a byte array using a ByteArrayOutputStream. This way, you have a representation of the file in memory, and it is in the form of a byte array. You seem to already have this. What is the reason for which you want it in some other format? – Alderath May 08 '14 at 06:24
  • 1
    The comments on this question make it extremely clear that what the OP wants is NOT what is asked in that other question. To anybody who bothers to read, it is obvious that this is not a duplicate. Why was it closed? – Dawood ibn Kareem May 08 '14 at 20:37

3 Answers3

22
 try (FileOutputStream fileOuputStream = new FileOutputStream("filename")){
    fileOuputStream.write(byteArray);
 }
mr.M
  • 851
  • 6
  • 23
  • 41
Suren Raju
  • 3,012
  • 6
  • 26
  • 48
  • 2
    You'd better close in a finally block, or better, use a try-with-resources statement. Your code will leak resources in case of an exception. – JB Nizet May 08 '14 at 05:55
  • 1
    But the [OP wrote](https://stackoverflow.com/questions/23533695/java-convert-bytes-to-file/23533769#comment36102606_23533695): *"No, just [an] instance of File in memory. I do not need to write it to [the] filesystem."* – Peter Mortensen Aug 31 '21 at 13:16
14

Try this:

Files.write(Paths.get("filename"), bytes);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • 1
    But the [OP wrote](https://stackoverflow.com/questions/23533695/java-convert-bytes-to-file/23533769#comment36102606_23533695): *"No, just [an] instance of File in memory. I do not need to write it to [the] filesystem."* – Peter Mortensen Aug 31 '21 at 13:19
5

A simple program that will write a byte[] into a file.

import java.io.File;
import java.io.FileOutputStream;

public class BytesToFile {

    public static void main(String[] args) {

        byte[] demBytes = null; // Instead of null, specify your bytes here.

        File outputFile = new File("LOCATION TO FILE");

        try ( FileOutputStream outputStream = new FileOutputStream(outputFile); ) {

            outputStream.write(demBytes);  // Write the bytes and you're done.

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

Note: The try-catch statement requires a Java version >= 1.7. Remember to change the bytes from null to correspond to your byte array.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
GabeTheApe
  • 348
  • 2
  • 13
  • 2
    About 10 minutes before you posted this, the OP posted a comment explaining that this was not what he wanted. – Dawood ibn Kareem May 08 '14 at 06:13
  • 2
    took 10 minutes to write on the iPad :( i guess someone else will find it useful. – GabeTheApe May 08 '14 at 06:52
  • 1
    But the [OP wrote](https://stackoverflow.com/questions/23533695/java-convert-bytes-to-file/23533769#comment36102606_23533695): *"No, just [an] instance of File in memory. I do not need to write it to [the] filesystem."* – Peter Mortensen Aug 31 '21 at 13:19