Skip to content
Advertisement

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

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

Advertisement

Answer

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();
FileOutputStream output = new FileOutputStream(new File("target-file"));
IOUtils.write(encoded, output);
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement