设为首页 加入收藏

TOP

Java I/O(3):NIO中的Buffer(二)
2023-07-25 21:25:27 】 浏览:39
Tags:Java I/O NIO Buffer
b);会抛异常BufferOverflowException
pb.put(buffer); // 叠加后数据是wiang,因为buffer的position=1 System.out.println("put(buffer)后bb状态: " + pb + ", buffer叠加后数据: " + new String(pb.array())); // 重新读取buffer中所有数据 System.out.println("测试rewind ======================>>>"); buffer.clear(); buffer.position(10); System.out.println("buffer当前状态: " + buffer); // 返回此缓冲区的限制 buffer.limit(15); System.out.println("limit后状态: " + buffer); // 把position设为0,mark设为-1,不改变limit的值 buffer.rewind(); System.out.println("rewind后状态: " + buffer); // 将所有未读的数据拷贝到Buffer起始处,然后将position设到最后一个未读元素正后面 System.out.println("测试compact ======================>>>"); buffer.clear(); buffer.put("abcd".getBytes()); System.out.println("compact前状态: " + buffer); System.out.println(new String(buffer.array())); // limit=position;position=0;mark=-1; 翻转,也就是让flip之后的position到limit这块区域变成之前的0到position这块 // 翻转就是将一个处于存数据状态的缓冲区变为一个处于准备取数据的状态,或者相反 buffer.flip(); System.out.println("flip后状态: " + buffer); // get()方法:相对读,从position位置读取一个byte,并将position+1,为下次读写作准备 System.out.println((char) buffer.get()); System.out.println((char) buffer.get()); System.out.println((char) buffer.get()); System.out.println("三次调用get后: " + buffer); System.out.println(new String(buffer.array())); // 把从position到limit中的内容移到0到limit-position的区域内 // position和limit的取值也分别变成limit-position、capacity // 如果先将positon设置到limit,再compact,那么相当于clear() buffer.compact(); System.out.println("compact后状态: " + buffer); System.out.println(new String(buffer.array()));

 

Java一般用BufferedInputStream、BufferedReader等带缓冲的I/O类来处理大文件,但如果文件超大的话,比如达到GB,甚至TB级别,更快的方式是采用NIO中引入的文件内存映射方案:MappedByteBuffer。

你只需要MappedByteBuffer读写性能极高,最主要的原因就是因为它实现了对异步操作的支持,就可以了!

可以用大文件来试一下:

// ByteBuffer读取大文件
public static void useFileChannel() {
    try{
        FileInputStream fis = new FileInputStream("你电脑上已经存在的文件路径,例如C:\\file1");
        FileChannel channel = fis.getChannel();
        long start = System.currentTimeMillis();
        ByteBuffer buff = ByteBuffer.allocate((int) channel.size());
        buff.clear();
        channel.read(buff);
        long end = System.currentTimeMillis();
        System.out.println(end - start);
        fis.close();
        channel.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

// MappedByteBuffer读取大文件
public static void useMappedByteBuffer() {
    try{
        FileInputStream fis = new FileInputStream("你电脑上已经存在的文件路径,例如C:\\file1");
        FileChannel channel = fis.getChannel();
        long start = System.currentTimeMillis();
        MappedByteBuffer mbb = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
        long end = System.currentTimeMillis();
        System.out.println(end - start);
        fis.close();
        channel.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public static void main(String[] args) {
    useFileChannel();
    useMappedByteBuffer();
}

 

最后把这两个方法放到main()里面试试看效果。

 

NIO中的Buffer说这么多已经足够了,用代码去感受会更直接。

 


 

 

感谢您的大驾光临!咨询技术、产品、运营和管理相关问题,请关注后留言。欢迎骚扰,不胜荣幸~

 

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇java连接sqlserver的方法分享 下一篇手写自定义springboot-starter,..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目