netty学习笔记09——filechannel的使用
先写总结
-
java.io.FileInputStream#getChannel,从一个文件输入流获取channel,这个channel是个只读channel,在getChannel的时候临时构造的
public FileChannel getChannel() { synchronized (this) { if (channel == null) { channel = FileChannelImpl.open(fd, path, true, false, this); } return channel; } }
-
java.io.FileOutputStream#getChannel,从一个输出流获取channel,这个channel是只能写的channel,和读的类似
public FileChannel getChannel() { synchronized (this) { if (channel == null) { channel = FileChannelImpl.open(fd, path, false, true, append, this); } return channel; } }
-
channel.read(byteBuffer),读到buffer里
-
channel.write(byteBuffer),写到buffer里
写入内容
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class NIOFileChannelWriter {
public static void main(String[] args) throws IOException {
//写一个字符串
String str = "hello, sirius";
//创建一个输出流
FileOutputStream fileOutputStream = new FileOutputStream("./hello2.txt");
//通过输出流获取文件channel
//这个channel真实类型是FileChannelImpl
FileChannel channel = fileOutputStream.getChannel();
//创建一个缓冲区
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
//将string,写入bytebuffer
byteBuffer.put(str.getBytes());
byteBuffer.flip();//切换读写模式,channel是从byteBuffer读取数据再进行写入的,所以要进行flip()
channel.write(byteBuffer);
fileOutputStream.close();
}
}
读取内容
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class NIOFileChannelRead {
public static void main(String[] args) throws IOException {
//创建一个输出流
FileInputStream fileInputStream = new FileInputStream("./hello2.txt");
//通过输出流获取文件channel
//这个channel真实类型是FileChannelImpl
FileChannel channel = fileInputStream.getChannel();
//创建一个缓冲区
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
channel.read(byteBuffer);//此时是写模式,从channel里读取数据写入到byteBuffer中
byteBuffer.flip();//切换成为读模式
System.out.println(new String(byteBuffer.array()));
}
}
读写内容
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class NIOFileChannelReadAndWriter {
public static void main(String[] args) throws IOException {
FileInputStream fileInputStream = new FileInputStream("./hello2.txt");
//通过输出流获取文件channel
//这个channel真实类型是FileChannelImpl
FileChannel readChannel = fileInputStream.getChannel();
FileOutputStream fileOutputStream = new FileOutputStream("./hello3.txt");
FileChannel writeChannel = fileOutputStream.getChannel();
//创建一个缓冲区
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
while (true){
byteBuffer.clear();//清空标记
int count = readChannel.read(byteBuffer);//写模式,从channel读数据到byteBuffer
if (count==-1){//读不到数据终止循环
break;
}
byteBuffer.flip();//读模式
writeChannel.write(byteBuffer);//从byteBuffer读数据到channel
}
}
}
快速复制
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
public class NIOFileChannelQuickCopy {
public static void main(String[] args) throws IOException {
FileInputStream fileInputStream = new FileInputStream("./hello2.txt");
//通过输出流获取文件channel
//这个channel真实类型是FileChannelImpl
FileChannel readChannel = fileInputStream.getChannel();
FileOutputStream fileOutputStream = new FileOutputStream("./hello3.txt");
FileChannel writeChannel = fileOutputStream.getChannel();
writeChannel.transferFrom(readChannel, 0, readChannel.size());
}
}