NIO之FileChannel解读

目录

基本概述

打开 FileChannel

从 FileChannel 读取数据 

向 FileChannel 写数据

关闭 FileChannel

FileChannel 的 position 方法 

FileChannel 的 size 方法

FileChannel 的 truncate 方法 

FileChannel 的 force 方法

FileChannel 的 transferTo 和 transferFrom 方法

Scatter/Gather

Scattering Reads

Gathering Writes 


基本概述

FileChannel 类可以实现常用的 read,write 以及 scatter/gather 操作,同时它也提 供了很多专用于文件的新方法。这些方法中的许多都是我们所熟悉的文件操作。

下面是一个使用 FileChannel 读取数据到 Buffer 中的示例 :

RandomAccessFile既可以读取文件内容,也可以向文件输出数据。同时RandomAccessFile支持“随机访问”的方式,程序快可以直接跳转到文件的任意地方来读写数据。

public class FileChannelDemo {

    public static void main(String[] args) throws IOException {
        RandomAccessFile aFile = new
                RandomAccessFile("d:\\atguigu\\01.txt", "rw");
        FileChannel inChannel = aFile.getChannel();
        ByteBuffer buf = ByteBuffer.allocate(48);
        int bytesRead = inChannel.read(buf);
        while (bytesRead != -1) {
            System.out.println("读取: " + bytesRead);
            buf.flip();
            while (buf.hasRemaining()) {
                System.out.print((char) buf.get());
            }
            buf.clear();
            bytesRead = inChannel.read(buf);
        }
        aFile.close();
        System.out.println("操作结束");
    }
}

Buffer 通常的操作

将数据写入缓冲区

调用 buffer.flip() 反转读写模式

从缓冲区读取数据

调用 buffer.clear() 或 buffer.compact() 清除缓冲区内容 

打开 FileChannel

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

RandomAccessFile aFile = new RandomAccessFile("d:\\atguigu\\01.txt", "rw");
FileChannel inChannel = aFile.getChannel();

从 FileChannel 读取数据 

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

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

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

向 FileChannel 写数据

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

public class FileChannelDemo {
    public static void main(String[] args) throws IOException {
        RandomAccessFile aFile = new
                RandomAccessFile("d:\\atguigu\\01.txt", "rw");
        FileChannel inChannel = aFile.getChannel();
        String newData = "New String to write to file..." +
                System.currentTimeMillis();
        ByteBuffer buf1 = ByteBuffer.allocate(48);
        buf1.clear();
        buf1.put(newData.getBytes());
        buf1.flip();

        while (buf1.hasRemaining()) {
            inChannel.write(buf1);
        }
        inChannel.close();
    }
}

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

关闭 FileChannel

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

inChannel.close();

FileChannel 的 position 方法 

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

这里有两个例子:
long pos = channel.position();
channel.position(pos +123);

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

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

FileChannel 的 size 方法

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

long fileSize = channel.size();

FileChannel 的 truncate 方法 

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

channel.truncate(1024);

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

FileChannel 的 force 方法

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

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

FileChannel 的 transferTo 和 transferFrom 方法

通道之间的数据传输:

如果两个通道中有一个是 FileChannel,那你可以直接将数据从一个 channel 传输到 另外一个 channel。

(1)transferFrom()方法

FileChannel 的 transferFrom()方法可以将数据从源通道传输到 FileChannel 中(译 者注:这个方法在 JDK 文档中的解释为将字节从给定的可读取字节通道传输到此通道 的文件中)。下面是一个 FileChannel 完成文件间的复制的例子:

public class FileChannelWrite {
    public static void main(String args[]) throws Exception {
        RandomAccessFile aFile = new
                RandomAccessFile("d:\\atguigu\\01.txt", "rw");
        FileChannel fromChannel = aFile.getChannel();
        RandomAccessFile bFile = new
                RandomAccessFile("d:\\atguigu\\02.txt", "rw");
        FileChannel toChannel = bFile.getChannel();
        long position = 0;
        long count = fromChannel.size();
        toChannel.transferFrom(fromChannel, position, count);
        aFile.close();
        bFile.close();
        System.out.println("over!");
    }
}

方法的输入参数 position 表示从 position 处开始向目标文件写入数据,count 表示最 多传输的字节数。如果源通道的剩余空间小于 count 个字节,则所传输的字节数要小 于请求的字节数。此外要注意,在 SoketChannel 的实现中,SocketChannel 只会传 输此刻准备好的数据(可能不足 count 字节)。因此,SocketChannel 可能不会将请 求的所有数据(count 个字节)全部传输到FileChannel 中。 

(2)transferTo()方法

transferTo()方法将数据从 FileChannel 传输到其他的 channel 中。

Scatter/Gather

Java NIO 开始支持 scatter/gather,scatter/gather 用于描述从 Channel 中读取或 者写入到 Channel 的操作。

分散(scatter)从 Channel 中读取是指在读操作时将读取的数据写入多个 buffer 中。 因此,Channel 将从 Channel 中读取的数据“分散(scatter)”到多个 Buffer 中。

聚集(gather)写入 Channel 是指在写操作时将多个 buffer 的数据写入同一个 Channel,因此,Channel 将多个 Buffer 中的数据“聚集(gather)”后发送到 Channel。

scatter / gather 经常用于需要将传输的数据分开处理的场合,例如传输一个由消息头 和消息体组成的消息,你可能会将消息体和消息头分散到不同的 buffer 中,这样你可 以方便的处理消息头和消息体。

Scattering Reads

Scattering Reads 是指数据从一个 channel 读取到多个 buffer 中。如下图描述:

ByteBuffer header = ByteBuffer.allocate(128);
ByteBuffer body = ByteBuffer.allocate(1024);
ByteBuffer[] bufferArray = { header, body };
channel.read(bufferArray);

