channel(通道)
Buffer(缓冲区)
selector(选择器)
读写切换
public static void main(String[] args) {
//创建一个Buffer,大小为5,可就是可以存放5个int
IntBuffer intBuffer=IntBuffer.allocate(5);
for(int i=0;i<intBuffer.capacity();i++){
intBuffer.put(i*2);
}
//读写切换
intBuffer.flip();
while(intBuffer.hasRemaining()){
System.out.println(intBuffer.get());
}
}
本地方法写
public static void main(String[] args) throws IOException {
String str="hello";
//创建一个输出流
FileOutputStream fileOutputStream=new FileOutputStream("src/test/JAVA/com/hua/nio/a");
//通过fileOutputStream获取对应的FileChannel,这个FielChannel的真实类型是FileChannelImpl
FileChannel fileChannel=fileOutputStream.getChannel();
//创建一个缓冲区ByteBuffer
ByteBuffer byteBuffer=ByteBuffer.allocate(1024);
//将str放到byteBuffer
byteBuffer.put(str.getBytes());
//对byteBuffer.flip();
byteBuffer.flip();
//把byteBuffer的数据写入到fileChannel
fileChannel.write(byteBuffer);
fileOutputStream.close();
}
本地方法读
public static void main(String[] args) throws IOException {
File file=new File("src/test/java/com/hua/nio/a");
FileInputStream fileInputStream =new FileInputStream(file);
FileChannel fileChannel=fileInputStream.getChannel();
ByteBuffer byteBuffer= ByteBuffer.allocate((int)file.length());
//将通道的数据读入到Buffer
fileChannel.read(byteBuffer);
//将byteBuffer的字节数据转成String
System.out.println(new String(byteBuffer.array()));
fileInputStream.close();
}
数据的双向交互
public static void main(String[] args) throws IOException {
FileInputStream fileInputStream=new FileInputStream("src/test/java/com/hua/nio/a");
FileOutputStream fileOutputStream=new FileOutputStream("src/test/java/com/hua/nio/b");
FileChannel fileChannel1=fileInputStream.getChannel();
FileChannel fileChannel2=fileOutputStream.getChannel();
ByteBuffer byteBuffer= ByteBuffer.allocate(512);
while(true){
//必须要clear,当下面fileChannel2的write的时候,此时的position会从0开始到limit,如果不clear回归到原来,
//当position和limit相等的时候,fileChannel1.read的返回结果为-1
byteBuffer.clear();
int read=fileChannel1.read(byteBuffer);
System.out.println(read);
if(read == -1){
break;
}
byteBuffer.flip();
fileChannel2.write(byteBuffer);
}
fileInputStream.close();
fileOutputStream.close();
}
通道的数据转换
public static void main(String[] args) throws IOException {
FileInputStream fileInputStream =new FileInputStream("src/test/java/com/hua/nio/a");
FileOutputStream fileOutputStream=new FileOutputStream("src/test/java/com/hua/nio/c");
FileChannel fileChannel1=fileInputStream.getChannel();
FileChannel fileChannel2=fileOutputStream.getChannel();
fileChannel2.transferFrom(fileChannel1,0,fileChannel1.size());
fileChannel1.close();
fileChannel2.close();
fileInputStream.close();
fileOutputStream.close();
}
类型的buffer
public static void main(String[] args) {
ByteBuffer buffer= ByteBuffer.allocate(64);
buffer.putInt(100);
buffer.putLong(5);
buffer.putChar('天');
buffer.putShort((short)11);
buffer.flip();
System.out.println();
System.out.println(buffer.getInt());
System.out.println(buffer.getLong());
System.out.println(buffer.getChar());
System.out.println(buffer.getShort());
}
只读的buffer
public static void main(String[] args) {
ByteBuffer buffer= ByteBuffer.allocate(64);
for(int i=0;i<64;i++){
buffer.put((byte)i);
}
buffer.flip();
//设置byteBuffer是只读的
ByteBuffer readOnlyBuffer=buffer.asReadOnlyBuffer();
System.out.println(readOnlyBuffer.getClass());
while(readOnlyBuffer.hasRemaining()){
System.out.println(readOnlyBuffer.get());
}
}
内存上的修改
public static void main(String[] args) throws IOException {
RandomaccessFile randomAccessFile=new RandomAccessFile("src/test/java/com/hua/nio/a","rw");
FileChannel channel=randomAccessFile.getChannel();
//FileChannel.MapMode.READ_WRITR使用的是读写模式
//第一个参数表示直接修改的起始位置
//第二个参数表示映射到内存的大小
//也就是说我们只能修改0到4的索引位置,
MAppedByteBuffer mappedByteBuffer=channel.map(FileChannel.MapMode.READ_WRITE,0,5);
mappedByteBuffer.put(0,(byte)'H');
mappedByteBuffer.put(2,(byte)'L');
randomAccessFile.close();
}