tags:

views:

275

answers:

5

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

+8  A: 

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.

mlk
byte[] encoded = key.getEncoded();i need to write encoded to a text file
rover12
What is `key` ?
Aaron Digulla
KeyGenerator kgen = KeyGenerator.getInstance("AES");kgen.init(128);SecretKey key = kgen.generateKey();byte[] encoded = key.getEncoded();
rover12
Then simply use FileOutputStream.
mlk
+1 for mentioning tutorials... (you'd received even a +2 if you mentioned www.google.com - OK, that was nasty, but it's not rovers first question...)
Andreas_D
You should wrap the FileOutputStream in a BufferedOutputStream as suggested by @JuanZe. Performance is much better.
Sam Barnum
+1  A: 

Apache Commons IO Utils has a FileUtils.writeByteArrayToFile() method. Note that if you're doing any file/IO work then the Apache Commons IO library will do a lot of work for you.

Brian Agnew
A: 

You can use IOUtils.write(byte[] data, OutputStream output) from Apache Commons IO.

KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128);
SecretKey key = kgen.generateKey();
byte[] encoded = key.getEncoded();
IOUtil.write(encoded, new FileOutputStream(new File("target-file")))
Why add a 3rd party library for this?
jarnbjo
IOUtil is wrong it is IOUtils.write
rover12
A: 

To write a byte array to a file use the method

public void write(byte[] b) throws IOException

from BufferedOutputStream class.

java.io.BufferedOutputStream implements a buffered output stream. By setting up such an output stream, an application can write bytes to the underlying output stream without necessarily causing a call to the underlying system for each byte written.

For your example you need something like:

String filename= "C:/SO/SOBufferedOutputStreamAnswer";
BufferedOutputStream bos = null;
try {
//create an object of FileOutputStream
FileOutputStream fos = new FileOutputStream(new File(filename));

//create an object of BufferedOutputStream
bos = new BufferedOutputStream(fos);

KeyGenerator kgen = KeyGenerator.getInstance("AES"); 
kgen.init(128); 
SecretKey key = kgen.generateKey(); 
byte[] encoded = key.getEncoded();

bos.write(encoded);

} 
// catch and handle exceptions...
JuanZe
+1 for mentioning BufferedOutputStream. You should ALWAYS wrap a FileOutputStream in a BufferedOutputStream, performance is much better.
Sam Barnum
If all you want to write to the file is a 16 byte key, wrapping the FileOutputStream in a BufferedOutputStream is probably slower than writing the data directly to the FileOutputStream.
jarnbjo
+2  A: 

A commenter asked "why use a third-party library for this?" The answer is that it's way too much of a pain to do it yourself. Here's an example of how to properly do the inverse operation of reading a byte array from a file (sorry, this is just the code I had readily available, and it's not like I want the asker to actually paste and use this code anyway):

public static byte[] toByteArray(File file) throws IOException { 
   ByteArrayOutputStream out = new ByteArrayOutputStream(); 
   boolean threw = true; 
   InputStream in = new FileInputStream(file); 
   try { 
     byte[] buf = new byte[BUF_SIZE]; 
     long total = 0; 
     while (true) { 
       int r = in.read(buf); 
       if (r == -1) {
         break; 
       }
       out.write(buf, 0, r); 
     } 
     threw = false; 
   } finally { 
     try { 
       in.close(); 
     } catch (IOException e) { 
       if (threw) { 
         log.warn("IOException thrown while closing", e); 
       } else {
         throw e;
       } 
     } 
   } 
   return out.toByteArray(); 
 }

Everyone ought to be thoroughly appalled by what a pain that is.

Use Good Libraries. I, unsurprisingly, recommend Guava's Files.write(byte[], File).

Kevin Bourrillion