public static void main(String[] args) throws IOException {// 这里的"rw"是指支持读和写RandomAccessFile randomAccessFile = new RandomAccessFile("C:\\Users\\Administrator\\Desktop\\data.txt","rw");FileChannel fileChannel = randomAccessFile.getChannel();// 读取文件内容:ByteBuffer buffer = ByteBuffer.allocate(1024);int num = fileChannel.read(buffer);System.out.println("读取的数据量:" + num + ",内容为:\r\n" + new String(buffer.array()));buffer.clear();// 向文件内追加内容:祝源码班各位大帅比迎娶白富美!buffer.put("\r\n".getBytes());buffer.put("祝源码班各位大帅比迎娶白富美!".getBytes());buffer.flip();while(buffer.hasRemaining()) {// 将 Buffer 中的内容写入文件fileChannel.write(buffer);}}
FileChannel 本地文件读写
一个Buffer写,一个Buffer读

//本地文件写数据public static void main(String[] args) throws Exception{String str = "hello";//创建一个输出流->channelFileOutputStream fileOutputStream = new FileOutputStream("d:\\file01.txt");//通过 fileOutputStream 获取 对应的 FileChannel//这个 fileChannel 真实 类型是 FileChannelImplFileChannel fileChannel = fileOutputStream.getChannel();//创建一个缓冲区 ByteBufferByteBuffer byteBuffer = ByteBuffer.allocate(1024);//将 str 放入 byteBufferbyteBuffer.put(str.getBytes());//对byteBuffer 进行flipbyteBuffer.flip();//将byteBuffer 数据写入到 fileChannelfileChannel.write(byteBuffer);fileOutputStream.close();}//本地文件读数据public static void main(String[] args) throws Exception {//创建文件的输入流File file = new File("d:\\file01.txt");FileInputStream fileInputStream = new FileInputStream(file);//通过fileInputStream 获取对应的FileChannel -> 实际类型 FileChannelImplFileChannel fileChannel = fileInputStream.getChannel();//创建缓冲区ByteBuffer byteBuffer = ByteBuffer.allocate((int) file.length());//将 通道的数据读入到BufferfileChannel.read(byteBuffer);//将byteBuffer 的 字节数据 转成StringSystem.out.println(new String(byteBuffer.array()));fileInputStream.close();}
一个Buffer读写文件

//使用一个Buffer完成文件读取public static void main(String[] args) throws Exception {FileInputStream fileInputStream = new FileInputStream("1.txt");FileChannel fileChannel01 = fileInputStream.getChannel();FileOutputStream fileOutputStream = new FileOutputStream("2.txt");FileChannel fileChannel02 = fileOutputStream.getChannel();ByteBuffer byteBuffer = ByteBuffer.allocate(512);while (true) { //循环读取//这里有一个重要的操作,一定不要忘了/*public final Buffer clear() {position = 0;limit = capacity;mark = -1;return this;}*/byteBuffer.clear(); //清空bufferint read = fileChannel01.read(byteBuffer);System.out.println("read =" + read);if(read == -1) { //表示读完break;}//将buffer 中的数据写入到 fileChannel02 -- 2.txtbyteBuffer.flip();fileChannel02.write(byteBuffer);}//关闭相关的流fileInputStream.close();fileOutputStream.close();}
拷贝文件
transferFrom
//拷贝文件transferFrom 方法public static void main(String[] args) throws Exception {//创建相关流FileInputStream fileInputStream = new FileInputStream("d:\\a.jpg");FileOutputStream fileOutputStream = new FileOutputStream("d:\\a2.jpg");//获取各个流对应的filechannelFileChannel sourceCh = fileInputStream.getChannel();FileChannel destCh = fileOutputStream.getChannel();//使用transferForm完成拷贝destCh.transferFrom(sourceCh,0,sourceCh.size());//关闭相关通道和流sourceCh.close();destCh.close();fileInputStream.close();fileOutputStream.close();}
transferTo
超过 2g 大小的文件传输
public class TestFileChannelTransferTo {public static void main(String[] args) {try (FileChannel from = new FileInputStream("data.txt").getChannel();FileChannel to = new FileOutputStream("to.txt").getChannel();) {// 效率高,底层会利用操作系统的零拷贝进行优化, 2g 数据long size = from.size();// left 变量代表还剩余多少字节for (long left = size; left > 0; ) {System.out.println("position:" + (size - left) + " left:" + left);left -= from.transferTo((size - left), left, to);}} catch (IOException e) {e.printStackTrace();}}}
