利用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

代码:

    1. import java.io.File;
    2. import java.io.FileInputStream;
    3. import java.io.FileNotFoundException;
    4. import java.io.FileOutputStream;
    5. import java.io.IOException;
    6. import java.io.InputStream;
    7. import java.io.OutputStream;
    8. import javax.swing.JOptionPane;
    9. /**
    10. * 复制文件或文件夹
    11. *
    12. * zww
    13. */
    14. public class CopyFileUtil {
    15. private static String MESSAGE = "";
    16. /**
    17. * 复制单个文件
    18. *
    19. * @param srcFileName
    20. *            待复制的文件名
    21. * @param descFileName
    22. *            目标文件名
    23. * @param overlay
    24. *            如果目标文件存在,是否覆盖
    25. * @return 如果复制成功返回true,否则返回false
    26. */
    27. public static boolean copyFile(String srcFileName, String destFileName,
    28. boolean overlay) {
    29. File srcFile = new File(srcFileName);
    30. // 判断源文件是否存在
    31. if (!srcFile.exists()) {
    32. MESSAGE = "源文件:" + srcFileName + "不存在!";
    33. JOptionPane.showMessageDialog(null, MESSAGE);
    34. return false;
    35. } else if (!srcFile.isFile()) {
    36. MESSAGE = "复制文件失败,源文件:" + srcFileName + "不是一个文件!";
    37. JOptionPane.showMessageDialog(null, MESSAGE);
    38. return false;
    39. }
    40. // 判断目标文件是否存在
    41. File destFile = new File(destFileName);
    42. if (destFile.exists()) {
    43. // 如果目标文件存在并允许覆盖
    44. if (overlay) {
    45. // 删除已经存在的目标文件,无论目标文件是目录还是单个文件
    46. new File(destFileName).delete();
    47. }
    48. } else {
    49. // 如果目标文件所在目录不存在,则创建目录
    50. if (!destFile.getParentFile().exists()) {
    51. // 目标文件所在目录不存在
    52. if (!destFile.getParentFile().mkdirs()) {
    53. // 复制文件失败:创建目标文件所在目录失败
    54. return false;
    55. }
    56. }
    57. }
    58. // 复制文件
    59. int byteread = 0; // 读取的字节数
    60. InputStream in = null;
    61. OutputStream out = null;
    62. try {
    63. in = new FileInputStream(srcFile);
    64. out = new FileOutputStream(destFile);
    65. byte[] buffer = new byte[1024];
    66. while ((byteread = in.read(buffer)) != -1) {
    67. out.write(buffer, 0, byteread);
    68. }
    69. return true;
    70. } catch (FileNotFoundException e) {
    71. return false;
    72. } catch (IOException e) {
    73. return false;
    74. } finally {
    75. try {
    76. if (out != null)
    77. out.close();
    78. if (in != null)
    79. in.close();
    80. } catch (IOException e) {
    81. e.printStackTrace();
    82. }
    83. }
    84. }
    85. /**
    86. * 复制整个目录的内容
    87. *
    88. * @param srcDirName
    89. *            待复制目录的目录名
    90. * @param destDirName
    91. *            目标目录名
    92. * @param overlay
    93. *            如果目标目录存在,是否覆盖
    94. * @return 如果复制成功返回true,否则返回false
    95. */
    96. public static boolean copyDirectory(String srcDirName, String destDirName,
    97. boolean overlay) {
    98. // 判断源目录是否存在
    99. File srcDir = new File(srcDirName);
    100. if (!srcDir.exists()) {
    101. MESSAGE = "复制目录失败:源目录" + srcDirName + "不存在!";
    102. JOptionPane.showMessageDialog(null, MESSAGE);
    103. return false;
    104. } else if (!srcDir.isDirectory()) {
    105. MESSAGE = "复制目录失败:" + srcDirName + "不是目录!";
    106. JOptionPane.showMessageDialog(null, MESSAGE);
    107. return false;
    108. }
    109. // 如果目标目录名不是以文件分隔符结尾,则加上文件分隔符
    110. if (!destDirName.endsWith(File.separator)) {
    111. destDirName = destDirName + File.separator;
    112. }
    113. File destDir = new File(destDirName);
    114. // 如果目标文件夹存在
    115. if (destDir.exists()) {
    116. // 如果允许覆盖则删除已存在的目标目录
    117. if (overlay) {
    118. new File(destDirName).delete();
    119. } else {
    120. MESSAGE = "复制目录失败:目的目录" + destDirName + "已存在!";
    121. JOptionPane.showMessageDialog(null, MESSAGE);
    122. return false;
    123. }
    124. } else {
    125. // 创建目的目录
    126. System.out.println("目的目录不存在,准备创建。。。");
    127. if (!destDir.mkdirs()) {
    128. System.out.println("复制目录失败:创建目的目录失败!");
    129. return false;
    130. }
    131. }
    132. boolean flag = true;
    133. File[] files = srcDir.listFiles();
    134. for (int i = 0; i < files.length; i++) {
    135. // 复制文件
    136. if (files[i].isFile()) {
    137. flag = CopyFileUtil.copyFile(files[i].getAbsolutePath(),
    138. destDirName + files[i].getName(), overlay);
    139. if (!flag)
    140. break;
    141. } else if (files[i].isDirectory()) {
    142. flag = CopyFileUtil.copyDirectory(files[i].getAbsolutePath(),
    143. destDirName + files[i].getName(), overlay);
    144. if (!flag)
    145. break;
    146. }
    147. }
    148. if (!flag) {
    149. MESSAGE = "复制目录" + srcDirName + "至" + destDirName + "失败!";
    150. JOptionPane.showMessageDialog(null, MESSAGE);
    151. return false;
    152. } else {
    153. return true;
    154. }
    155. }
    156. public static void main(String[] args) {
    157. String srcDirName = "C:/test/test0/test1";
    158. String destDirName = "c:/ttt";
    159. CopyFileUtil.copyDirectory(srcDirName, destDirName, true);
    160. }
    161. }

