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

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. VB中DateDiff 函数解释

    VB中DateDiff 函数使用方法 DateDiff (interval, Date1 , Date2[,firstweekofyear[,firstweekofyear]])  返回一个Varia ...

  2. 观察者模式与Boost.Signals

      1)  观察者模式定义 略,各种设计模式的书上都有定义. 2)  观察者模式一般实现 观察者模式一般实现,都是“被观察者”保存一个“观察者”的列表,循环这个列表来通知“观察者”.代码,其中使用了b ...

  3. Linux远程登录

    Linux远程登录 远程登录 关闭linux的防火墙 /etc/init.d/iptables stop 启动VNC服务器 Vncserver & 然后记住desktop is localho ...

  4. HTML5+CSS3项目总结

      经过一个月的学习,我基本掌握了HTML5的一些标签的用法和特性,以及一些CSS3的属性的特点和用法. 在本周安排的为期四天的第一阶段的课程的项目实训中,我基本能够熟练运用学到的知识,完成页面的速度 ...

  5. Android Service(上)

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/11952435 相信大多数朋友对Service这个名词都不会陌生,没错,一个老练的A ...

  6. react基于webpack和babel以及es6的项目搭建

    项目demo地址https://github.com/aushion/webpack_reac_config 1.打开命令提示窗口,输入 mkdir react_test cd react_test ...

  7. poj 2112 floyd+Dinic最大流+二分最小值

    题目大意是: K台挤奶机器,C头牛,K不超过30,C不超过200,每台挤奶机器最多可以为M台牛工作,给出这些牛和机器之间,牛和牛之间,机器与机器之间的距离,在保证让最多的牛都有机器挤奶的情况下,给出其 ...

  8. [转]PictureEx.h和PictureEx.cpp源文件

    要显示一个gif,网上找了个,子类化了MFCl图片控件,用着方便,记一下 转自:http://www.bccn.net/Article/net/vcnet/jszl/200709/6386.html ...

  9. css布局学习笔记之max-width

    设置块级元素的 width 可以阻止它从左到右撑满容器.然后你就可以设置左右外边距为 auto 来使其水平居中.元素会占据你所指定的宽度,然后剩余的宽度会一分为二成为左右外边距. div{ width ...

  10. WPF安装部署小结

    开机启动 右击"MySetup">>"视图">>"注册表",在"HKEY_LOCAL-MACHINE&qu ...