文件拷贝效率问题
来源:互联网 发布:mac的扑克游戏在哪里 编辑:程序博客网 时间:2024/04/30 03:52
- package jonavin.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;
- public class IOUtil {
- /**
- * 文件拷贝-- 一个字节一个字节拷贝
- * @param srcFile
- * @param destFile
- */
- 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 b;
- while ((b = in.read()) != -1) {
- out.write(b);//写入一个字节的低八位
- }
- in.close();
- out.close();
- }
- /**
- * 拷贝文件一次读取多个字节
- */
- public static void copyFileByByteBuf(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 b;
- byte[] buf = new byte[8*1024];//开辟8K的缓存
- while ((b = in.read(buf))!=-1) {
- out.write(buf, 0, b);
- }
- in.close();
- out.close();
- }
- /**
- * 通过缓冲流copy文件
- * @throws IOException
- */
- public static void copyFileByBuffed(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);//目标文件
- BufferedInputStream inbuf = new BufferedInputStream(in);
- BufferedOutputStream outbuf = new BufferedOutputStream(out);
- int b;
- while ((b = inbuf.read())!=-1) {
- outbuf.write(b);
- }
- in.close();
- out.close();
- inbuf.close();
- outbuf.close();
- }
- /**
- * 通过缓冲流copy文件 缓冲流一次读取多个字节
- * @throws IOException
- */
- public static void copyFileByBuffedBuf(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);//目标文件
- BufferedInputStream inbuf = new BufferedInputStream(in);
- BufferedOutputStream outbuf = new BufferedOutputStream(out);
- int b;
- byte[] buf = new byte[8*1024];
- while ((b = inbuf.read(buf))!=-1) {
- outbuf.write(buf,0,b);
- }
- in.close();
- out.close();
- inbuf.close();
- outbuf.close();
- }
- /**
- * 文件拷贝
- * @param srcpath
- * @param destpath
- * @throws IOException
- */
- public static void copyFile(String srcpath,String destpath)throws IOException{
- copyFileByBuffedBuf(new File(srcpath), new File(destpath));
- }
- }
调用 : IOUtil.copyFile("C:\\apache-tomcat-7.0.57.zip", "c:\\ret.dat");//源文件大小9M
copyFileByByte ----》 62229 毫秒
copyFileByByteBuf -----》 97 毫秒
copyFileByBuffed -----》 457 毫秒
copyFileByBuffedBuf ----》 28 毫秒
0 0
- 文件拷贝效率问题
- 关于文件拷贝效率问题
- Java中拷贝文件的效率问题
- 关于文件拷贝效率问题(面试总结)
- 文件的拷贝问题
- java读取文件效率问题
- Linux下的几种文件拷贝方式效率对比
- ubuntu文件拷贝的问题
- [Python]深拷贝效率
- [Python]深拷贝效率
- 关于Excel文件读取的效率问题
- Python大文件解析的效率问题
- [NIO]利用内存映射文件以及普通通道进行文件拷贝效率分析
- 多进程拷贝文件 提高io效率 利用 文件空洞 内容很简单 原理很深刻
- 远程桌面不能拷贝文件的问题
- nachos中拷贝文件过大的问题
- 远程桌面不能拷贝文件的问题
- PC_远程桌面无法拷贝文件问题
- MySQL配置文件my.cnf设置
- 漫谈程序员系列:怎样成为技术达人
- android开发中常用命令
- 杭电1695 GCD(容斥原理三题)
- Office中字体大小与像素值对应关系
- 文件拷贝效率问题
- 浅析MFC框架
- 关于TabHost选项卡标签图标始不出现问题.
- VS 一些你可能忽略但是用着很方便的快捷键
- SRM 608 T1 && T2
- jQuery插件开发全解析
- HTTP状态代码
- Visual Studio “类视图”和“对象浏览器”图标含义
- Java基础——I/O