Java AsynchronousFileChannel read from file
import java.nio.ByteBuffer; import java.nio.channels.AsynchronousFileChannel; import java.nio.charset.Charset; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; import java.util.concurrent.Future; public class Main { public static void main(String[] args) { Path path = Paths.get("Main.java"); try (AsynchronousFileChannel afc = AsynchronousFileChannel.open(path, StandardOpenOption.READ)) { // Get a data buffer of the file size to read int fileSize = (int) afc.size(); ByteBuffer dataBuffer = ByteBuffer.allocate(fileSize); // Perform the asynchronous read operation Future<Integer> result = afc.read(dataBuffer, 0); int readBytes = result.get(); System.out.println(readBytes); byte[] byteData = dataBuffer.array(); Charset cs = Charset.forName("UTF-8"); String data = new String(byteData, cs); System.out.println(data);/*from w w w .j a va 2s. c o m*/ } catch (Exception ex) { ex.printStackTrace(); } } }
import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.AsynchronousFileChannel; import java.nio.channels.CompletionHandler; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; import java.util.EnumSet; import java.util.concurrent.ExecutorService; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; public class Main { public static void main(String args[]) { ExecutorService pool = new ScheduledThreadPoolExecutor(3); try (AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(Paths.get("Main.java"), EnumSet.of(StandardOpenOption.READ), pool)) { System.out.println("Main Thread ID: " + Thread.currentThread().getId()); CompletionHandler<Integer, ByteBuffer> handler = new CompletionHandler<Integer, ByteBuffer>() { @Override/* w w w . ja v a 2s . c om*/ public synchronized void completed(Integer result, ByteBuffer attachment) { for (int i = 0; i < attachment.limit(); i++) { System.out.print((char) attachment.get(i)); } System.out.println("CompletionHandler Thread ID: " + Thread.currentThread().getId()); } @Override public void failed(Throwable e, ByteBuffer attachment) { System.out.println("Failed"); } }; final int bufferCount = 5; ByteBuffer buffers[] = new ByteBuffer[bufferCount]; for (int i = 0; i < bufferCount; i++) { buffers[i] = ByteBuffer.allocate(10); fileChannel.read(buffers[i], i * 10, buffers[i], handler); } pool.awaitTermination(1, TimeUnit.SECONDS); System.out.println("Byte Buffers"); for (ByteBuffer byteBuffer : buffers) { for (int i = 0; i < byteBuffer.limit(); i++) { System.out.print((char) byteBuffer.get(i)); } System.out.println(); } } catch (IOException | InterruptedException ex) { ex.printStackTrace(); } } }