Here you can find the source of writeFile(File file, byte[] data)
public static void writeFile(File file, byte[] data) throws IOException
//package com.java2s; //License from project: Apache License import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; public class Main { private static final int MAX_BUFFER_SIZE = 4096; public static void writeFile(File file, byte[] data) throws IOException { FileOutputStream output = null; FileChannel fc = null;/* w w w .j a v a2 s . co m*/ try { output = new FileOutputStream(file); fc = output.getChannel(); ByteBuffer buffer = ByteBuffer.allocate(MAX_BUFFER_SIZE); int offset = 0; while (offset < data.length) { buffer.clear(); int len = data.length - offset; if (len > MAX_BUFFER_SIZE) len = MAX_BUFFER_SIZE; buffer.put(data, offset, len); offset += len; buffer.flip(); fc.write(buffer); } } finally { if (fc != null) close(fc); if (output != null) close(output); } } private static void close(InputStream input) { if (input != null) { try { input.close(); } catch (IOException e) { } } } private static void close(OutputStream output) { if (output != null) { try { output.close(); } catch (IOException e) { } } } private static void close(FileChannel channel) { if (channel != null) { try { channel.close(); } catch (IOException e) { } } } }