最近公司的项目用到文件拷贝,由于涉及到的大量大文件的拷贝工作,代码性能问题显得尤为重要,所以写了以下例子对几种文件拷贝操作做一比较:

0、文件拷贝测试方法

 public static void fileCopy(String source, String target,int type) {
Date start = new Date();
File in = null;
File out = null;
FileInputStream fis = null;
FileOutputStream fos = null;
try {
in = new File(source);
out = new File(target);
switch (type) {
case 1:
copyer1(in, out, fis, fos);
break;
case 2:
copyer2(in, out, fis, fos);
break;
case 3:
copyer3(in, out, fis, fos);
break;
case 4:
copyer4(in, out, fis, fos);
break;
default:
break;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Date end = new Date();
System.out.println("方法"+type+"用时:"+(end.getTime() - start.getTime())+" ms!");
}
}

方式一:一次读取全部数据

     /**
* 一次全部读取文件内容
*/
@SuppressWarnings("resource")
public static void copyer1(File in,File out,FileInputStream fis,FileOutputStream fos) throws IOException{
fis = new FileInputStream(in);
int size = fis.available();
byte[] buffer = new byte[size];
fis.read(buffer);
fos = new FileOutputStream(out);
fos.write(buffer);
fos.flush();
}

方式二:每次读入固定字节的数据

     /**
* 每次读取固定字节的数据
*/
@SuppressWarnings("resource")
public static void copyer2(File in,File out,FileInputStream fis,FileOutputStream fos) throws IOException{ fis = new FileInputStream(in);
fos = new FileOutputStream(out);
byte[] buffer = new byte[1024];
while (fis.read(buffer) != -1) {
fos.write(buffer);
}
fos.flush();
}

方式三:每次读取一行数据,适合按行解析数据的场景

     /**
* 每次读取一行数据
*/
@SuppressWarnings("resource")
public static void copyer3(File in,File out,FileInputStream fis,FileOutputStream fos) throws IOException{ fis = new FileInputStream(in);
fos = new FileOutputStream(out);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String line = null;
while ((line = br.readLine()) != null) {
fos.write(line.getBytes());
}
fos.flush();
}

方式四:每次读取一个字符,~_~,想想都累

     /**
* 每次读取一个字节
*/
@SuppressWarnings("resource")
public static void copyer4(File in,File out,FileInputStream fis,FileOutputStream fos) throws IOException{ fis = new FileInputStream(in);
fos = new FileOutputStream(out);
int i = 0;
while ((i = fis.read()) != -1) {
fos.write(i);
}
fos.flush();
}
}

最后:测试用main函数

     public static void main(String[] args) {
String source = "e:\\in.txt";
String target = "e:\\out.txt";
for (int i = 1; i < 5; i++) {
fileCopy(source, target, i);
}
}

测试文件:

运行结果:

几种基于javaI/O的文件拷贝操作比较的更多相关文章

  1. (java)从零开始之--异常处理(以文件拷贝为例)

    开发过程中避免不了对异常的处理,但是异常的处理又不能乱throw 下面是简单的抛异常处理 public static void CopyFile(String souFile,String dirFi ...

  2. Linux文件拷贝(6)

    本篇介绍文件拷贝操作,主要讲两个命令: 命令 对应英文 作用 tree[目录名] tree 以树状图列出文件目录结构 cp 源文件 目标文件 copy 复制文件或者目录 tree tree命令可以将一 ...

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

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

  4. java中把文件拷贝到指定目录下最简单几种方法

    java中把文件拷贝到指定目录下最简单几种方法   String savePath = "D:/file";// 文件保存到d盘的file目录下 File savefile = n ...

  5. 总结java中文件拷贝剪切的5种方式-JAVA IO基础总结第五篇

    本文是Java IO总结系列篇的第5篇,前篇的访问地址如下: 总结java中创建并写文件的5种方式-JAVA IO基础总结第一篇 总结java从文件中读取数据的6种方法-JAVA IO基础总结第二篇 ...

  6. 【Java面试】Java有几种文件拷贝方式,哪一种效率最高?

    "Java有几种文件拷贝方式,哪一种效率最高?" 这个问题是京东一面的时候,针对4年经验的同学的一个面试题. 大家好,我是Mic,一个工作了14年的Java程序员. 关于这个问题的 ...

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

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

  8. 基于gSOAP使用头文件的C语言版web service开发过程例子

    基于gSOAP使用头文件的C语言版web service开发过程例子 一服务端 1 打开VS2005,创建一个工程,命名为calcServer. 2 添加一个头文件calc.h,编辑内容如下: 1// ...

  9. 基于 Struts2 的单文件和多文件上传

    文件的上传下载是 Web 开发中老生常谈的功能,基于 Struts2 框架对于实现这一功能,更是能够给我们带来很多的便利.Struts2 已经有默认的 upload 拦截器.我们只需要写参数,它就会自 ...

随机推荐

  1. [HDU 1535]Invitation Cards[SPFA反向思维]

    题意: (欧洲人自己写的题面就是不一样啊...各种吐槽...果断还是看晕了) 有向图, 有个源叫CCS, 求从CCS到其他所有点的最短路之和, 以及从其他所有点到CCS的最短路之和. 思路: 返回的时 ...

  2. linux下安装swftools工具

    swfTools是一种实用工具与Adobe Flash文件(swf文件)工作的集合.可以把(pdf/gif/png/jpeg/jpg/font/wav) 7种格式转换为swf文件.一般常用于文件在线浏 ...

  3. jquery selector

    jquery的选择器功能 1 :lt(index) selector 一组元素选择index之前的元素,若index<0 则倒着选过来 http://api.jquery.com/lt-sele ...

  4. Java Timer触发定时器

    XML: <!-- Java Timer定时 --> <!-- <bean id="shortUrlTask" class=" com.sprin ...

  5. MJExtension(JSON到数据模型的自动转换)

    整理自:http://www.jianshu.com/p/93c242452b9b. 1.MJExtension的功能 字典-->模型 模型-->字典 字典数组-->模型数组 模型数 ...

  6. 自定义带弹性效果的pageControl

    分三部分实现,在drawrect方法里画出灰色背景,根据pageCount创建对应个数的dotView放置在对应位置,并隐藏,创建一个CAShapeView类型的layer,根据scrollView的 ...

  7. Niagara AX之在Station下显示Home节点

    默认的Station下是没有Home节点的,那么,这个Home节点是怎么添加上去的呢? 注意Home后面的描述(Description):“Navigation tree defined by nav ...

  8. GitHub+hexo to Blog

    title: GitHub+hexo to Blog date: 2014-12-26 09:44:53 tags: hexo, github --- 摘要 一直想要一个自己的博客,不过一直怯于对网站 ...

  9. (原)mkl用到的函数

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5585301.html 计算 $C=\alpha *A*B+\beta *C$: void cblas_ ...

  10. (原)C++中测试代码执行时间

    转载请注明出处(不过这个用法网上到处都是): http://www.cnblogs.com/darkknightzh/p/4987738.html LARGE_INTEGER nFreq, nBegi ...