通道提供与I/O服务的直接连接 用于在字节缓冲区和位于通道另一侧的实体(通常是一个文件或套接字)之间有效地传输数据
多数情况 通道与操作系统的文件描述符和文件句柄有着一对一的关系
借助通道 可以最小的开销来访问操作系统本身的I/O服务 缓冲区是通道内部发送和接收数据的端点
File I/O和Stream I/O
文件通道 套接字通道
创建通道:
文件通道:
package com.tony.app;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class CharBufferDemo {
public static void main(String[] args) throws Exception {
// 使用文件通道读写数据
// 读
FileInputStream fs = new FileInputStream(new File("C:\\read.txt"));
FileChannel fileChannel_read = fs.getChannel();
// 通道只能使用ByteBuffer
ByteBuffer byteBuffer = ByteBuffer.allocate(10);
int len = 0;
byte[] bytes = new byte[10];
while ((len = fileChannel_read.read(byteBuffer)) > 0) {
byteBuffer.flip();
byteBuffer.get(bytes,0,len);
byteBuffer.clear();
System.out.println(new String(bytes));
}
fileChannel_read.close();
fs.close();
// 写
// 从FileInputStream的getChannel()获取的FileChannel是只读的
// write的话会抛出java.nio.channels.NonWritableChannelException异常
// FileInputStream fos = new FileInputStream (new File("C:\\write.txt"));
// FileChannel fileChannel_write = fos.getChannel();
// ByteBuffer byteBuffer_write = ByteBuffer.allocate(10);
// byteBuffer_write.put("tony".getBytes());
// byteBuffer.flip();
// fileChannel_write.write(byteBuffer_write);
RandomAccessFile raf = new RandomAccessFile(new File("C:\\write.txt"),"rw");
FileChannel fileChannel_write = raf.getChannel();
ByteBuffer byteBuffer_write = ByteBuffer.allocate(10);
byteBuffer_write.put("tony".getBytes());
byteBuffer_write.flip();
fileChannel_write.write(byteBuffer_write);
}
}
tonywenxin
tonytonyts
dfdfdfdfdf
关闭通道:
通道提供了Scatter/Gather的功能:
在多个缓冲区上实现一个简单I/O操作
对于write操作 数据是从几个缓冲区按顺序抽取(gather)并沿通道发送的 该gather过程的效果就像全部缓冲区的内容被连接起来 并在发送数据前存放到一个大的缓冲区中
对read操作 从通道读取的数据会按照顺序被散布(scatter)到多个缓冲区 将每个缓冲区填满直至通道中的数据或者缓冲区的最大空间被消耗完
scatter/gather经常用于需要将传输的数据分开处理的场合 例如传输一个由消息头和消息头组成的消息
你可能会将消息体和消息头分散到不同的Buffer中 这样可以方便的处理消息头和消息体
scatter read:
gather write:
文件通道:
文件空洞:
文件锁定:
锁是可以共享也可以独占
文件锁定依赖本地操作系统的实现 不是所有的操作系统和文件系统都支持共享文件锁 不支持共享的 被自动提升为独占
不是所有的平台都以同一个方式实现基本的文件锁定 不同的OS 或同一个OS上不同的文件系统 文件锁定都会存在差异
文件通道实现的文件锁定 锁的对象是文件不是通道或线程 意味着文件锁不适用于判优同一台JVM上的多个线程发起的访问
内存映射文件:P87
package com.tony.app;
import java.io.File;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
public class CharBufferDemo {
public static void main(String[] args) throws Exception {
File file = new File("C:\\write.txt");
RandomAccessFile raf = new RandomAccessFile(file,"rw");
FileChannel fileChannel_write = raf.getChannel();
MappedByteBuffer mappedByteBuffer = fileChannel_write.map(FileChannel.MapMode.READ_ONLY, 0, file.length());
while(mappedByteBuffer.hasRemaining()) {
System.out.print((char)mappedByteBuffer.get());
}
fileChannel_write.close();
raf.close();
}
}
Channel to Channel 传输:
如果两个通道中有一个是FileChannel 可以直接将数据从一个Channel传输到另一个Channel
不能在socket通道之间直接传输数据
package com.tony.app;
import java.io.File;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
public class CharBufferDemo {
public static void main(String[] args) throws Exception {
File from = new File("C:\\write.txt");
File to = new File("C:\\read.txt");
RandomAccessFile raf_from = new RandomAccessFile(from,"rw");
FileChannel fileChannel_from = raf_from.getChannel();
RandomAccessFile raf_to = new RandomAccessFile(to,"rw");
FileChannel fileChannel_to = raf_to.getChannel();
// fileChannel_to.transferFrom(fileChannel_from, 0, fileChannel_from.size());
fileChannel_from.transferTo(0,fileChannel_from.size() , fileChannel_to);
}
}