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

FileChannel 本地文件读写

一个Buffer写,一个Buffer读

image.jpeg

  1. //本地文件写数据
  2. public static void main(String[] args) throws Exception{
  3. String str = "hello";
  4. //创建一个输出流->channel
  5. FileOutputStream fileOutputStream = new FileOutputStream("d:\\file01.txt");
  6. //通过 fileOutputStream 获取 对应的 FileChannel
  7. //这个 fileChannel 真实 类型是 FileChannelImpl
  8. FileChannel fileChannel = fileOutputStream.getChannel();
  9. //创建一个缓冲区 ByteBuffer
  10. ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
  11. //将 str 放入 byteBuffer
  12. byteBuffer.put(str.getBytes());
  13. //对byteBuffer 进行flip
  14. byteBuffer.flip();
  15. //将byteBuffer 数据写入到 fileChannel
  16. fileChannel.write(byteBuffer);
  17. fileOutputStream.close();
  18. }
  19. //本地文件读数据
  20. public static void main(String[] args) throws Exception {
  21. //创建文件的输入流
  22. File file = new File("d:\\file01.txt");
  23. FileInputStream fileInputStream = new FileInputStream(file);
  24. //通过fileInputStream 获取对应的FileChannel -> 实际类型 FileChannelImpl
  25. FileChannel fileChannel = fileInputStream.getChannel();
  26. //创建缓冲区
  27. ByteBuffer byteBuffer = ByteBuffer.allocate((int) file.length());
  28. //将 通道的数据读入到Buffer
  29. fileChannel.read(byteBuffer);
  30. //将byteBuffer 的 字节数据 转成String
  31. System.out.println(new String(byteBuffer.array()));
  32. fileInputStream.close();
  33. }

一个Buffer读写文件

image.jpeg

  1. //使用一个Buffer完成文件读取
  2. public static void main(String[] args) throws Exception {
  3. FileInputStream fileInputStream = new FileInputStream("1.txt");
  4. FileChannel fileChannel01 = fileInputStream.getChannel();
  5. FileOutputStream fileOutputStream = new FileOutputStream("2.txt");
  6. FileChannel fileChannel02 = fileOutputStream.getChannel();
  7. ByteBuffer byteBuffer = ByteBuffer.allocate(512);
  8. while (true) { //循环读取
  9. //这里有一个重要的操作,一定不要忘了
  10. /*
  11. public final Buffer clear() {
  12. position = 0;
  13. limit = capacity;
  14. mark = -1;
  15. return this;
  16. }
  17. */
  18. byteBuffer.clear(); //清空buffer
  19. int read = fileChannel01.read(byteBuffer);
  20. System.out.println("read =" + read);
  21. if(read == -1) { //表示读完
  22. break;
  23. }
  24. //将buffer 中的数据写入到 fileChannel02 -- 2.txt
  25. byteBuffer.flip();
  26. fileChannel02.write(byteBuffer);
  27. }
  28. //关闭相关的流
  29. fileInputStream.close();
  30. fileOutputStream.close();
  31. }

拷贝文件

transferFrom

  1. //拷贝文件transferFrom 方法
  2. public static void main(String[] args) throws Exception {
  3. //创建相关流
  4. FileInputStream fileInputStream = new FileInputStream("d:\\a.jpg");
  5. FileOutputStream fileOutputStream = new FileOutputStream("d:\\a2.jpg");
  6. //获取各个流对应的filechannel
  7. FileChannel sourceCh = fileInputStream.getChannel();
  8. FileChannel destCh = fileOutputStream.getChannel();
  9. //使用transferForm完成拷贝
  10. destCh.transferFrom(sourceCh,0,sourceCh.size());
  11. //关闭相关通道和流
  12. sourceCh.close();
  13. destCh.close();
  14. fileInputStream.close();
  15. fileOutputStream.close();
  16. }

transferTo

超过 2g 大小的文件传输

  1. public class TestFileChannelTransferTo {
  2. public static void main(String[] args) {
  3. try (
  4. FileChannel from = new FileInputStream("data.txt").getChannel();
  5. FileChannel to = new FileOutputStream("to.txt").getChannel();
  6. ) {
  7. // 效率高,底层会利用操作系统的零拷贝进行优化, 2g 数据
  8. long size = from.size();
  9. // left 变量代表还剩余多少字节
  10. for (long left = size; left > 0; ) {
  11. System.out.println("position:" + (size - left) + " left:" + left);
  12. left -= from.transferTo((size - left), left, to);
  13. }
  14. } catch (IOException e) {
  15. e.printStackTrace();
  16. }
  17. }
  18. }