不考虑多线程优化,单线程文件复制最快的方法是(文件越大该方法越有优势,一般比常用方法快30+%):

  1. private static void nioTransferCopy(File source, File target) {
  2. FileChannel in = null;
  3. FileChannel out = null;
  4. FileInputStream inStream = null;
  5. FileOutputStream outStream = null;
  6. try {
  7. inStream = new FileInputStream(source);
  8. outStream = new FileOutputStream(target);
  9. in = inStream.getChannel();
  10. out = outStream.getChannel();
  11. in.transferTo(0, in.size(), out);
  12. } catch (IOException e) {
  13. e.printStackTrace();
  14. } finally {
  15. close(inStream);
  16. close(in);
  17. close(outStream);
  18. close(out);
  19. }
  20. }

如果需要监测复制进度,可以用第二快的方法(留意buffer的大小,对速度有很大影响):

  1. private static void nioBufferCopy(File source, File target) {
  2. FileChannel in = null;
  3. FileChannel out = null;
  4. FileInputStream inStream = null;
  5. FileOutputStream outStream = null;
  6. try {
  7. inStream = new FileInputStream(source);
  8. outStream = new FileOutputStream(target);
  9. in = inStream.getChannel();
  10. out = outStream.getChannel();
  11. ByteBuffer buffer = ByteBuffer.allocate(4096);
  12. while (in.read(buffer) != -1) {
  13. buffer.flip();
  14. out.write(buffer);
  15. buffer.clear();
  16. }
  17. } catch (IOException e) {
  18. e.printStackTrace();
  19. } finally {
  20. close(inStream);
  21. close(in);
  22. close(outStream);
  23. close(out);
  24. }
  25. }

常用的方法1是:

  1. private static void customBufferBufferedStreamCopy(File source, File target) {
  2. InputStream fis = null;
  3. OutputStream fos = null;
  4. try {
  5. fis = new BufferedInputStream(new FileInputStream(source));
  6. fos = new BufferedOutputStream(new FileOutputStream(target));
  7. byte[] buf = new byte[4096];
  8. int i;
  9. while ((i = fis.read(buf)) != -1) {
  10. fos.write(buf, 0, i);
  11. }
  12. }
  13. catch (Exception e) {
  14. e.printStackTrace();
  15. } finally {
  16. close(fis);
  17. close(fos);
  18. }
  19. }

常用的方法2是:

  1. private static void customBufferStreamCopy(File source, File target) {
  2. InputStream fis = null;
  3. OutputStream fos = null;
  4. try {
  5. fis = new FileInputStream(source);
  6. fos = new FileOutputStream(target);
  7. byte[] buf = new byte[4096];
  8. int i;
  9. while ((i = fis.read(buf)) != -1) {
  10. fos.write(buf, 0, i);
  11. }
  12. }
  13. catch (Exception e) {
  14. e.printStackTrace();
  15. } finally {
  16. close(fis);
  17. close(fos);
  18. }
  19. }

