Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a file from a ByteArrayOutputStream

Can someone explain how I can get a file object if I have only a ByteArrayOutputStream. How to create a file from a ByteArrayOutputStream?

like image 500
Al Phaba Avatar asked Jul 05 '13 12:07

Al Phaba


People also ask

How do I write a byte array to a file?

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.

How do I create a ByteArrayInputStream file?

Use the FileUtils#readFileToByteArray(File) from Apache Commons IO, and then create the ByteArrayInputStream using the ByteArrayInputStream(byte[]) constructor. Show activity on this post. create a ByteArrayInputStream around the file content, which is now in memory.


1 Answers

You can do it with using a FileOutputStream and the writeTo method.

ByteArrayOutputStream byteArrayOutputStream = getByteStreamMethod(); try(OutputStream outputStream = new FileOutputStream("thefilename")) {     byteArrayOutputStream.writeTo(outputStream); } 

Source: "Creating a file from ByteArrayOutputStream in Java." on Code Inventions

like image 164
Suresh Atta Avatar answered Oct 25 '22 03:10

Suresh Atta