集册 Java实例教程 文件读取和将来以及AsynchronousFileChannel

文件读取和将来以及AsynchronousFileChannel

欢马劈雪     最近更新时间:2020-01-02 10:19:05

687
文件读取和将来以及AsynchronousFileChannel

import java.nio.ByteBuffer;

import java.nio.channels.AsynchronousFileChannel;

import java.nio.charset.Charset;// from n o w j a v a . c o m - 时  代  Java

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) {

    ByteBuffer buffer = ByteBuffer.allocate(100);

    String encoding = System.getProperty("file.encoding");

    Path path = Paths.get("C:/folder1/folder0/folder8", "story.txt");

    try (AsynchronousFileChannel asynchronousFileChannel = AsynchronousFileChannel

        .open(path, StandardOpenOption.READ)) {

      Future<Integer> result = asynchronousFileChannel.read(buffer, 0);

      while (!result.isDone()) {
      /* from 
      时代Java - N o w  J a v a . c o m*/

        System.out.println("Do something else while reading ...");

      }

      System.out.println("Read done: " + result.isDone());

      System.out.pr
展开阅读全文