Java examples for File Path IO:ByteBuffer
Collecting many buffers in a gathering write
import java.io.FileOutputStream; import java.nio.ByteBuffer; import java.nio.channels.GatheringByteChannel; import java.util.LinkedList; import java.util.List; public class Main { private static final String fileName = "test.txt"; public static void main(String[] argv) throws Exception { int reps = 10; FileOutputStream fos = new FileOutputStream(fileName); GatheringByteChannel gatherChannel = fos.getChannel(); ByteBuffer[] bs = utterBS(reps); while (gatherChannel.write(bs) > 0) { // Loop until write( ) returns zero }/*from www.j ava 2 s . c o m*/ System.out.println(fileName); fos.close(); } private static String newline = System.getProperty("line.separator"); private static ByteBuffer[] utterBS(int howMany) throws Exception { List list = new LinkedList(); for (int i = 0; i < howMany; i++) { list.add(pickRandom()); list.add(pickRandom()); list.add(pickRandom()); } ByteBuffer[] bufs = new ByteBuffer[list.size()]; list.toArray(bufs); return (bufs); } private static ByteBuffer pickRandom() throws Exception { String string = "asdf"; int total = string.length(); ByteBuffer buf = ByteBuffer.allocate(total); buf.put(string.getBytes("US-ASCII")); buf.flip(); return (buf); } }