- 一、通道(channel)介绍
- 二、主要实现类
- 三、获取通道
- 四、案例
- 4.1 FileChannel-文件复制
- 4.2 SocketChannel、ServerSocketChannel
- 4.3 DatagramChannel
Channel是一个对象,作用是用于源节点和目标节点的连接,在java NIO中负责缓冲区数据的传递。Channel本身不存储数据,因此需要配合缓冲区进行传输。
主要的实现类有如下四个: FileChannel, SocketChannel, ServerSocketChannel, DatagramChannel,
- Java针对支持通道的类提供了getChannel()方法
| 本地IO | 网络IO |
|---|---|
| FileInputStream/FileOutputStream | Socket |
| RandomAccessFile | ServerSocket |
| 无 | DatagramSocket |
- 在JDK1.7中的NIO.2针对各个通道提供了静态方法open()
- 在JDK1.7中的NIO.2的Files工具类的newByteChannel()
public static void main(String[] args) throws Exception {
// 1本地IO获取通道
FileInputStream inputStream = new FileInputStream(resourceFilePath);
FileOutputStream outputStream = new FileOutputStream(targetFilePath);
FileChannel inChannel = inputStream.getChannel();
FileChannel outChannel = outputStream.getChannel();
// 2.通过open方法获取
FileChannel.open(Paths.get(resourceFilePath), StandardOpenOption.READ);
FileChannel.open(Paths.get(targetFilePath),
StandardOpenOption.READ,
StandardOpenOption.WRITE,
StandardOpenOption.CREATE);
}
四、案例
4.1 FileChannel-文件复制
public class FileNioTest {
public static void main(String[] args) {
String resourceFilePath = "D:\test\copyFileTest.txt";
String targetFilePath = "D:\test\copyFileTest-1.txt";
// 复制文件
// copyFile1(resourceFilePath, targetFilePath);
// copyFile2(resourceFilePath, targetFilePath);
copyFile3(resourceFilePath, targetFilePath);
}
private static void copyFile1(String resourceFilePath, String targetFilePath) {
// 使用FileChannel配合缓冲区实现文件复制的功能
try (
FileInputStream inputStream = new FileInputStream(resourceFilePath);
FileOutputStream outputStream = new FileOutputStream(targetFilePath);
FileChannel inChannel = inputStream.getChannel();
FileChannel outChannel = outputStream.getChannel();
) {
ByteBuffer buffer = ByteBuffer.allocate(1024);
while (inChannel.read(buffer) != -1) {
// 切换读模式
buffer.flip();
outChannel.write(buffer);
// 清空
buffer.clear();
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static void copyFile2(String resourceFilePath, String targetFilePath) {
// 内存映射文件的方式实现文件复制
try (
FileChannel inChannel = FileChannel.open(Paths.get(resourceFilePath), StandardOpenOption.READ);
FileChannel outChannel = FileChannel.open(Paths.get(targetFilePath),
StandardOpenOption.READ,
StandardOpenOption.WRITE,
StandardOpenOption.CREATE);
) {
MappedByteBuffer inMap = inChannel.map(FileChannel.MapMode.READ_ONLY, 0, inChannel.size());
MappedByteBuffer outMap = outChannel.map(FileChannel.MapMode.READ_WRITE, 0, inChannel.size());
byte[] b = new byte[inMap.limit()];
// 从磁盘文件中获取数据写入到b字节数组中
inMap.get(b);
// 将b字节数组中的数据写入到磁盘文件中
outMap.put(b);
} catch (Exception e) {
e.printStackTrace();
}
}
private static void copyFile3(String resourceFilePath, String targetFilePath) {
// Channel-to-channel方式实现复制
try (
FileChannel inChannel = FileChannel.open(Paths.get(resourceFilePath), StandardOpenOption.READ);
FileChannel outChannel = FileChannel.open(Paths.get(targetFilePath),
StandardOpenOption.READ,
StandardOpenOption.WRITE,
StandardOpenOption.CREATE);
) {
// inChannel.transferTo(0, inChannel.size(), outChannel);
outChannel.transferFrom(inChannel, 0, inChannel.size());
} catch (Exception e) {
e.printStackTrace();
}
}
}
4.2 SocketChannel、ServerSocketChannel
4.3 DatagramChannel
参考:https://blog.csdn.net/qq_38526573/article/details/89207100



