几种基于javaI/O的文件拷贝操作比较
最近公司的项目用到文件拷贝,由于涉及到的大量大文件的拷贝工作,代码性能问题显得尤为重要,所以写了以下例子对几种文件拷贝操作做一比较:
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的文件拷贝操作比较的更多相关文章
- (java)从零开始之--异常处理(以文件拷贝为例)
开发过程中避免不了对异常的处理,但是异常的处理又不能乱throw 下面是简单的抛异常处理 public static void CopyFile(String souFile,String dirFi ...
- Linux文件拷贝(6)
本篇介绍文件拷贝操作,主要讲两个命令: 命令 对应英文 作用 tree[目录名] tree 以树状图列出文件目录结构 cp 源文件 目标文件 copy 复制文件或者目录 tree tree命令可以将一 ...
- Java实现文件拷贝的4种方法.
原文地址:http://blog.csdn.net/ta8210/article/details/2073817 使用 java 进行文件拷贝 相信很多人都会用,,不过效率上是否最好呢? 最近看了看N ...
- java中把文件拷贝到指定目录下最简单几种方法
java中把文件拷贝到指定目录下最简单几种方法 String savePath = "D:/file";// 文件保存到d盘的file目录下 File savefile = n ...
- 总结java中文件拷贝剪切的5种方式-JAVA IO基础总结第五篇
本文是Java IO总结系列篇的第5篇,前篇的访问地址如下: 总结java中创建并写文件的5种方式-JAVA IO基础总结第一篇 总结java从文件中读取数据的6种方法-JAVA IO基础总结第二篇 ...
- 【Java面试】Java有几种文件拷贝方式,哪一种效率最高?
"Java有几种文件拷贝方式,哪一种效率最高?" 这个问题是京东一面的时候,针对4年经验的同学的一个面试题. 大家好,我是Mic,一个工作了14年的Java程序员. 关于这个问题的 ...
- 文件拷贝, 使用 BIO,NIO的对比,四种写法性能分析。
测试环境: jdk 1.7 + 2G内存 测试代码基本上复制了: http://blog.csdn.net/tabactivity/article/details/9317143 1 2 3 4 5 ...
- 基于gSOAP使用头文件的C语言版web service开发过程例子
基于gSOAP使用头文件的C语言版web service开发过程例子 一服务端 1 打开VS2005,创建一个工程,命名为calcServer. 2 添加一个头文件calc.h,编辑内容如下: 1// ...
- 基于 Struts2 的单文件和多文件上传
文件的上传下载是 Web 开发中老生常谈的功能,基于 Struts2 框架对于实现这一功能,更是能够给我们带来很多的便利.Struts2 已经有默认的 upload 拦截器.我们只需要写参数,它就会自 ...
随机推荐
- hdu 1728 逃离迷宫(dFS+优先队列)
求转弯最少的走路方式!!!! #include<stdio.h> #include<string.h> #include<queue> using namespac ...
- 打印log 保存log
using UnityEngine; using System.Collections; using System.IO; using System; using System.Text; names ...
- Coreseek:常见的问题2
1.failed to lock XXXXX.spl档 这是当你构建的指数将是一个问题,您不必打开searchd服务关闭,既然你开searchd维修,他将建立呼叫xxx.spl临时文件,施工时的指数会 ...
- poj1562--Oil Deposits
Description The GeoSurvComp geologic survey company is responsible for detecting underground oil dep ...
- EffectiveC#17--装箱和拆箱的最小化
1.如下这段代码会经历装箱和拆箱.例如25会先装箱成object后传递给writeline方法(一次拷贝),在方法内部又 经历拆箱成int(第二次拷贝)后然后调用tostring(). Console ...
- django的Model 模型中常用的字段类型
常用的字段类型: AutoField:自增长字段,通常不用,如果未在Model中显示指定主键,django会默认建立一个整型的自增长主键字段 BooleanField:布尔型,值为True或False ...
- web跳转到自己的app
做个笔记 原文:http://blog.csdn.net/ba_jie/article/details/6884818 iPhone SDK可以把你的App和一个自定义的URL Scheme绑定.该U ...
- _ConnectionPtr.CreateInstance(__uuidof(Connection))“不支持此接口”错误解决
最近在换了win7 64位的系统,今天突然发现以前写的ADO连接数据库的代码编译后在windows2003下会执行到: _ConnectionPtr.CreateInstance(__uuidof(C ...
- CDZSC_2015寒假新人(2)——数学 H
H - H Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status ...
- IE 与 FireFox 的 event 详解 (转)
原文链接 FF的FIREBUG,不仅能测试JS还能检查CSS错误,是一般常用的. 但它主要检查FF方面的错误,对IE就无能为力了. 要测试IE,就用ieTester,它可以测试IE几乎所有版本(1.0 ...