从上篇文章中知道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实现文件拷贝的更多相关文章

  1. 07 IO流(四)——文件字节流 FileInputStream/FileOutputStream与文件的拷贝

    两个类的简述 专门用来对文件进行读写的类. 父类是InputStream.OutputStream 文件读入细节 FileOutputStream流的构造方法:new FileOutputStream ...

  2. 文件拷贝, 使用 BIO,NIO的对比,四种写法性能分析。

    测试环境: jdk 1.7 +  2G内存 测试代码基本上复制了: http://blog.csdn.net/tabactivity/article/details/9317143 1 2 3 4 5 ...

  3. Java IO和Java NIO在文件拷贝上的性能差异分析

    1.  在JAVA传统的IO系统中,读取磁盘文件数据的过程如下: 以FileInputStream类为例,该类有一个read(byte b[])方法,byte b[]是我们要存储读取到用户空间的缓冲区 ...

  4. Java实现文件拷贝的4种方法.

    原文地址:http://blog.csdn.net/ta8210/article/details/2073817 使用 java 进行文件拷贝 相信很多人都会用,,不过效率上是否最好呢? 最近看了看N ...

  5. Java IO和Java NIO 和通道 在文件拷贝上的性能差异分析

    1.  在JAVA传统的IO系统中,读取磁盘文件数据的过程如下: 以FileInputStream类为例,该类有一个read(byte b[])方法,byte b[]是我们要存储读取到用户空间的缓冲区 ...

  6. Java通过NIO实现快速文件拷贝的代码

    将内容过程重要的内容片段做个记录,下面的内容段是关于Java通过NIO实现快速文件拷贝的内容. public static void fileCopy( File in, File out ) thr ...

  7. 38、使用IO流进行文件拷贝

    使用IO流进行文件拷贝 需求:在项目的根目录里面创建一个java.txt的文件,然后将这个文件拷贝到file文件夹里面并且重命名为good.txt文件先以流的方式将java.txt文件读取到内存中,然 ...

  8. BIO与NIO的方式实现文件拷贝

    面试题 - 编程实现文件拷贝.(这个题目在笔试的时候经常出现,下面的代码给出了两种实现方案) import java.io.FileInputStream; import java.io.FileOu ...

  9. Java文件拷贝方式

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11444284.html 利用java.io类库,直接为源文件构建一个FileInputStream读取 ...

随机推荐

  1. Codeforces 553C Love Triangles(图论)

    Solution: 比较好的图论的题. 要做这一题,首先要分析love关系和hate关系中,love关系具有传递性.更关键的一点,hate关系是不能成奇环的. 看到没有奇环很自然想到二分图的特性. 那 ...

  2. USACO 2.2 Subset Sums 集合(subset)

    Description 对于从1到N的连续整集合,能划分成两个子集合,且保证每个集合的数字和是相等的.举个例子,如果N=3,对于{1,2,3}能划分成两个子集合,他们每个的所有数字和是相等的: {3} ...

  3. Jquery 和 Js

     jQuery  [一]  jQuery     1: javaScript和 javaScript库:        (1) javaScript自身存在的3个弊端:复杂的文档对象模型(DOM),不 ...

  4. PHP文件上传与安全

    文件上传的流程 上传必须由POST方式的file类型表单提交,被提交的地方 一定是一个php程序,用户在表单使用file类型的域.选在一个自己电脑上的文件,提交到php程序以后 其实就已经完成了一个上 ...

  5. #Leet Code# LRU Cache

    语言:C++ 描述:使用单链表实现,HeadNode是key=-1,value=-1,next=NULL的结点.距离HeadNode近的结点是使用频度最小的Node. struct Node { in ...

  6. WPF之application对象

    WPF:Application简介 Application是一个地址空间,在WPF中应用程序就是在System.Windows命名空间下的一个Application实例.一个应用程序只能对应一个App ...

  7. python判断一个数字是整数还是浮点数

    在网上闲逛,发现了一个python的用法

  8. linux下配置NFS服务器

    (声明:本文大部分文字摘自Linux NFS服务器的安装与配置) 一.NFS简介     NFS 是Network File System的缩写,即网络文件系统.一种使用于分散式文件系统的协定,由Su ...

  9. (转载)KL距离,Kullback-Leibler Divergence

    转自:KL距离,Kullback-Leibler Divergence   KL距离,是Kullback-Leibler差异(Kullback-Leibler Divergence)的简称,也叫做相对 ...

  10. 写个简单的ANT脚本来编译项目

    <?xml version="1.0" encoding="GBK"?> <project name="j2ee project&q ...