什么时候使用FileChannel读取()/write()文件? [英] When Use FileChannel to read()/write() files?

查看:618
本文介绍了什么时候使用FileChannel读取()/write()文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读《用Java思考》一书,其中解释了java.nio.*软件包,并说NIO比使用传统IO流读取和写入文件要快.为什么?

I am reading the book Thinking in Java, which explains the java.nio.* package and says that NIO is faster than reading and writing files with traditional IO streams. Why?

我已经查看了以下信息:

I have reviewed the following information:

  • IO流是面向字节的,传统IO处理单元是字节,而NIO处理单元是块(字节数组),但是我认为传统IO也可以通过BufferedFile *和传统方法直接处理块(字节数组) IO也有直接的字节数组处理方法

  • IO stream is byte oriented, traditional IO processing unit is byte, and NIO processing unit is block (byte array), but I think traditional IO can also directly process block (byte array) through BufferedFile*, and traditional IO also has direct Method of processing byte array

private native int readBytes(byte b[], int off, int len) throws IOException;

  • IO正在阻止读取,NIO可以是非阻止的,但是我发现文件NIO只能是非阻止的,那么NIO没有优势.

  • IO is blocking read, NIO can be non-blocking, but I found that the file NIO can only be non-blocking, then NIO has no advantage.

    我认为使用NIO通常是需要使用NIO的其他优点,例如:

    I think the need to use NIO is generally other advantages that need to use NIO, such as:

     transferTo()/transferFrom()
    

  • 那么,什么时候应该使用NIO进行文件读写?为什么它比传统的IO更快?正确的使用方式是什么?我应该只在读写文件时使用IO还是NIO吗?

    So, when should I use NIO for file reading and writing? Why is it faster than traditional IO? What is the correct way to use it? Should I use IO or NIO only when reading and writing files?

    推荐答案

    在只有两种情况下,FileChannelFileInputStreamFileOutputStream快.

    There are only two cases where a FileChannel is faster than a FileInputStream or FileOutputStream.

    第一个是可以使用堆外(直接")ByteBuffer来保存数据,以便不将其复制到Java堆中的情况.例如,如果您正在编写将静态文件传递到套接字的Web服务器,则使用FileInputStreamSocketChannel而不是FileInputStreamSocketOutputStream会更快.

    The first is when you can use an off-heap ("direct") ByteBuffer to hold data, so that it isn't copied into the Java heap. For example, if you were writing a web-server that delivered static files to a socket, it would be faster to use a FileInputStream and a SocketChannel rather than a FileInputStream and a SocketOutputStream.

    在我看来,这些案例很少而且相差甚远.通常,当您使用Java读取(或写入)文件时,您将对数据进行某些处理.在这种情况下,您无法避免将数据复制到堆上.

    These cases are, in my opinion, very few and far between. Normally when you read (or write) a file in Java you will be doing something with the data. In which case, you can't avoid copying the data onto the heap.

    FileChannel的另一个用途是创建一个MappedByteBuffer以随机访问文件的内容.这比使用RandomAccessFile快得多,因为它用利用操作系统分页机制的内存访问代替了对操作系统内核的显式调用.

    The other use for a FileChannel is to create a MappedByteBuffer for random access to the contents of a file. This is significantly faster than using RandomAccessFile because it replaces explicit calls to the OS kernel with memory accesses that leverage the OS's paging mechanism.

    如果您刚刚开始使用Java使用I/O,建议您坚持使用java.io中的类,除非并且直到您能解释为什么切换到java.nio可以提高性能.使用面向流的抽象要比面向块的抽象容易得多.

    If you're just getting started with I/O in Java, I recommend sticking with the classes in java.io unless and until you can explain why switching to java.nio will give you improved performance. It's much easier to use a stream-oriented abstraction than a block-oriented one.

    这篇关于什么时候使用FileChannel读取()/write()文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