Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I write a byte array to a file in Java?

Tags:

java

How to write a byte array to a file in Java?

like image 623
rover12 Avatar asked Nov 20 '09 11:11

rover12


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.

Which stream is used to write a byte array to a file?

Java ByteArrayOutputStream class is used to write common data into multiple files. In this stream, the data is written into a byte array which can be written to multiple streams later. The ByteArrayOutputStream holds a copy of data and forwards it to multiple streams.

How do you write bytes in Java?

byte Syntax in JAVA:byte Variable_Name = Value; For example: byte x = 10; Here x is variable name and 10 is a value assigned to a variable integer data type byte.

How do you create a multipart file from a byte array?

Use the default CommonsMultipartFile where you to use the FileDiskItem object to create it. Example: FileItem fileItem = new DiskFileItem("fileData", "application/pdf",true, outputFile. getName(), 100000000, new java.


1 Answers

As Sebastian Redl points out the most straight forward now java.nio.file.Files.write. Details for this can be found in the Reading, Writing, and Creating Files tutorial.


Old answer: FileOutputStream.write(byte[]) would be the most straight forward. What is the data you want to write?

The tutorials for Java IO system may be of some use to you.

like image 50
Michael Lloyd Lee mlk Avatar answered Sep 21 '22 13:09

Michael Lloyd Lee mlk