java IO 之文件的复制
2016-04-03 12:03
387 查看
最近本彩笔在学习java的IO,整理一下通过io对文件进行复制,所以写了一个工具类,以便以后使用。
package com.imooc.io;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* 文件拷贝的工具类
* @author I
*
*/
public class JavaIOCopy {
/**
* 文件拷贝,字节批量读取
* @param srcFile
* @param destFile
* @throws IOException
*/
public static void copyFile(File srcFile,File destFile)throws IOException{
if(!srcFile.exists()){
throw new IllegalArgumentException("文件:"+srcFile+"不存在");
}
if(!srcFile.isFile()){
throw new IllegalArgumentException(srcFile+"不是文件");
}
FileInputStream in = new FileInputStream(srcFile);
FileOutputStream out = new FileOutputStream(destFile);
byte[] buf = new byte[8*1024];
int b ;
while((b = in.read(buf))!=-1){
out.write(buf,0,b);
out.flush();
}
in.close();
out.close();
}
/**
* 进行文件的拷贝,利用带缓冲的字节流
* @param srcFile
* @param destFile
* @throws IOException
*/
public static void copyFileByBuffer(File srcFile,File destFile)throws IOException{
if(!srcFile.exists()){
throw new IllegalArgumentException("文件:"+srcFile+"不存在");
}
if(!srcFile.isFile()){
throw new IllegalArgumentException(srcFile+"不是文件");
}
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(srcFile));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(destFile));
int c ;
while((c = bis.read())!=-1){
bos.write(c);
bos.flush();
}
bis.close();
bos.close();
}
/**
* 单字节,不带缓冲进行文件拷贝
* @param srcFile
* @param destFile
* @throws IOException
*/
public static void copyFileByByte(File srcFile,File destFile)throws IOException{
if(!srcFile.exists()){
throw new IllegalArgumentException("文件:"+srcFile+"不存在");
}
if(!srcFile.isFile()){
throw new IllegalArgumentException(srcFile+"不是文件");
}
FileInputStream in = new FileInputStream(srcFile);
FileOutputStream out = new FileOutputStream(destFile);
int c ;
while((c = in.read())!=-1){
out.write(c);
out.flush();
}
in.close();
out.close();
}
}
写一个测试类测试一下性能:
<span style="font-size:18px;">package com.imooc.io;
import java.io.File;
import java.io.IOException;
public class IOUtilTest3 {
/**
* @param args
*/
public static void main(String[] args) {
try {
Long start = System.currentTimeMillis();
JavaIOCopy.copyFile(new File("d:\\1.jpg"), new File(
"d:\\2.jpg"));
Long end = System.currentTimeMillis();
System.out.println(end-start);
} catch (IOException e) {
e.printStackTrace();
}
}
}</span>
测试结果结果为:31毫秒
copyFileByBuffer 耗时为 43857毫秒
copyFileByByte 耗时更长 65866毫秒
性能上差距太大。。。。
package com.imooc.io;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* 文件拷贝的工具类
* @author I
*
*/
public class JavaIOCopy {
/**
* 文件拷贝,字节批量读取
* @param srcFile
* @param destFile
* @throws IOException
*/
public static void copyFile(File srcFile,File destFile)throws IOException{
if(!srcFile.exists()){
throw new IllegalArgumentException("文件:"+srcFile+"不存在");
}
if(!srcFile.isFile()){
throw new IllegalArgumentException(srcFile+"不是文件");
}
FileInputStream in = new FileInputStream(srcFile);
FileOutputStream out = new FileOutputStream(destFile);
byte[] buf = new byte[8*1024];
int b ;
while((b = in.read(buf))!=-1){
out.write(buf,0,b);
out.flush();
}
in.close();
out.close();
}
/**
* 进行文件的拷贝,利用带缓冲的字节流
* @param srcFile
* @param destFile
* @throws IOException
*/
public static void copyFileByBuffer(File srcFile,File destFile)throws IOException{
if(!srcFile.exists()){
throw new IllegalArgumentException("文件:"+srcFile+"不存在");
}
if(!srcFile.isFile()){
throw new IllegalArgumentException(srcFile+"不是文件");
}
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(srcFile));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(destFile));
int c ;
while((c = bis.read())!=-1){
bos.write(c);
bos.flush();
}
bis.close();
bos.close();
}
/**
* 单字节,不带缓冲进行文件拷贝
* @param srcFile
* @param destFile
* @throws IOException
*/
public static void copyFileByByte(File srcFile,File destFile)throws IOException{
if(!srcFile.exists()){
throw new IllegalArgumentException("文件:"+srcFile+"不存在");
}
if(!srcFile.isFile()){
throw new IllegalArgumentException(srcFile+"不是文件");
}
FileInputStream in = new FileInputStream(srcFile);
FileOutputStream out = new FileOutputStream(destFile);
int c ;
while((c = in.read())!=-1){
out.write(c);
out.flush();
}
in.close();
out.close();
}
}
写一个测试类测试一下性能:
<span style="font-size:18px;">package com.imooc.io;
import java.io.File;
import java.io.IOException;
public class IOUtilTest3 {
/**
* @param args
*/
public static void main(String[] args) {
try {
Long start = System.currentTimeMillis();
JavaIOCopy.copyFile(new File("d:\\1.jpg"), new File(
"d:\\2.jpg"));
Long end = System.currentTimeMillis();
System.out.println(end-start);
} catch (IOException e) {
e.printStackTrace();
}
}
}</span>
<span style="font-size:18px;"> </span>
测试结果结果为:31毫秒
copyFileByBuffer 耗时为 43857毫秒
copyFileByByte 耗时更长 65866毫秒
性能上差距太大。。。。
相关文章推荐
- 一天一排序之“希尔排序(缩小增量排序)”
- 坦克游戏教程二:实现坦克移动以及方向控制
- 《Java程序设计》第五周学习总结
- cordova plugin 安卓原生插件开发笔记(一)
- Android中配置JDK和SDK的环境变量
- Eclipse创建的项目出现感叹号的解决方法
- java类初始化顺序
- Java 中方法的重载
- springmvc整合slf4j、log4j记录文本日志
- Java多线程使用场景
- Java 中带参带返回值方法的使用
- Java如何实现WebSocket通讯问题(转载)
- Java 正则表达式
- 文件操作File
- eclipse svn拷贝团队 队员文件 到项目导致svn失去版本控制等错误原因
- Java垃圾回收机制
- Java 中带参无返回值方法的使用
- SE Springer小组之《Spring音乐播放器》可行性研究报告三、四
- spring学习---2
- Java static 代码块测试