通道 Channel
用于源节点和目标节点的连接,负责数据的传输,Channel可以类比为铁路,Buffer可以类比为高铁,人群就是数据。
java.nio.channels.Channel 接口主要有以下实现
- FileChannel 文件通道,用作文件操作
- SocketChannel 套接字通道,用作网络编程客户端
- ServerScoketChannel 服务端套接字通道,用作网络编程服务端
- DatagramChnnel 数据报通道,用作UDP协议数据传输数据
//测试文件拷贝
@Test
void t1() throws IOException {
String source = "/Users/liupeng/IdeaProjects/test/1.txt";
String des = "/Users/liupeng/IdeaProjects/test/2.txt";
FileInputStream inputStream = new FileInputStream(source);
FileOutputStream outputStream = new FileOutputStream(des);
//1.获取Channel
FileChannel inChannel = inputStream.getChannel();
FileChannel outChannel = outputStream.getChannel();
//2.分配缓冲区 Buffer
ByteBuffer buf = ByteBuffer.allocate(1024);
// ByteBuffer buf = ByteBuffer.allocateDirect(1024);//直接使用操作系统内存
//拷贝文件
while (inChannel.read(buf) != -1) {
buf.flip();
outChannel.write(buf);
buf.clear();
}
//关闭流和通道
outChannel.close();
inChannel.close();
outputStream.close();
inputStream.close();
}
//通道间数据传输
@Test
void t5() throws IOException {
String source = "/Users/liupeng/IdeaProjects/test/1.txt";
String des = "/Users/liupeng/IdeaProjects/test/3.txt";
FileChannel in = FileChannel.open(Paths.get(source));
FileChannel out = FileChannel.open(Paths.get(des),
StandardOpenOption.CREATE,StandardOpenOption.WRITE);
// in.transferTo(0, in.size(), out);
out.transferFrom(in, 0, in.size());
in.close();
out.close();
}
//使用直接缓冲区完成文件的复制(内存映射文件)
@Test
public void t8() throws IOException {
String source = "/Users/liupeng/IdeaProjects/test/1.txt";
String des = "/Users/liupeng/IdeaProjects/test/5.txt";
FileChannel inChannel = FileChannel.open(Paths.get(source), StandardOpenOption.READ);
FileChannel outChannel = FileChannel.open(Paths.get(des),
StandardOpenOption.WRITE, StandardOpenOption.READ, StandardOpenOption.CREATE);
//内存映射文件
MappedByteBuffer inMappedBuf = inChannel.map(FileChannel.MapMode.READ_ONLY, 0, inChannel.size());
MappedByteBuffer outMappedBuf = outChannel.map(FileChannel.MapMode.READ_WRITE, 0, inChannel.size());
//直接对缓冲区进行数据的读写操作
byte[] dst = new byte[inMappedBuf.limit()];
inMappedBuf.get(dst);
outMappedBuf.put(dst);
inChannel.close();
outChannel.close();
}
//分散和聚集
@Test
public void test4() throws IOException {
String source = "/Users/liupeng/IdeaProjects/test/1.txt";
String des = "/Users/liupeng/IdeaProjects/test/5.txt";
RandomAccessFile sourceFile = new RandomAccessFile(source, "rw");
//1. 获取通道
FileChannel sourceChannel = sourceFile.getChannel();
//2. 分配指定大小的缓冲区
ByteBuffer buf1 = ByteBuffer.allocate(10);
ByteBuffer buf2 = ByteBuffer.allocate(5);
ByteBuffer buf3 = ByteBuffer.allocate(20);
//3. 分散读取
ByteBuffer[] buffers = {buf1, buf2,buf3};
sourceChannel.read(buffers);
for (ByteBuffer b : buffers) {
b.flip();
System.out.println(new String(b.array()));
}
//4. 聚集写入
RandomAccessFile desFile = new RandomAccessFile(des, "rw");
FileChannel desChannel = desFile.getChannel();
desChannel.write(buffers);
}