通道(Channel)
所有的 IO 在NIO 中都从一个Channel开始,数据可以从Channel读到Buffer中,也可以从Buffer写到Channel中
channel必须注册到selector
Channel与流的区别:
- BIO 中的 stream 是单向的,例如 FileInputStream 对象只能进行读取数据的操作,而 NIO 中的通道(Channel)是双向的,可以读操作,也可以写操作。
- 通道可以异步地读写。
- 通道中的数据总是要先读到一个Buffer,或者总是要从一个Buffer中写入
- Channel在NIO中是一个接口
public interface Channel extends Closeable{}
常用的 Channel 类
- FileChannel:用于文件的数据读写
- DatagramChannel:用于 UDP 的数据读写
- ServerSocketChannel 和 SocketChannel:用于 TCP 的数据读写。【ServerSocketChanne 类似 ServerSocket , SocketChannel 类似 Socket】
FileChannel:用于文件的数据读写
public int read(ByteBuffer dst) ,从通道读取数据并放到缓冲区中
public int write(ByteBuffer src) ,把缓冲区的数据写到通道中
public long transferFrom(ReadableByteChannel src, long position, long count),从目标通道中复制数据到当前通道
public long transferTo(long position, long count, WritableByteChannel target),把数据从当前通道复制给目标通道
通道之间的数据传输
- FileChannel的transferFrom()方法可以将数据从其他通道传输到FileChannel中
- transferTo()方法将数据从FileChannel传输到其他的channel中
-
RandomAccessFile fromFile = new RandomAccessFile("fromFile.txt", "rw"); FileChannel fromChannel = fromFile.getChannel(); RandomAccessFile toFile = new RandomAccessFile("toFile.txt", "rw"); FileChannel toChannel = toFile.getChannel(); //从position处开始向目标文件写入数据 long position = 0; //count表示最多传输的字节数。 // 如果源通道的剩余空间小于 count 个字节,则所传输的字节数要小于请求的字节数。 long count = fromChannel.size(); // toChannel.transferFrom(position, count, fromChannel); //transferTo()方法将数据从FileChannel传输到其他的channel中 //fromChannel.transferTo(position, count, toChannel);
Java 的几种文件拷贝方式
利用 java.io 类库,直接为源文件构建一个 FileInputStream 读取,然后再为目标文件构建一个 FileOutputStream,完成写入工作。
利用 java.nio 类库提供的 transferTo 或 transferFrom 方法实现。 NIO transferTo/From 的方式可能更快,因为它更能利用现代操作系统底层机制,避免不必要拷贝和上下文切换