1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
| public class CopyFileUtil {
public static void copyDir(String srcPath,String destPath) throws FileNotFoundException, IOException { File src = new File(srcPath); File dest = new File(destPath); copyDir(src, dest); }
public static void copyDir(File src,File dest) throws FileNotFoundException, IOException { if (src.isDirectory()) { dest = new File(dest,src.getName()); } copyDirectory(src, dest); }
public static void copyDirectory(File src,File dest) throws FileNotFoundException,IOException{ if (src.isFile()) { try { CopyFileUtil.copyFile(src, dest); } catch (FileNotFoundException e) { } catch (IOException e) { } }else if (src.isDirectory()) { dest.mkdirs(); for(File sub:src.listFiles()) { copyDirectory(sub, new File(dest,sub.getName())); } } }
public static void copyFile(String srcPath,String destPaht) throws IOException,FileNotFoundException { copyFile(new File(srcPath), new File(destPaht)); }
public static void copyFile(File src,File dest) throws IOException,FileNotFoundException { if (!src.isFile()) { System.out.println("只能拷贝文件"); throw new IOException("只能拷贝文件"); } if (dest.isDirectory()) { System.out.println("不能建立与文件夹同名的文件"); throw new IOException("不能建立与文件夹同名的文件"); } InputStream is = new BufferedInputStream(new FileInputStream(src)); OutputStream os = new BufferedOutputStream(new FileOutputStream(dest)); byte[] flush = new byte[1024]; int len = 0; while(-1!=(len= (is.read(flush)))) { os.write(flush); } os.flush(); os.close(); is.close(); } }
|