Java: 复制文件最快方法
利用Java复制文件到处都可以用到,这里总结了一个类供大家参考。里面总共有两个方法:
public static boolean copyFile(String srcFileName, String destFileName,boolean overlay);
public static boolean copyDirectory(String srcDirName, String destDirName,boolean overlay) ;
其中:
srcFileName 待复制的文件名
descFileName 目标文件名
overlay 如果目标文件存在,是否覆盖
如果复制成功返回true,否则返回false
代码:
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import javax.swing.JOptionPane;
- /**
- * 复制文件或文件夹
- *
- * zww
- */
- public class CopyFileUtil {
- private static String MESSAGE = "";
- /**
- * 复制单个文件
- *
- * @param srcFileName
- * 待复制的文件名
- * @param descFileName
- * 目标文件名
- * @param overlay
- * 如果目标文件存在,是否覆盖
- * @return 如果复制成功返回true,否则返回false
- */
- public static boolean copyFile(String srcFileName, String destFileName,
- boolean overlay) {
- File srcFile = new File(srcFileName);
- // 判断源文件是否存在
- if (!srcFile.exists()) {
- MESSAGE = "源文件:" + srcFileName + "不存在!";
- JOptionPane.showMessageDialog(null, MESSAGE);
- return false;
- } else if (!srcFile.isFile()) {
- MESSAGE = "复制文件失败,源文件:" + srcFileName + "不是一个文件!";
- JOptionPane.showMessageDialog(null, MESSAGE);
- return false;
- }
- // 判断目标文件是否存在
- File destFile = new File(destFileName);
- if (destFile.exists()) {
- // 如果目标文件存在并允许覆盖
- if (overlay) {
- // 删除已经存在的目标文件,无论目标文件是目录还是单个文件
- new File(destFileName).delete();
- }
- } else {
- // 如果目标文件所在目录不存在,则创建目录
- if (!destFile.getParentFile().exists()) {
- // 目标文件所在目录不存在
- if (!destFile.getParentFile().mkdirs()) {
- // 复制文件失败:创建目标文件所在目录失败
- return false;
- }
- }
- }
- // 复制文件
- int byteread = 0; // 读取的字节数
- InputStream in = null;
- OutputStream out = null;
- try {
- in = new FileInputStream(srcFile);
- out = new FileOutputStream(destFile);
- byte[] buffer = new byte[1024];
- while ((byteread = in.read(buffer)) != -1) {
- out.write(buffer, 0, byteread);
- }
- return true;
- } catch (FileNotFoundException e) {
- return false;
- } catch (IOException e) {
- return false;
- } finally {
- try {
- if (out != null)
- out.close();
- if (in != null)
- in.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- /**
- * 复制整个目录的内容
- *
- * @param srcDirName
- * 待复制目录的目录名
- * @param destDirName
- * 目标目录名
- * @param overlay
- * 如果目标目录存在,是否覆盖
- * @return 如果复制成功返回true,否则返回false
- */
- public static boolean copyDirectory(String srcDirName, String destDirName,
- boolean overlay) {
- // 判断源目录是否存在
- File srcDir = new File(srcDirName);
- if (!srcDir.exists()) {
- MESSAGE = "复制目录失败:源目录" + srcDirName + "不存在!";
- JOptionPane.showMessageDialog(null, MESSAGE);
- return false;
- } else if (!srcDir.isDirectory()) {
- MESSAGE = "复制目录失败:" + srcDirName + "不是目录!";
- JOptionPane.showMessageDialog(null, MESSAGE);
- return false;
- }
- // 如果目标目录名不是以文件分隔符结尾,则加上文件分隔符
- if (!destDirName.endsWith(File.separator)) {
- destDirName = destDirName + File.separator;
- }
- File destDir = new File(destDirName);
- // 如果目标文件夹存在
- if (destDir.exists()) {
- // 如果允许覆盖则删除已存在的目标目录
- if (overlay) {
- new File(destDirName).delete();
- } else {
- MESSAGE = "复制目录失败:目的目录" + destDirName + "已存在!";
- JOptionPane.showMessageDialog(null, MESSAGE);
- return false;
- }
- } else {
- // 创建目的目录
- System.out.println("目的目录不存在,准备创建。。。");
- if (!destDir.mkdirs()) {
- System.out.println("复制目录失败:创建目的目录失败!");
- return false;
- }
- }
- boolean flag = true;
- File[] files = srcDir.listFiles();
- for (int i = 0; i < files.length; i++) {
- // 复制文件
- if (files[i].isFile()) {
- flag = CopyFileUtil.copyFile(files[i].getAbsolutePath(),
- destDirName + files[i].getName(), overlay);
- if (!flag)
- break;
- } else if (files[i].isDirectory()) {
- flag = CopyFileUtil.copyDirectory(files[i].getAbsolutePath(),
- destDirName + files[i].getName(), overlay);
- if (!flag)
- break;
- }
- }
- if (!flag) {
- MESSAGE = "复制目录" + srcDirName + "至" + destDirName + "失败!";
- JOptionPane.showMessageDialog(null, MESSAGE);
- return false;
- } else {
- return true;
- }
- }
- public static void main(String[] args) {
- String srcDirName = "C:/test/test0/test1";
- String destDirName = "c:/ttt";
- CopyFileUtil.copyDirectory(srcDirName, destDirName, true);
- }
- }
不考虑多线程优化,单线程文件复制最快的方法是(文件越大该方法越有优势,一般比常用方法快30+%):
- private static void nioTransferCopy(File source, File target) {
- FileChannel in = null;
- FileChannel out = null;
- FileInputStream inStream = null;
- FileOutputStream outStream = null;
- try {
- inStream = new FileInputStream(source);
- outStream = new FileOutputStream(target);
- in = inStream.getChannel();
- out = outStream.getChannel();
- in.transferTo(0, in.size(), out);
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- close(inStream);
- close(in);
- close(outStream);
- close(out);
- }
- }
如果需要监测复制进度,可以用第二快的方法(留意buffer的大小,对速度有很大影响):
- private static void nioBufferCopy(File source, File target) {
- FileChannel in = null;
- FileChannel out = null;
- FileInputStream inStream = null;
- FileOutputStream outStream = null;
- try {
- inStream = new FileInputStream(source);
- outStream = new FileOutputStream(target);
- in = inStream.getChannel();
- out = outStream.getChannel();
- ByteBuffer buffer = ByteBuffer.allocate(4096);
- while (in.read(buffer) != -1) {
- buffer.flip();
- out.write(buffer);
- buffer.clear();
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- close(inStream);
- close(in);
- close(outStream);
- close(out);
- }
- }
常用的方法1是:
- private static void customBufferBufferedStreamCopy(File source, File target) {
- InputStream fis = null;
- OutputStream fos = null;
- try {
- fis = new BufferedInputStream(new FileInputStream(source));
- fos = new BufferedOutputStream(new FileOutputStream(target));
- byte[] buf = new byte[4096];
- int i;
- while ((i = fis.read(buf)) != -1) {
- fos.write(buf, 0, i);
- }
- }
- catch (Exception e) {
- e.printStackTrace();
- } finally {
- close(fis);
- close(fos);
- }
- }
常用的方法2是:
- private static void customBufferStreamCopy(File source, File target) {
- InputStream fis = null;
- OutputStream fos = null;
- try {
- fis = new FileInputStream(source);
- fos = new FileOutputStream(target);
- byte[] buf = new byte[4096];
- int i;
- while ((i = fis.read(buf)) != -1) {
- fos.write(buf, 0, i);
- }
- }
- catch (Exception e) {
- e.printStackTrace();
- } finally {
- close(fis);
- close(fos);
- }
- }
Java: 复制文件最快方法的更多相关文章
- Java 复制文件的高效方法
转载自:http://jingyan.baidu.com/article/ff4116259c2d7712e4823780.html 在Java编程中,复制文件的方法有很多,而且经常要用到.我以前一直 ...
- JAVA复制文件最快的算法
/** * 复制文件 * * @param srcFile * 源文件File * @param destDir * 目标目录File * @param newFileName * 新文件名 * @r ...
- Java复制文件用数据流方法,renameTO()方法是相当于剪切操作
我想达到的效果是,一个文件复制到另一个地方,然后重命名 //判断是否存在 File file = new File("D:/tomcat9.0.12/apache-tomcat-9.0.12 ...
- java复制文件夹及所有子目录和文件
package text; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; im ...
- java复制文件的4种方式
尽管Java提供了一个可以处理文件的IO操作类.但是没有一个复制文件的方法.复制文件是一个重要的操作,当你的程序必须处理很多文件相关的时候.然而有几种方法可以进行Java文件复制操作,下面列举出4中最 ...
- [JAVA]java复制文件的4种方式
尽管Java提供了一个可以处理文件的IO操作类. 但是没有一个复制文件的方法. 复制文件是一个重要的操作,当你的程序必须处理很多文件相关的时候. 然而有几种方法可以进行Java文件复制操作,下面列举出 ...
- 4种java复制文件的方式
尽管Java提供了一个可以处理文件的IO操作类,但是没有一个复制文件的方法.复制文件是一个重要的操作,当你的程序必须处理很多文件相关的时候.然而有几种方法可以进行Java文件复制操作,下面列举出4中最 ...
- java复制文件
package com.test.tes; import java.io.File; import java.io.FileInputStream; import java.io.FileOutput ...
- 不能往Windows Server 2008 R2 Server中复制文件的解决方法
目前一直直接往Windows 2008 R2 Server中复制文件(暂时还没有搭建ftp服务),突然不能复制了,于是百度找到了解决方法,特此记录(记忆). 1.在任务管理器中找到“rdpclip.e ...
随机推荐
- hdu 1760 DFS+博弈
0代表可放 1带表不能放 每次放一个2*2的方块 不能放者败如果先手必胜则输出Yes 必胜态:从当前状态所能到达的状态中存在一个必败态必败态:从当前状态所能达到的状态全部是必胜态 Sample Inp ...
- C++ 内存分配(new,operator new)详解
参考:C++ 内存分配(new,operator new)详解 如何限制对象只能建立在堆上或者栈上 new运算符和operator new() new:指我们在C++里通常用到的运算符,比如A* a ...
- 面试题41:和为s的两个数字 || 和为s的连续正数序列
和为s的两个数字 题目:输入一个递增排序的数组和一个数字s,在数组中查找两个数,使得它们的和正好是s.如果有多对数字的和等于s,输出任意一对即可. 有点类似于夹逼的思想 注意两个int相加的和要用lo ...
- 【51nod】1742 开心的小Q
题解 我们由于莫比乌斯函数如果有平方数因子就是0,那么我们可以列出这样的式子 \(\sum_{i = 1}^{n} \sum_{d|i} (1 - |\mu(d)|)\) 然后枚举倍数 \(\sum_ ...
- 2017 Bangladesh National High School Programming Contest ( National Round, Senior Group ), NHSPC 2017 题解
[题目链接] A. Charm Is Not Always Enough 模拟一下就可以了. #include <bits/stdc++.h> using namespace std; i ...
- vue开发computed,watch,method执行的先后顺序
1#computed:计算属性将被混入到 Vue 实例中.所有 getter 和 setter 的 this 上下文自动地绑定为 Vue 实例. 2#methods:methods 将被混入到 Vue ...
- CPU线程 和 Java线程
一 cpu个数.核数.线程数的关系 cpu个数:是指物理上,也及硬件上的核心数: 核数:是逻辑上的,简单理解为逻辑上模拟出的核心数:一个CPU核心数模拟出2线程的CPU 线程数:是同一时刻设备能并行执 ...
- Linux下gcc与g++用法以及编写makefile
1. gcc与g++编译流程: 1) 编译流程: 2) 预处理:生成.i的预处理文件. Ø 只激活预处理,这个不生成文件,需要把它重定向一个输出文件. ...
- BZOJ 2754 SCOI 2012 喵星球上的点名 后缀数组 树状数组
2754: [SCOI2012]喵星球上的点名 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 2068 Solved: 907[Submit][St ...
- 【对比分析五】CSS阻塞和JS阻塞
js 的阻塞特性: 所有浏览器在下载 JS 的时候,会阻止一切其他活动,比如其他资源的下载,内容的呈现等等.直到 JS 下载.解析.执行完毕后才开始继续并行下载其他资源并呈现内容.为了提高用户体验,新 ...