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

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. 2014 BDTC 參会有感

    中国大数据技术大会(Big Data Technology Conference,BDTC)是眼下国内最具影响.规模最大的大数据领域的技术盛会. 大会的前身是Hadoop中国云计算大会(Hadoop ...

  2. .net DataTable 正确排序姿势

    关于dataTable中根据列排序正确姿势做个随笔,方便查阅 System.Data.DataTable dt = new System.Data.DataTable(); dt.Columns.Ad ...

  3. gradient杂谈

    工作中难免遇到某些小项目中没有设计的情况,这对于PS基础薄弱的我来说非常恐怖.无奈之下,只好自己自学UI方面的知识,但对于某些能用CSS实现的背景样式等,还是尽可能地用已经掌握的知识去实现.本文主要分 ...

  4. GDI+创建Graphics对象的2种方式

      1.this.CreateGraphics()     // 调用控件的CreateGraphics()方法 2.在OnPaint事件中,PaintEventArgs类型的参数e对象的Graphi ...

  5. ios 调用相机后 view 下沉问题

    我只加了一句代码 现在不报错了  因为这个问题是随机性的  我也不太明白这个地方是怎么回事   我只是这样子做了  问题不出来了 if ([[[UIDevice currentDevice] syst ...

  6. CGFloat和float

    CGFloat :在mac上自适应,在64位的系统,会变宽,32位会变窄,手机没变化float:没有变化

  7. C++中const

    [const] 0.普通const对象定义在栈空间中 { ; ; cout << &a << ' ' << &b; } Result: 0x22ab ...

  8. Ubunu下安装mongoDB

    mongoDB有两种安装模式: 1. 源码安装 wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.2.0.tgz tar zxvf ...

  9. Tempter of the Bone(dfs+奇偶剪枝)

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  10. python对真假的判断方式

    一.如下是以下值就认为是假 1.None-->None值 2.False-->False值 3.0-->数值零不管它是int,float还是complex类型 4.'',(),[]- ...