//复制文件夹 package Demo; import java.util.*; import java.io.*; //import org.apache.*; import org.apache.commons.io.FileUtils;//使用的是这个库 public class loop { public static void copyFile (File sorce, File dest){ try{ FileUtils.copyFile(sorce,dest); }catch (Exception e){ e.printStackTrace(); System.out.println(e); } } public static void main(String[] args){ File sorce = new File("C:\Users\shun.guo\Desktop\新建文件夹\t1\新建文本文档.txt"); File des = new File("C:\Users\shun.guo\Desktop\新建文件夹\t2\hh.txt"); loop.copyFile(sorce,des); } }
package Demo; import java.util.*; import java.io.*; //import org.apache.*; import org.apache.commons.io.FileUtils; public class loop { public static void copyFile (File sorce, File dest){ try{//复制文件 FileUtils.copyFile(sorce,dest); }catch (Exception e){ e.printStackTrace(); System.out.println(e); } } public static void copydir(File sorce,File dest){ try{//复制文件夹 FileUtils.copyDirectory(sorce,dest); }catch (Exception e){ e.printStackTrace(); System.out.println(e); } } public static void main(String[] args){ File sorce = new File("C:\Users\shun.guo\Desktop\新建文件夹\t1\新建文本文档.txt"); File des = new File("C:\Users\shun.guo\Desktop\新建文件夹\t2\gk.txt"); loop.copyFile(sorce,des); File sorce1 = new File("C:\Users\shun.guo\Desktop\新建文件夹\t1\tt"); File des1 = new File("C:\Users\shun.guo\Desktop\新建文件夹\t2\tt2"); loop.copydir(sorce1, des1); } }
复制文件和文件夹教程
去官网http://commons.apache.org/proper/commons-io/download_io.cgi下载
Download Apache Commons IO
将下载后的jar文件复制到项目的路径下面,然后再Java的语句里面import就可以了