首页 > 编程语言 >java异步读取文件2种实现

java异步读取文件2种实现

时间:2022-11-10 02:11:06浏览次数:42  
标签:异步 java nio buffer util ByteBuffer import 读取

`import com.sun.tools.jconsole.JConsoleContext;

import java.io.;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousFileChannel;
import java.nio.channels.CompletionHandler;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.file.OpenOption;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.time.LocalDate;
import java.util.
;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Main {
public static void main(String[] args) throws IOException, ExecutionException, InterruptedException {
/基于Future接口的异步读取文件/
// AsynchronousFileChannel channel = AsynchronousFileChannel.open(Paths.get("G:\C#图解教程(第四版高清).pdf"), StandardOpenOption.READ);
// ByteBuffer buffer = ByteBuffer.allocateDirect(13010241024);
// var result = channel.read(buffer,0);
// while(!result.isDone())
// {
// System.out.printf("我还没完成呢,宝贝!");
// }
// buffer.flip();//将缓存字节数组的指针设置为数组的开始下标0,否则读的就是最后一个位置byte = 0
// byte[] data = new byte[buffer.limit()];
// buffer.get(data);
// //System.out.println(data.toString());
// buffer.clear();

    /*基于回调的读取文件*/
    var fc = new CompletionHandler<Integer,Object>(){
        @Override
        public void completed(Integer result, Object attachment) {
            System.out.println("我完成了"+result);
        }
        @Override
        public void failed(Throwable exc, Object attachment) {
            System.out.println("我出了点问题!" +exc.getMessage());
        }
    };
    ByteBuffer buffer = ByteBuffer.allocateDirect(130*1024*1024);
    try (AsynchronousFileChannel channel = AsynchronousFileChannel.open(Paths.get("G:\\C#图解教程(第四版高清).pdf"),StandardOpenOption.READ,StandardOpenOption.WRITE)){
        channel.write(buffer,0,null ,fc);
        Thread.sleep(1000);
    }

}

}

`

标签:异步,java,nio,buffer,util,ByteBuffer,import,读取
From: https://www.cnblogs.com/liflower/p/16875765.html

相关文章

  • [JavaScript-05]函数和箭头函数
    1.函数functionaddNums(num1=1,num2=2){console.log('num1+num2:',num1+num2);returnnum1+num2;}addNums();addNums(5,6);console.log('addNums(......
  • Java创建对象的5种方式
    new:最常见的方法Employeeemp1=newEmployee();newInstance()用newInstance()创建对象分2种1、使用class类的newInstance()方法需要有一个无参的构造方法,这个n......
  • Java集合简单介绍
    Java集合框架主要包括两种类型的容器,一种是Collection,存储一个元素集合,另一种是Map,存储键/值对映射。一、Collection集合List集合特点:有序可重复ArrayList集合(内部......
  • 关于Java中枚举Enum的深入剖析
    在编程语言中我们,都会接触到枚举类型,通常我们进行有穷的列举来实现一些限定。Java也不例外。Java中的枚举类型为Enum,本文将对枚举进行一些比较深入的剖析。什么是EnumEnum是......
  • JAVA-this关键字
    packagecom.itheima;/*学生类*/publicclassstudent02{//成员变量privateStringname;privateintage;publicvoidsetAge(intage){......
  • [JavaScript-03]IF 三元表达式 逻辑运算 == ===
    1.语句//if语句letx=10;if(x==10){console.log('xis10')};//ifelseifelsex=20;if(x===10){console.log('xis10');}elsei......
  • Java代码块运行顺序细节阐述
    以下这个例子非常好的阐述了父子类同时存在时静态代码块/静态变量初始化,普通代码块/普通成员变量初始化,构造器之间的具体运行顺序。注意,在构造器开头,我们可以看作按顺序隐......
  • [JavaScript-04]Switch
    1.Switch//Switch语句constcolor='green';switch(color){case'red':console.log('colorisred');break;case'blue':......
  • JAVA-private关键字
    packagecom.itheima;/*学生类*/publicclassstudent01{//成员变量privateStringname;//给name设置privateprivateintage;publicvoid......
  • [JavaScript-02]FOR WHILE循环
    1.语句//For语句for(letindex=0;index<=6;index++){console.log(`ForLoopNumber:${index}`);}//While语句leti=0;while(i<=6){c......