本地文件写数据,如果没有文件创建文件
/**
* 使用ByteBuffer(缓冲)和FileChannel(通道),将Hello NIO写入到file01.txt文件中
* 如果文件不存在创建文件
* @author hrui
* @date 2024/7/27 22:27
*/
public class NIOFileChannel01 {
public static void main(String[] args) throws IOException {
String str = "Hello NIO";
//1.创建一个输出流(Channel还是通过输入输出流创建)
FileOutputStream fileOutputStream = new FileOutputStream("D:\\file01.txt");
//2.通过fileOutputStream获取对应的FileChannel
//这个fileChannel对象的真实类型是FileChannelImpl
FileChannel fileChannel = fileOutputStream.getChannel();
//3.创建一个缓冲区(ByteBuffer)
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
//4.将str放入到ByteBuffer
byteBuffer.put(str.getBytes());
//5.将byteBuffer的指针复位
byteBuffer.flip();
//6.将byteBuffer的数据写入到fileChannel
fileChannel.write(byteBuffer);
fileOutputStream.close();
}
}
从本地文件读数据
/**
* 本地文件读数据
* @author hrui
* @date 2024/7/28 7:50
*/
public class NIOFileChannel02 {
public static void main(String[] args) throws IOException {
//创建文件的输入流
File file = new File("D:\\file01.txt");
FileInputStream fileInputStream = new FileInputStream(file);
//通过fileInputStream 获取对应的FileChannel
FileChannel fileChannel = fileInputStream.getChannel();
//创建缓冲区
ByteBuffer byteBuffer = ByteBuffer.allocate((int) file.length());
//将通道的数据读入到缓冲区
fileChannel.read(byteBuffer);
//将缓冲区的数据转成字符串
System.out.println(new String(byteBuffer.array()));
fileInputStream.close();
}
}
文件拷贝
package org.example.demo;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
/**
* 使用一个Buffer完成文件拷贝
* 将D盘目录下的1.txt(随便写点内容)拷贝到2.txt
* @author hrui
* @date 2024/7/28 8:14
*/
public class NIOFileChannel03 {
public static void main(String[] args) throws IOException {
// 创建一个输入流 读文件
FileInputStream fileInputStream = new FileInputStream("D://1.txt");
FileChannel channel01 = fileInputStream.getChannel();
// 创建一个输出流 写文件
FileOutputStream fileOutputStream = new FileOutputStream("D://2.txt");
FileChannel channel02 = fileOutputStream.getChannel();
ByteBuffer allocate = ByteBuffer.allocate(5);
//循环读取
while(true){
//这里有个重要的操作,不要忘记
allocate.clear();
// 从通道读取512字节数据放入ByteBuffer
int read = channel01.read(allocate);
if (read == -1){//如果读到-1说明读完了
break;
}
//没有读完 将buffer已经有的数据直接写入到channel02
allocate.flip();//切换模式
channel02.write(allocate);
}
//关闭通道
channel01.close();
channel02.close();
//关闭流
fileInputStream.close();
fileOutputStream.close();
}
}
拷贝的另外一种方式
把D盘一张图片进行拷贝 例如 1.PNG -----> 2.PNG
package org.example.demo;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
/**
* @author hrui
* @date 2024/7/28 9:30
*/
public class NIOFileChannel04 {
public static void main(String[] args) throws IOException {
//创建相关流
FileInputStream fileInputStream = new FileInputStream("3.jpg");
FileOutputStream fileOutputStream = new FileOutputStream("4.jpg");
//创建相关通道
FileChannel channel01 = fileInputStream.getChannel();
FileChannel channel02 = fileOutputStream.getChannel();
//使用transferFrom完成传输(拷贝)
channel02.transferFrom(channel01, 0, channel01.size());
//关闭相关通道和流
channel01.close();
channel02.close();
fileInputStream.close();
fileOutputStream.close();
}
}