该篇简单介绍下文件channel的基本使用:读文件 channel.read()、写文件 channel.write()、文件传输 channel.transferFrom() + channel.transferTo() 。
1、读文件
通过FileChanel+ByteBuffer读取文件内容
public static void main(String[] args) throws IOException {
String filePath="C:\\Users\\Administrator\\Desktop\\test.txt";
// 1、创建一个FileChannel; rw表示读写模式
RandomAccessFile randomAccessFile=new RandomAccessFile(filePath,"rw");
FileChannel channel = randomAccessFile.getChannel();
// 2、创建buffer
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
// 2、通过channel读文件到buffer中
int byteNum = channel.read(byteBuffer);
// -1 表示读取到最后一行了
while (byteNum!=-1){
System.out.println("读取了:"+byteNum);
// buffer中是否有内容
while (byteBuffer.hasRemaining()){
// 读写切换(内部游标操作)
byteBuffer.flip();
byte b = byteBuffer.get();
System.out.println((char)b);
}
// 清空缓冲区
byteBuffer.clear();
// 清空缓冲区后继续读文件
byteNum=channel.read(byteBuffer);
}
randomAccessFile.close();
}
2、写文件
public static void main(String[] args) throws IOException {
// 1、创建channel; 文件不存在会自动创建
String filePath="C:\\Users\\Administrator\\Desktop\\test_write.txt";
RandomAccessFile randomAccessFile=new RandomAccessFile(filePath, "rw");
// 2、创建channel
FileChannel fileChannel = randomAccessFile.getChannel();
// 3、创建一个buffer
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
// 4、向buffer中写一个内容
String tempContext="this is context :123456789";
byteBuffer.put(tempContext.getBytes());
// 5、读写切换
byteBuffer.flip();
while (byteBuffer.hasRemaining()){ // 如果有剩余内容
fileChannel.write(byteBuffer);
// 因为不知道是否buffer中还有内容,需要循环调用
}
// 6、关闭channel
fileChannel.close();
System.out.println("结束");
}
3、文件传输
3.1 transferTo()
吧当前channel内容传输到另外一个channel,用来把一个文件内容复制到另外一个文件中。
public static void main(String[] args) throws IOException {
// 源文件的channel
String fromPath="C:\\Users\\Administrator\\Desktop\\test.txt";
RandomAccessFile randomAccessFile1=new RandomAccessFile(fromPath,"rw");
FileChannel fromChannel = randomAccessFile1.getChannel();
// 要复制到的目标文件的channel
String targetPath="C:\\Users\\Administrator\\Desktop\\test_write.txt";
RandomAccessFile randomAccessFile=new RandomAccessFile(targetPath, "rw");
FileChannel targetChannel = randomAccessFile.getChannel();
// 三个参数: 从哪开始复制、复制多少、复制到哪个channel中
fromChannel.transferTo(0, fromChannel.size(), targetChannel);
fromChannel.close();
targetChannel.close();
System.out.println("完毕");
}
3.2 transferFrom()
把一个channel中的内容传输到当前channel。
public static void main(String[] args) throws IOException {
// 源文件的channel
String fromPath="C:\\Users\\Administrator\\Desktop\\test.txt";
RandomAccessFile randomAccessFile1=new RandomAccessFile(fromPath,"rw");
FileChannel fromChannel = randomAccessFile1.getChannel();
// 要复制到的目标文件的channel
String targetPath="C:\\Users\\Administrator\\Desktop\\test_write.txt";
RandomAccessFile randomAccessFile=new RandomAccessFile(targetPath, "rw");
FileChannel targetChannel = randomAccessFile.getChannel();
// 三个参数: 从哪个channel来的、 从哪开始复制、复制多少
targetChannel.transferFrom(fromChannel, 0, fromChannel.size());
fromChannel.close();
targetChannel.close();
System.out.println("完毕");
}