JAVA NIO(三) 三种文件的复制方法与效率对比
2018-03-24 11:21
726 查看
文件的复制最能体现io效率了.因为既需要读取数据还需要写出到硬盘中,下面提供了三种文件复制的方法 可以对比一下 直接缓冲区与非直接缓冲区的效率对比.
控制台输出:
nioCopyTest1耗费时间:1417
nioCopyTest2耗费时间:142
nioCopyTest3耗费时间:68
直接缓冲不需要文件的复制拷贝 所以大大增加效率 其中第三种方法通道之间的文件传输 速度最快了直接在通道中完成.
public class Nio { public static void main(String[] args) throws IOException { nioCopyTest1(); nioCopyTest2(); nioCopyTest3(); } /**通道之间的数据传输(直接缓冲区的模式) * */ private static void nioCopyTest3() throws IOException { long startTime = System.currentTimeMillis(); FileChannel inChannel = FileChannel.open(Paths.get("E:\\ 1.avi"), StandardOpenOption.READ); FileChannel outChennel = FileChannel.open(Paths.get("E:\\ 13.avi"),StandardOpenOption.WRITE,StandardOpenOption.READ,StandardOpenOption.CREATE_NEW); outChennel.transferFrom(inChannel,0,inChannel.size()); long end = System.currentTimeMillis(); System.out.println("nioCopyTest3耗费时间:"+(end-startTime)); } /** * 使用直接缓冲区完成文件的复制(内存映射文件) */ private static void nioCopyTest2() throws IOException { long startTime = System.currentTimeMillis(); FileChannel inChannel = FileChannel.open(Paths.get("E:\\ 1.avi"), StandardOpenOption.READ); FileChannel outChennel = FileChannel.open(Paths.get("E:\\ 12.avi"),StandardOpenOption.WRITE,StandardOpenOption.READ,StandardOpenOption.CREATE_NEW); //内存映射文件(什么模式 从哪开始 到哪结束) MappedByteBuffer inMappeBuf = inChannel.map(FileChannel.MapMode.READ_ONLY,0,inChannel.size()); MappedByteBuffer outMappeBuf = outChennel.map(FileChannel.MapMode.READ_WRITE,0,inChannel.size()); //直接都缓冲区进行数据的读写操作 byte[] dst = new byte[inMappeBuf.limit()]; inMappeBuf.get(dst); outMappeBuf.put(dst); inChannel.close(); outChennel.close(); long end = System.currentTimeMillis(); System.out.println("nioCopyTest2耗费时间:"+(end-startTime)); } /** * 非直接缓冲区 文件的复制 * @throws IOException */ private static void nioCopyTest1()throws IOException { long startTime = System.currentTimeMillis(); FileInputStream fileInputStream = new FileInputStream(new File("E:\\ 1.avi")); FileOutputStream fileOutputStream = new FileOutputStream("E:\\ 11.avi"); //获取通道 FileChannel inChannel = fileInputStream.getChannel(); FileChannel outChanel = fileOutputStream.getChannel(); //分配缓冲区的大小 ByteBuffer buf = ByteBuffer.allocate(1024); //将通道中的数据存入缓冲区 while (inChannel.read(buf) != -1) { buf.flip();//切换读取数据的模式 outChanel.write(buf); buf.clear(); } outChanel.close(); inChannel.close(); fileInputStream.close(); fileOutputStream.close(); long end = System.currentTimeMillis(); System.out.println("nioCopyTest1耗费时间:"+(end-startTime)); } }
控制台输出:
nioCopyTest1耗费时间:1417
nioCopyTest2耗费时间:142
nioCopyTest3耗费时间:68
直接缓冲不需要文件的复制拷贝 所以大大增加效率 其中第三种方法通道之间的文件传输 速度最快了直接在通道中完成.
相关文章推荐
- java的三种写文件方法的效率性能比较
- java io 复制文件与 nio 复制文件的效率问题
- Java NIO——与传统IO文件复制的效率比较(channel.transferTo、Files.copy 、channel.write)
- java数组复制的四种方法效率对比
- java中io与nio复制文件性能对比
- java的三种写文件方法的效率性能比较
- java中IO文件复制两种方法及对比
- java字节流复制文件的方法
- JAVA功能代码《12》----使用NIO快速复制文件
- Android中split,java.util.StringTokenizer和indexOf的使用以及各方法效率的对比
- 转载:Java 将一行字符串追加至文件末尾的三种方法
- Java List遍历方法 及其效率对比
- Java List遍历方法 及其效率对比
- java 3中方法复制一个文件
- java写文件的三种方法比较
- Java追加文件内容的三种方法 .
- Java实现文件的复制和新Nio包通道的运用--Thinking in java
- java复制文件的一种方法
- Android中split,java.util.StringTokenizer和indexOf方法效率的对比,不看不知道,一看下一跳
- java写的一个用字节流复制文件的方法