- 创建文件
- Future 读取文件
- CompletionHandler 读取文件
- Future 寫入文件
- CompletionHandler 寫入文件
- 参考文章
创建文件
public void create(String path, String fileName) throws IOException {
String uri = path + "/" + fileName;
AsynchronousFileChannel.open(Paths.get(uri),
StandardOpenOption.CREATE, StandardOpenOption.WRITE);
}
Future 读取文件
public void readWithFuture(String path, String fileName) throws IOException {
String uri = path + "/" + fileName;
System.out.println(uri);
AsynchronousFileChannel channel
= AsynchronousFileChannel.open(Paths.get(uri), StandardOpenOption.READ);
ByteBuffer buffer = ByteBuffer.allocate(1024);
long position = 0;
Future<Integer> operation = channel.read(buffer, position);
while(!operation.isDone());
buffer.flip();
byte[] data = new byte[buffer.limit()];
buffer.get(data);
System.out.println(new String(data));
buffer.clear();
}
CompletionHandler 读取文件
public void readWithCompletionHandler(String path, String fileName) throws IOException {
String uri = path + "/" + fileName;
System.out.println(uri);
AsynchronousFileChannel fileChannel =
AsynchronousFileChannel.open(Paths.get(uri), StandardOpenOption.READ);
ByteBuffer buffer = ByteBuffer.allocate(1024);
long position = 0;
fileChannel.read(buffer, position, buffer,
new CompletionHandler<Integer, ByteBuffer>() {
@Override
public void completed(Integer result, ByteBuffer buffer) {
System.out.println("Read Done");
System.out.println("result = " + result);
buffer.flip();
byte[] data = new byte[buffer.limit()];
buffer.get(data);
System.out.println(new String(data));
buffer.clear();
}
@Override
public void failed(Throwable exc, ByteBuffer attachment) {
System.out.println("Read failed");
exc.printStackTrace();
}
});
// 给终端显示留一些时间,实际项目可以删除
int cTime = 0;
while(cTime < 5) {
try {
Thread.sleep(500);
++cTime;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Future 写入文件
public void readWithFuture(String path, String fileName) throws IOException {
String uri = path + "/" + fileName;
System.out.println(uri);
AsynchronousFileChannel channel =
AsynchronousFileChannel.open(Paths.get(uri), StandardOpenOption.READ);
ByteBuffer buffer = ByteBuffer.allocate(1024);
long position = 0;
Future<Integer> operation = channel.read(buffer, position);
while(!operation.isDone());
buffer.flip();
byte[] data = new byte[buffer.limit()];
buffer.get(data);
System.out.println(new String(data));
buffer.clear();
}
CompletionHandler 写入文件
public void writeWithCompletionHandler(String path, String fileName, String message) throws IOException {
String uri = path + "/" + fileName;
System.out.println(uri);
final AsynchronousFileChannel channel =
AsynchronousFileChannel.open(Paths.get(uri),
StandardOpenOption.CREATE, StandardOpenOption.WRITE);
byte[] byteArray = message.getBytes();
ByteBuffer buffer = ByteBuffer.wrap(byteArray);
channel.write(buffer, 0, null, new CompletionHandler<Integer, ByteBuffer>(){
@Override
public void completed(Integer result, ByteBuffer attachment) {
System.out.println("Write done");
}
@Override
public void failed(Throwable exc, ByteBuffer attachment) {
System.out.println("Write failed");
exc.printStackTrace();
}
});
}
参考文章
Java NIO:http://tutorials.jenkov.com/java-nio/asynchronousfilechannel.html
String ByteBuffer转换:http://ivan4126.blog.163.com/blog/static/209491092201361621344523/