Java之FileChannel类的理解和使用

来源:互联网 发布:李乐的霍去病 知乎 编辑:程序博客网 时间:2024/05/21 02:52

Java NIO中的FileChannel是一个连接到文件的通道。可以通过文件通道读写文件。

FileChannel无法设置为非阻塞模式,它总是运行在阻塞模式下。

打开FileChannel

在使用FileChannel之前,必须先打开它。但是,我们无法直接打开一个FileChannel,需要通过使用一个InputStream、OutputStream或RandomAccessFile来获取一个FileChannel实例。下面是通过RandomAccessFile打开FileChannel的示例:

1RandomAccessFile aFile = newRandomAccessFile("data/nio-data.txt","rw");
2FileChannel inChannel = aFile.getChannel();

从FileChannel读取数据

调用多个read()方法之一从FileChannel中读取数据。如:

1ByteBuffer buf = ByteBuffer.allocate(48);
2int bytesRead = inChannel.read(buf);

首先,分配一个Buffer。从FileChannel中读取的数据将被读到Buffer中。

然后,调用FileChannel.read()方法。该方法将数据从FileChannel读取到Buffer中。read()方法返回的int值表示了有多少字节被读到了Buffer中。如果返回-1,表示到了文件末尾。

向FileChannel写数据

使用FileChannel.write()方法向FileChannel写数据,该方法的参数是一个Buffer。如:

01String newData = "New String to write to file..." + System.currentTimeMillis();
02 
03ByteBuffer buf = ByteBuffer.allocate(48);
04buf.clear();
05buf.put(newData.getBytes());
06 
07buf.flip();
08 
09while(buf.hasRemaining()) {
10    channel.write(buf);
11}

注意FileChannel.write()是在while循环中调用的。因为无法保证write()方法一次能向FileChannel写入多少字节,因此需要重复调用write()方法,直到Buffer中已经没有尚未写入通道的字节。

关闭FileChannel

用完FileChannel后必须将其关闭。如:

1channel.close();

FileChannel的position方法

有时可能需要在FileChannel的某个特定位置进行数据的读/写操作。可以通过调用position()方法获取FileChannel的当前位置。

也可以通过调用position(long pos)方法设置FileChannel的当前位置。

这里有两个例子:

1long pos = channel.position();
2channel.position(pos +123);

如果将位置设置在文件结束符之后,然后试图从文件通道中读取数据,读方法将返回-1 —— 文件结束标志。

如果将位置设置在文件结束符之后,然后向通道中写数据,文件将撑大到当前位置并写入数据。这可能导致“文件空洞”,磁盘上物理文件中写入的数据间有空隙。

FileChannel的size方法

FileChannel实例的size()方法将返回该实例所关联文件的大小。如:

1long fileSize = channel.size();

FileChannel的truncate方法

可以使用FileChannel.truncate()方法截取一个文件。截取文件时,文件将中指定长度后面的部分将被删除。如:

1channel.truncate(1024);

这个例子截取文件的前1024个字节。

FileChannel的force方法

FileChannel.force()方法将通道里尚未写入磁盘的数据强制写到磁盘上。出于性能方面的考虑,操作系统会将数据缓存在内存中,所以无法保证写入到FileChannel里的数据一定会即时写到磁盘上。要保证这一点,需要调用force()方法。

force()方法有一个boolean类型的参数,指明是否同时将文件元数据(权限信息等)写到磁盘上。

下面的例子同时将文件数据和元数据强制写到磁盘上:

1channel.force(true);


使用

说那么多可能没用,我们还是直接来看看分别使用两种方法进行文件复制的对比。

首先是普通的输入输出流进行复制文件:

    /**     * 普通的文件复制方法     *     * @param fromFile 源文件     * @param toFile   目标文件     * @throws FileNotFoundException 未找到文件异常     */    public void fileCopyNormal(File fromFile, File toFile) throws FileNotFoundException {        InputStream inputStream = null;        OutputStream outputStream = null;        try {            inputStream = new BufferedInputStream(new FileInputStream(fromFile));            outputStream = new BufferedOutputStream(new FileOutputStream(toFile));            byte[] bytes = new byte[1024];            int i;            //读取到输入流数据,然后写入到输出流中去,实现复制            while ((i = inputStream.read(bytes)) != -1) {                outputStream.write(bytes, 0, i);            }        } catch (Exception e) {            e.printStackTrace();        } finally {            try {                if (inputStream != null)                    inputStream.close();                if (outputStream != null)                    outputStream.close();            } catch (IOException e) {                e.printStackTrace();            }        }    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

在上面的代码中,传入源文件和目标文件两个参数,然后根据两个文件,分别出具输入输出流,然后将输入流的数据读取,并且写入输出流中,就完成了文件的复制操作。

下面再看一下利用fileChannel进行文件的复制操作。

    /**     * 用filechannel进行文件复制     *     * @param fromFile 源文件     * @param toFile   目标文件     */    public void fileCopyWithFileChannel(File fromFile, File toFile) {        FileInputStream fileInputStream = null;        FileOutputStream fileOutputStream = null;        FileChannel fileChannelInput = null;        FileChannel fileChannelOutput = null;        try {            fileInputStream = new FileInputStream(fromFile);            fileOutputStream = new FileOutputStream(toFile);            //得到fileInputStream的文件通道            fileChannelInput = fileInputStream.getChannel();            //得到fileOutputStream的文件通道            fileChannelOutput = fileOutputStream.getChannel();            //将fileChannelInput通道的数据,写入到fileChannelOutput通道            fileChannelInput.transferTo(0, fileChannelInput.size(), fileChannelOutput);        } catch (IOException e) {            e.printStackTrace();        } finally {            try {                if (fileInputStream != null)                    fileInputStream.close();                if (fileChannelInput != null)                    fileChannelInput.close();                if (fileOutputStream != null)                    fileOutputStream.close();                if (fileChannelOutput != null)                    fileChannelOutput.close();            } catch (IOException e) {                e.printStackTrace();            }        }    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

上面代码中,也是先分别创建了两个文件的输入输出流,然后在分别获取到两个文件的文件通道,然后将源文件的文件通道直接和目标文件的文件通道进行连接,直接将数据写入到目标文件中区。不需要进行分别的读取和写入操作了。

运行代码之后,复制一个文件,对比两种复制方法,发现利用filechannel使用的时间比普通的读取输入时间缩短了将近一半。尤其是在进行大文件复制的时候,filechannel显得更加有优势。这里就不贴出图片了。见谅!


总结

这里我们了解了FileChannel类,知道了它所具有的特点和功能,那么我们就可以好好的使用它了。尤其是在我们复制文件的时候,可以更好的利用这个类,可以提高效率,也可以防止出现oom等其它情况。




参考博客地址:

http://ifeve.com/file-channel/

http://blog.csdn.net/qq_16628781/article/details/70532307