Java: 复制文件最快方法的更多相关文章

  1. Java 复制文件的高效方法

    转载自:http://jingyan.baidu.com/article/ff4116259c2d7712e4823780.html 在Java编程中,复制文件的方法有很多,而且经常要用到.我以前一直 ...

  2. JAVA复制文件最快的算法

    /** * 复制文件 * * @param srcFile * 源文件File * @param destDir * 目标目录File * @param newFileName * 新文件名 * @r ...

  3. Java复制文件用数据流方法,renameTO()方法是相当于剪切操作

    我想达到的效果是,一个文件复制到另一个地方,然后重命名 //判断是否存在 File file = new File("D:/tomcat9.0.12/apache-tomcat-9.0.12 ...

  4. java复制文件夹及所有子目录和文件

    package text; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; im ...

  5. java复制文件的4种方式

    尽管Java提供了一个可以处理文件的IO操作类.但是没有一个复制文件的方法.复制文件是一个重要的操作,当你的程序必须处理很多文件相关的时候.然而有几种方法可以进行Java文件复制操作,下面列举出4中最 ...

  6. [JAVA]java复制文件的4种方式

    尽管Java提供了一个可以处理文件的IO操作类. 但是没有一个复制文件的方法. 复制文件是一个重要的操作,当你的程序必须处理很多文件相关的时候. 然而有几种方法可以进行Java文件复制操作,下面列举出 ...

  7. 4种java复制文件的方式

    尽管Java提供了一个可以处理文件的IO操作类,但是没有一个复制文件的方法.复制文件是一个重要的操作,当你的程序必须处理很多文件相关的时候.然而有几种方法可以进行Java文件复制操作,下面列举出4中最 ...

  8. java复制文件

    package com.test.tes; import java.io.File; import java.io.FileInputStream; import java.io.FileOutput ...

  9. 不能往Windows Server 2008 R2 Server中复制文件的解决方法

    目前一直直接往Windows 2008 R2 Server中复制文件(暂时还没有搭建ftp服务),突然不能复制了,于是百度找到了解决方法,特此记录(记忆). 1.在任务管理器中找到“rdpclip.e ...

随机推荐

  1. ssm使用Ajax的formData进行异步图片上传返回图片路径,并限制格式和大小

    之前整理过SSM的文件上传,这次直接用代码了. 前台页面和js //form表单 <form id= "uploadForm" enctype="multipart ...

  2. php安装amqp扩展

    1.要安装AMQP PHP扩展,必须先安装librabbitmq库 1.1使用以下步骤下载并安装库: # 下载 git clone git://github.com/alanxz/rabbitmq-c ...

  3. 【Sequel Pro】下载查询结果乱码问题处理方式

    1.下载查询结果已CSV格式保存 2.出现乱码问题样式如下截图: 3. 右键下载的CSV文件,选择用文本编辑打开 4.全选打开的页面内容,重新新建“文本编辑”并进行“储存” 5.打开 1.CSV,则看 ...

  4. 46:求1+2+...+n

    /** * 面试题46:求1+2+...+n * 求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). */ ...

  5. caffe 中 plot accuracy和loss, 并画出网络结构图

    plot accuracy + loss 详情可见:http://www.2cto.com/kf/201612/575739.html 1. caffe保存训练输出到log 并绘制accuracy l ...

  6. 七牛云整合Ueditor的ThinkPHP版本

    首先去七牛云官网下载phpSDK工具放在Think/library/Vendor下. ueditor后台调用方法: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...

  7. Adobe Audition CC 2017 (10.0)安装教程

    Adobe Audition CC 2017 (10.0)安装教程 注:下载地址在文章末尾 第一步:首先请将电脑的网络断开,很简单:禁用本地连接或者拔掉网线,这样就可以免除登录Creative Clo ...

  8. My blog in AI ---神经网络,网络架构

    上一篇博文中,我们介绍了神经网络中的神经元,那么该如何组织起来这些神经元,才能发挥出最好的效果去解决现实中的问题呢? 这是一个复杂的问题,在工程中,神经网络的架构也是训练的也是一种超参数,本节先在理论 ...

  9. iOS 11开发教程(十一)了解iOS11应用视图

    iOS 11开发教程(十一)了解iOS11应用视图 在iPhone或者iPad中,用户看到的和摸到的都是视图.视图是用户界面的重要组成元素.本节将主要讲解视图的添加.删除以及位置和大小的设置等内容. ...

  10. 02-c#基础之01-基础语法(二)

    1.变量的存储以及变量的几种类型 变量:用来在计算机当中存储数据. 存储变量的语法: 变量类型 变量名: 变量名=值: int number=100: 2.赋值"=" " ...