Java Reference
In-Depth Information
// Get the data to write in a ByteBuffer
ByteBuffer dataBuffer = getDataBuffer();
// Prepare the attachment
Attachment attach = new Attachment();
attach.asyncChannel = afc;
attach.buffer = dataBuffer;
attach.path = path;
// Perform the asynchronous write operation
afc.write(dataBuffer, 0, attach, handler);
try {
// Let the thread sleep for five seconds,
// to allow the asynchronous write is complete
System.out.println("Sleeping for 5 seconds...");
Thread.sleep(5000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Done...");
}
catch (IOException e) {
e.printStackTrace();
}
}
public static ByteBuffer getDataBuffer() {
String lineSeparator = System.getProperty("line.separator");
StringBuilder sb = new StringBuilder();
sb.append("My heart leaps up when I behold");
sb.append(lineSeparator);
sb.append("A Rainbow in the sky");
sb.append(lineSeparator);
sb.append(lineSeparator);
sb.append("So was it when my life began;");
sb.append(lineSeparator);
sb.append("So is it now I am a man;");
sb.append(lineSeparator);
sb.append("So be it when I shall grow old,");
sb.append(lineSeparator);
sb.append("Or let me die!");
sb.append(lineSeparator);
sb.append(lineSeparator);
sb.append("The Child is father of the man;");
sb.append(lineSeparator);
sb.append("And I could wish my days to be");
Search WWH ::




Custom Search