当前位置:   article > 正文

Java NIO知识整理_java中的nio知识体系

java中的nio知识体系

前几天学了下NIO这块,因之前基本没用到过也算是新知识,这篇文章着重分享Channel,内存映射,缓冲区不会做过多介绍,有兴趣可以百度一下找资料看

一   使用通道边读边写的经典写法

以复制图片到同一个目录为例,把wp.jpg复制一份放到d盘下

  1. package com.debug;
  2. import java.io.*;
  3. import java.nio.ByteBuffer;
  4. import java.nio.channels.FileChannel;
  5. public class UseChanel01 {
  6. public static void main(String[] args) throws Exception {
  7. File f1=new File("d:"+File.separator+"wp.jpg");
  8. File f2=new File("d:"+File.separator+"wp01.jpg");
  9. FileInputStream in=new FileInputStream(f1);
  10. FileOutputStream out=new FileOutputStream(f2);
  11. //取得输入输出流的通道
  12. FileChannel inChanel=in.getChannel();
  13. FileChannel outChanel=out.getChannel();
  14. //开辟缓冲
  15. ByteBuffer buf=ByteBuffer.allocate(1024);
  16. while(inChanel.read(buf)!=-1) {
  17. buf.flip();//重设缓冲区
  18. outChanel.write(buf);//输出到缓冲区
  19. buf.clear();//清空缓冲区
  20. }
  21. outChanel.close();
  22. inChanel.close();
  23. out.close();
  24. in.close();
  25. }
  26. }

二  NIO2的写法

NIO2之后简化了获取Channel的方法,不需要通过InputStream和OutoutStream获取,代码如下

  1. package com.debug;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.nio.ByteBuffer;
  5. import java.nio.MappedByteBuffer;
  6. import java.nio.channels.FileChannel;
  7. import java.nio.channels.FileChannel.MapMode;
  8. import java.nio.file.Paths;
  9. import java.nio.file.StandardOpenOption;
  10. public class UseChannel02 {
  11. public static void main(String[] args) throws Exception {
  12. //NIO2 取得channel(不需要通过输入输出流取得)
  13. FileChannel inChaeenl=FileChannel.open(Paths.get("d:",File.separator,"wp.jpg"), StandardOpenOption.READ);
  14. FileChannel outChaeenl=FileChannel.open(Paths.get("d:",File.separator,"wp03.jpg"),StandardOpenOption.READ, StandardOpenOption.WRITE,StandardOpenOption.CREATE);
  15. //使用内存映射的方式'边读边写'
  16. MappedByteBuffer mapByteInBuffer=inChaeenl.map(MapMode.READ_ONLY, 0, inChaeenl.size());
  17. MappedByteBuffer mapByteOutBuffer=outChaeenl.map(MapMode.READ_WRITE, 0, inChaeenl.size());
  18. byte[] dst=new byte[mapByteInBuffer.limit()];
  19. mapByteInBuffer.get(dst);
  20. mapByteOutBuffer.put(dst);
  21. outChaeenl.close();
  22. inChaeenl.close();
  23. }
  24. }

三  除使用内存映射的方式还有其他使用起来更简洁的API

  1. package com.debug;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.nio.ByteBuffer;
  5. import java.nio.MappedByteBuffer;
  6. import java.nio.channels.FileChannel;
  7. import java.nio.channels.FileChannel.MapMode;
  8. import java.nio.file.Paths;
  9. import java.nio.file.StandardOpenOption;
  10. public class UseChannel03 {
  11. public static void main(String[] args) throws Exception {
  12. //NIO2 取得channel
  13. FileChannel inChaeenl=FileChannel.open(Paths.get("d:",File.separator,"wp.jpg"), StandardOpenOption.READ);
  14. FileChannel outChaeenl=FileChannel.open(Paths.get("d:",File.separator,"wp04.jpg"),StandardOpenOption.READ, StandardOpenOption.WRITE,StandardOpenOption.CREATE);
  15. //使用transferFrom或者transferTo进行边读边写(最简洁的方式)
  16. outChaeenl.transferFrom(inChaeenl, 0, inChaeenl.size());
  17. outChaeenl.close();
  18. inChaeenl.close();
  19. }
  20. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/781111
推荐阅读
相关标签
  

闽ICP备14008679号