BufferedInputStream,FileInputStream,FileChannel实现文件拷贝
2014-08-27 10:04
429 查看
从上篇文章中知道BufferedInputStream是自带缓冲区的输入流,可以大大减少IO次数,提供效率。下面的例子中实现了用BufferedInputStream与FileInputStream实现20M文件的差异
<pre name="code" class="java">public class BufferedOutputStreamDemo { /** * 用BufferedInputStream, BufferedOutputStream实现文件拷贝 * @throws IOException */ @Test public void test1() throws IOException{ File originFile = new File("D:"+File.separator+"test"+File.separator+"bufferedStream_copy.txt"); File targetFile = new File("D:"+File.separator+"test"+File.separator+"copy"+File.separator+"bufferedStream_copy.txt"); targetFile.deleteOnExit(); targetFile.createNewFile(); InputStream inputStream = new FileInputStream(originFile); OutputStream outputStream = new FileOutputStream(targetFile); BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream); long length = originFile.length(); double size = length/1024/1024; int temp = 0; byte b[] = new byte[(int)originFile.length()] ; long startTime = System.nanoTime(); //此种方法拷贝20M文件耗时约1451ms /*while((temp =bufferedInputStream.read()) != -1){ bufferedOutputStream.write(temp); }*/ //此种方法拷贝20M文件耗时约146ms while(bufferedInputStream.read(b, 0, b.length) != -1){ bufferedOutputStream.write(b, 0, b.length); } long endTime = System.nanoTime(); System.out.println("copy大小为"+size+"M文件耗费时间:"+(endTime-startTime)/1000000+"ms"); bufferedInputStream.close(); //bufferedOutputStream.close(); } /** * 用FileInputStream和FileOutputStream实现文件拷贝 * @throws IOException */ @Test public void test2() throws IOException{ File originFile = new File("D:"+File.separator+"test"+File.separator+"bufferedStream_copy.txt"); File targetFile = new File("D:"+File.separator+"test"+File.separator+"copy"+File.separator+"bufferedStream_copy.txt"); targetFile.deleteOnExit(); targetFile.createNewFile(); FileInputStream inputStream = new FileInputStream(originFile); FileOutputStream outputStream = new FileOutputStream(targetFile); int temp = 0; long length = originFile.length(); byte[] byteBuffer = new byte[(int) length]; double size = length/1024/1024; long startTime = System.nanoTime(); //此种方法拷贝20M文件几分钟 /*while((temp =inputStream.read()) != -1){ outputStream.write(temp); }*/ while(inputStream.read(byteBuffer, 0, (int)length) != -1){ outputStream.write(byteBuffer, 0, (int)length); } long endTime = System.nanoTime(); System.out.println("copy大小为"+size+"M文件耗费时间:"+(endTime-startTime)/1000000+"ms"); inputStream.close(); outputStream.close(); } /** * 用FileChannel实现文件拷贝 * @throws IOException */ @Test public void test3() throws IOException{ File originFile = new File("D:"+File.separator+"test"+File.separator+"bufferedStream_copy.txt"); File targetFile = new File("D:"+File.separator+"test"+File.separator+"copy"+File.separator+"bufferedStream_copy.txt"); targetFile.deleteOnExit(); targetFile.createNewFile(); FileInputStream inputStream = new FileInputStream(originFile); FileOutputStream outputStream = new FileOutputStream(targetFile); FileChannel inputChannel = inputStream.getChannel(); FileChannel outputChannel = outputStream.getChannel(); long length = originFile.length(); double size = length/1024/1024; long startTime = System.nanoTime(); inputChannel.transferTo(0, length, outputChannel); long endTime = System.nanoTime(); System.out.println("copy大小为"+size+"M文件耗费时间:"+(endTime-startTime)/1000000+"ms"); inputStream.close(); outputStream.close(); } }
相关文章推荐
- BufferedInputStream,FileInputStream,FileChannel实现文件拷贝
- Java基础-FileInputStream、FileOutputStream、BufferedInputStream、BufferedOutputStream实现文件拷贝
- Java BufferedInputStream和BufferedOutputStream实现文件拷贝
- 利用缓冲流BufferedReader和BufferedWriter来实现文件拷贝功能 package com.zt; import java.io.BufferedReader; import j
- 利用C# 实现文件拷贝
- 用命名管道实现局域网上两台主机间的文件拷贝
- 在WIN95中实现文件拷贝的编程方法
- 在WIN95中实现文件拷贝的编程方法
- 怎样在PB中实现文件的拷贝与删除
- 在WIN95中实现文件拷贝的编程方法
- 目录下的文件拷贝,或者整个目录拷贝的实现
- 用c语言实现文件拷贝
- ISO C++实现文件拷贝
- 在WIN95中实现文件拷贝的编程方法
- 案例 1-3: 结合InputStream 和 OutputStream 进行文件内容拷贝
- 用java实现文件的拷贝
- 如何实现文件的拷贝
- 实现整个文件夹及单个文件的拷贝
- 实现整个文件夹及单个文件的拷贝
- Delphi中实现文件拷贝的三种方法