注意 buffer 首先被插入到数组,然后再将数组作为 channel.read() 的输入参数。 read()方法按照 buffer 在数组中的顺序将从 channel 中读取的数据写入到 buffer,当 一个 buffer 被写满后,channel 紧接着向另一个 buffer 中写。

Scattering Reads 在移动下一个 buffer 前,必须填满当前的 buffer,这也意味着它 不适用于动态消息(译者注:消息大小不固定)。换句话说,如果存在消息头和消息体, 消息头必须完成填充(例如 128byte),Scattering Reads 才能正常工作。

Gathering Writes 

Gathering Writes 是指数据从多个 buffer 写入到同一个 channel。如下图描述:

ByteBuffer header = ByteBuffer.allocate(128);
ByteBuffer body = ByteBuffer.allocate(1024);
//write data into buffers
ByteBuffer[] bufferArray = { header, body };
channel.write(bufferArray);

buffers 数组是 write()方法的入参,write()方法会按照 buffer 在数组中的顺序,将数 据写入到 channel,注意只有 position 和 limit 之间的数据才会被写入。因此,如果 一个 buffer 的容量为 128byte,但是仅仅包含 58byte 的数据,那么这 58byte 的数 据将被写入到 channel 中。因此与 Scattering Reads 相反,Gathering Writes 能较 好的处理动态消息。 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: FileChannel是Java NIO中的一个类,它用于从文件中读取数据或向文件写入数据。FileChannel是非阻塞的,这意味着当调用FileChannel的读写方法时,线程不会被阻塞。相反,它会立即返回一个Future对象,你可以在后面调用这个对象的get()方法来获取实际读写操作的结果。 例如,你可以使用FileChannel来读取文件的内容,代码如下: ``` Path path = Paths.get("file.txt"); FileChannel channel = FileChannel.open(path, StandardOpenOption.READ); ByteBuffer buffer = ByteBuffer.allocate(1024); Future<Integer> result = channel.read(buffer, 0); int bytesRead = result.get(); ``` 你也可以使用FileChannel向文件写入数据,代码如下: ``` Path path = Paths.get("file.txt"); FileChannel channel = FileChannel.open(path, StandardOpenOption.WRITE); ByteBuffer buffer = ByteBuffer.wrap("Hello, World!".getBytes()); Future<Integer> result = channel.write(buffer, 0); int bytesWritten = result.get(); ``` 需要注意的是,FileChannel只能用于文件,如果你想要从网络或其他非文件源读取数据,你需要使用Java NIO中的其他类,例如SocketChannel或ServerSocketChannel。 ### 回答2: FileChannel是Java NIO中表示文件的通道。它是对文件进行读取和写入的通道,类似于传统IO中的InputStream和OutputStream。 通过FileChannel,我们可以使用缓冲区(Buffer)来读取和写入文件的数据。我们可以创建一个FileChannel对象来操作文件,这可以通过FileInputStream、FileOutputStream或RandomAccessFile等类的getChannel()方法来获取。 FileChannel支持阻塞式和非阻塞式的IO操作。我们可以使用read()方法从通道中读取数据,使用write()方法将数据写入通道。还可以使用transferTo()和transferFrom()方法在通道之间直接传输数据,避免了传统IO中需要使用缓冲区来进行数据复制的过程。 FileChannel还提供了其他的一些有用的方法,比如position()和position(long newPosition)用于获取和设置文件指针的位置;size()方法用于获取文件的大小;truncate(long size)用于截断文件等。 在操作文件时,通常需要首先打开一个FileChannel,然后创建一个缓冲区来进行数据的读取和写入。一般的操作流程是,首先从通道中读取数据到缓冲区中,然后进行相应的处理,最后将数据写入到文件中。 总之,FileChannel是Java NIO中用于操作文件的通道,它提供了更加灵活和高效的文件操作方式。通过FileChannel,我们可以使用缓冲区来读取和写入文件的数据,还可以直接在通道之间传输数据。同时,FileChannel还提供了一些方便的方法来操作文件的指针和大小等属性。使用FileChannel可以提高文件IO操作的效率和灵活性。 ### 回答3: NIO(New Input/Output)中的FileChannel是用于文件的通道类,它提供了一种在文件中读取、写入和操作数据的方式。 FileChannel可以通过Java程序对文件进行各种操作,包括读取文件内容、写入数据到文件,以及对文件指定位置进行读写操作等。 FileChannel的一些主要功能包括: 1. 读取文件内容:可以使用FileChannel的read()方法来读取文件中的数据,可以一次读取一定数量的字节,也可以读取整个文件的内容。 2. 写入文件内容:使用FileChannel的write()方法,可以将数据写入文件,可以一次写入一定数量的字节,也可以覆盖原有内容或者追加到文件末尾。 3. 文件位置操作:FileChannel的position()方法可以获取当前位置,也可以通过position(long newPosition)方法设置新的位置。可以根据实际需要在文件中进行定位,进行读写操作。 4. 通道之间的传输:FileChannel提供了transferFrom()和transferTo()方法用于直接在通道之间传输数据,可以高效地传输大量数据。 5. 文件的锁定:可以使用FileChannel的lock()和tryLock()方法对文件进行锁定,确保多个线程或进程之间对同一个文件的读写操作的互斥性。 总之,FileChannelNIO中用于处理文件的通道,它提供了一系列的API方法,可以方便地读取、写入和操作文件中的数据,同时还提供了一些文件的操作特性,如文件位置操作、通道之间的数据传输以及文件的锁定等。使用FileChannel可以更灵活地对文件进行操作,并且相比于传统的IO操作,它在性能上可以得到一定的提升。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一个风轻云淡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值