下面列出了java.nio.channels.AsynchronousFileChannel#write() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void writeFile(String filePath, String input) throws IOException {
Path path = Paths.get(filePath);
AsynchronousFileChannel afc = AsynchronousFileChannel.open(path, WRITE, CREATE);
WriteHandler handler = new WriteHandler();
ByteBuffer dataBuffer = ByteBuffer.wrap(input.getBytes());//getDataBuffer();
Attachment attach = new Attachment();
attach.asyncChannel = afc;
attach.buffer = dataBuffer;
attach.path = path;
afc.write(dataBuffer, 0, attach, handler);
System.out.println("Sleeping for 3 seconds...");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void writeFile(String filePath, String input) throws IOException {
Path path = Paths.get(filePath);
AsynchronousFileChannel afc = AsynchronousFileChannel.open(path, WRITE, CREATE);
WriteHandler handler = new WriteHandler();
ByteBuffer dataBuffer = ByteBuffer.wrap(input.getBytes());//getDataBuffer();
Attachment attach = new Attachment();
attach.asyncChannel = afc;
attach.buffer = dataBuffer;
attach.path = path;
afc.write(dataBuffer, 0, attach, handler);
System.out.println("Sleeping for 3 seconds...");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void givenPathAndContent_whenWritesToFileWithFuture_thenCorrect() throws IOException, ExecutionException, InterruptedException {
final String fileName = "temp";
final Path path = Paths.get(fileName);
final AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.CREATE);
final ByteBuffer buffer = ByteBuffer.allocate(1024);
final long position = 0;
buffer.put("hello world".getBytes());
buffer.flip();
final Future<Integer> operation = fileChannel.write(buffer, position);
buffer.clear();
operation.get();
final String content = readContent(path);
assertEquals("hello world", content);
}
/***************************************
* {@inheritDoc}
*/
@Override
protected boolean performAsyncOperation(
int nBytesWritten,
AsynchronousFileChannel rChannel,
ByteBuffer rData,
ChannelCallback<Integer, AsynchronousFileChannel> rCallback)
{
long nPosition = get(FILE_POSITION);
if (rData.hasRemaining())
{
if (nBytesWritten > 0)
{
nPosition += nBytesWritten;
}
rChannel.write(rData, nPosition, rData, rCallback);
return false;
}
else // finished
{
// remove position in the case of a later restart
deleteRelation(FILE_POSITION);
rData.clear();
return true;
}
}
private static void checkAsyncWrite(AsynchronousFileChannel asyncChannel) throws Throwable {
ByteBuffer buf = buffer("1234567890");
assertEquals(10, (int) asyncChannel.write(buf, 0).get());
buf.flip();
SettableFuture<Integer> future = SettableFuture.create();
asyncChannel.write(buf, 0, null, setFuture(future));
assertThat(future.get(10, SECONDS)).isEqualTo(10);
}
public static void main(String[] args) {
try {
//Path path = Paths.get("data/nio-data.txt");
Path path = Paths.get("data/test-write2.txt");
AsynchronousFileChannel fileChannelW =
AsynchronousFileChannel.open(path, StandardOpenOption.WRITE);
ByteBuffer buffer = ByteBuffer.allocate(1024);
long positionW = 0;
buffer.put("test data\r using Java SE NIO \r async with Future".getBytes());
buffer.flip();
Future<Integer> operationW = fileChannelW.write(buffer, positionW);
buffer.clear();
while(!operationW.isDone());
System.out.println("Write done");
AsynchronousFileChannel fileChannelR =
AsynchronousFileChannel.open(path, StandardOpenOption.READ);
//ByteBuffer buffer = ByteBuffer.allocate(1024);
buffer = ByteBuffer.allocate(1024);
long positionR = 0;
Future<Integer> operationR = fileChannelR.read(buffer, positionR);
while(!operationR.isDone());
buffer.flip();
byte[] data = new byte[buffer.limit()];
buffer.get(data);
System.out.println(new String(data));
buffer.clear();
} catch(IOException ioe) {
ioe.printStackTrace();
}
}