Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert bytes[] to 'File' in Java [duplicate]

Tags:

java

file

byte

I have an array of bytes

bytes[] doc

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

like image 744
Bob Avatar asked May 08 '14 05:05

Bob


People also ask

Can we convert byte array to file in java?

Convert byte[] array to File using Java In order to convert a byte array to a file, we will be using a method named the getBytes() method of String class. Implementation: Convert a String into a byte array and write it in a file.

What does byte [] do in java?

A byte array is an array of bytes (tautology FTW!). You could use a byte array to store a collection of binary data, for example, the contents of a file. The downside to this is that the entire file contents must be loaded into memory.

Can you print [] byte?

You can simply iterate the byte array and print the byte using System. out. println() method.

What is Class for byte [] in java?

Byte class is a wrapper class for the primitive type byte which contains several methods to effectively deal with a byte value like converting it to a string representation, and vice-versa. An object of Byte class can hold a single byte value. Byte class offers four constants in the form of Fields.


2 Answers

 try (FileOutputStream fileOuputStream = new FileOutputStream("filename")){
    fileOuputStream.write(byteArray);
 }
like image 101
Suren Raju Avatar answered Sep 28 '22 09:09

Suren Raju


Try this:

Files.write(Paths.get("filename"), bytes);
like image 33
Evgeniy Dorofeev Avatar answered Sep 28 '22 08:09

Evgeniy Dorofeev