BufferedInputStream,FileInputStream,FileChannel实现文件拷贝
从上篇文章中知道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实现文件拷贝的更多相关文章
- 07 IO流(四)——文件字节流 FileInputStream/FileOutputStream与文件的拷贝
两个类的简述 专门用来对文件进行读写的类. 父类是InputStream.OutputStream 文件读入细节 FileOutputStream流的构造方法:new FileOutputStream ...
- 文件拷贝, 使用 BIO,NIO的对比,四种写法性能分析。
测试环境: jdk 1.7 + 2G内存 测试代码基本上复制了: http://blog.csdn.net/tabactivity/article/details/9317143 1 2 3 4 5 ...
- Java IO和Java NIO在文件拷贝上的性能差异分析
1. 在JAVA传统的IO系统中,读取磁盘文件数据的过程如下: 以FileInputStream类为例,该类有一个read(byte b[])方法,byte b[]是我们要存储读取到用户空间的缓冲区 ...
- Java实现文件拷贝的4种方法.
原文地址:http://blog.csdn.net/ta8210/article/details/2073817 使用 java 进行文件拷贝 相信很多人都会用,,不过效率上是否最好呢? 最近看了看N ...
- Java IO和Java NIO 和通道 在文件拷贝上的性能差异分析
1. 在JAVA传统的IO系统中,读取磁盘文件数据的过程如下: 以FileInputStream类为例,该类有一个read(byte b[])方法,byte b[]是我们要存储读取到用户空间的缓冲区 ...
- Java通过NIO实现快速文件拷贝的代码
将内容过程重要的内容片段做个记录,下面的内容段是关于Java通过NIO实现快速文件拷贝的内容. public static void fileCopy( File in, File out ) thr ...
- 38、使用IO流进行文件拷贝
使用IO流进行文件拷贝 需求:在项目的根目录里面创建一个java.txt的文件,然后将这个文件拷贝到file文件夹里面并且重命名为good.txt文件先以流的方式将java.txt文件读取到内存中,然 ...
- BIO与NIO的方式实现文件拷贝
面试题 - 编程实现文件拷贝.(这个题目在笔试的时候经常出现,下面的代码给出了两种实现方案) import java.io.FileInputStream; import java.io.FileOu ...
- Java文件拷贝方式
原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11444284.html 利用java.io类库,直接为源文件构建一个FileInputStream读取 ...
随机推荐
- jquery返回顶部-ie6配合css表达式。
css: .wraper{ width:980px; height:2048px; margin:0 auto; background:#ccc; } /*返回头部*/ #backToTop a{;; ...
- phpcms v9使用GET调用指定id文章内容、页面数据方法
不知道大家有没有相同的体会?在使用Phpcms V9建站调用数据的时候,基础的数据用默认的模板的调用语句就好了,但复杂不常见的数据怎么调用呢?我们技术可能会研究半天,怀着探索的精神不断尝试.孜孜不倦. ...
- python for list generate content
content = [ii for ii in range(50)] This can generate a list content
- Docker Machine
Docker Machine http://dockone.io/article/1485?utm_source=tuicool&utm_medium=referral 本地安装与使用 Doc ...
- MFC 之ActiveX控件学习
本文将介绍ActiveX控件的应用与工作原理,读者可以把ActiveX控件看成一个极小服务器的应用程序,它不能独立运行,必须要嵌入到容器程序中与容器一起运行,就像电脑主机中的显卡,它自己在电脑硬件系统 ...
- this compilation unit is not on the build path of a java project
在eclipse中新建maven project后,会自动生成main\test目录结构,新建一个测试类,然后编辑类文件时,总是提示错误:this compilation unit is not on ...
- 李洪强漫谈iOS开发[C语言-033]-三元运算符的应用
- c++复习基础要点02 虚函数与模板 与static inline是否共存
1. 虚函数能否定义为模板函数 当一个类有虚函数时,它一定有一个虚表,用来纪录每个虚函数的实际地址.这也就是说这个虚表的大小是在编译期就确定了的.有多少个虚函数,虚表就纪录几个. ...
- css的定位机制
牛腩新闻发不系统中遇到了CSS(Cascading style sheets),第一次接触,比较陌生还!因为CSS很多关于元素定位的问题,并且很多情况下元素的位置以像素精度计.一个不小心就很头疼,为此 ...
- linux 设置命令行属性,背景色,前景色等
我的博客:www.while0.com 主要是命令setterm.