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读取 ...
随机推荐
- angularjs应用骨架(2)
时隔一个星期,接着上一篇的angularjs应用骨架继续聊聊angularjs其他的其他的内容. 区分UI和控制器的职责 在应用控制器中有三种职责: 1.为应用中模型设置初始状态 2.通过$scope ...
- php多线程即时通讯
即时通讯:推送消息http://www.workerman.net/
- 配置并学习微信JS-SDK(2)—图片接口
测试地址:http://www.qq210.com/shoutu/android 检查图像接口 选择本地或拍照的图片 上传选择的图片 预览上传的图片 //1.检查图像接口 document.query ...
- mysql 时间戳与日期格式的相互转换
1.UNIX时间戳转换为日期用函数: FROM_UNIXTIME() ); 输出:2006-08-22 12:11:10 2.日期转换为UNIX时间戳用函数: UNIX_TIMESTAMP() Sel ...
- yii下多条件多表组合查询以及自写ajax分页
多条件组合查询主要用到yii的CDbCriteria,这个类很多oem框架都有,非常好用. 前台表单 前台查询表单效果是这样的,多个条件组,每个组里放多个input,name为数组.当任何一个复选框被 ...
- 监视/etc/passwd文件是否正常
帮助监视/etc/passwd文件是否正常(P90 练习6.7) 1)找出有UID0的所有项 2)找出有重复UID的所有项 3)找出有重复登录名的所有项 4)找出没有口令的所有项 5)找出没有作废日期 ...
- 配置SHH集群
==特别要注意当前用户的问题== 1. 修改路由信息 vi /etc/hosts 10.211.55.15 hmaster1 10.211.55.16 hmaster2 10.211.55.17 hs ...
- NorFlash
一.NorFlash概述 1.NorFlash Intel于1988年首先开发出NOR Flash 技术,彻底改变了原先由EPROM(Erasable Programmable Read-Only-M ...
- Warning: Invalid argument supplied for foreach()
经常对提交过来的数据进行双重循环,但是为空时会报错:Warning: Invalid argument supplied for foreach() 如下解决即可:foreach($data[$i] ...
- Windows命令行下pip安装python whl包
因为做网页爬虫,需要用到一个爬新闻的BeautifulSoup 的包,然后再关网上下的是whl包,第一次装,虽然花了点时间,最后还是装上去了,记录一下,方便下次. 先发一下官方文档地址.http:// ...