从上篇文章中知道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. js学习--DOM操作详解大全 前奏(认识DOM)

    一 . 节点属性 DOM 是树型结构,相应的,可以通过一些节点属性来遍历节点树: 方法 说明 nodeName 节点名称,相当于tagName.属性节点返回属性名,文本节点返回#text.nodeNa ...

  2. phpmyadmin数据导入最大限制的解决方法

    mysql导入文件最大限制更改解决方法:phpmyadmin库导入出错:You probably tried to upload too large file. Please refer to doc ...

  3. 12个Icon图标资源网站

    1.除了Icon以外,还有很多不错的UI设计素材. 地址:http://worldui.com/2.除了免费Icon资源下载以外,还提供Icon定制的付费服务.地址:http://dryicons.c ...

  4. Python: 设计模式 之 工厂模式例(1)

    #!/usr/bin/env python #coding=utf-8 # # 工厂模式一例 # 版权所有 2014 yao_yu (http://blog.csdn.net/yao_yu_126) ...

  5. WPFDispatcher示例

    Dispatcher 类提供用于管理线程工作项队列的服务. 效果演示: <Window x:Class="WPF之Dispatcher对象.MainWindow" xmlns ...

  6. 从文章"避免复制与粘贴"到文章"Extract Method"的反思(2)

    好了.在上一篇里面讲了讲怎么把临时变量应该从函数里面剔除去.这个过程叫做从临时变量变成查询 那么接下来我们聊聊把代码提炼成函数,有叫做用函数对象取代函数 那么,问题来了:在函数中什么样的代码是需要被提 ...

  7. 【C语言】重定向和文件

    重定向和文件 一.相关基础知识 重定向:在计算机领域,重定向是大多数命令行解释器所具有的功能,包括各种可以将标准流重定向用户规定地点的Unix shells. 输入重定向:可以使程序能够使用文件代替键 ...

  8. const变量与define定义常量的区别

    一.概念性区别 const 变量就是在普通变量前边加上一个关键字const,它赋值的唯一机会就是“定义时”,此变量不能被程序修改,存储在rodata区. define定义的是常量,不是变量,所以编译器 ...

  9. 使用OPCDAAuto.dll编写C# OPC采集程序

    在一台新机器上运行使用OPC自动化接口编写的C#程序报错如下: 索 COM 类工厂中 CLSID 为 {28E68F9A-8D75-11D1-8DC3-3C302A000000} 的组件失败,原因是出 ...

  10. 实现pushViewController:animated:的不同页面转换特效

    1. 首先要明确的是,不使用pushViewController的默认动画,所以在调用这个函数时,要将animated设置为NO.2. 使用普通的来CATransition实现转换效果,代码如下:CA ...