1 /** 2 * 文件夹拷贝(文件内含有文件和文件夹) 3 * 4 * @param src 5 * @param des 6 */ 7 private static void copy(String src, String des) { 8 File file1 = new File(src); 9 File[] fs = file1.listFiles(); 10 File file2 = new File(des); 11 if (!file2.exists()) { 12 file2.mkdirs(); 13 for (File f : fs) { 14 if (f.isFile()) { 15 fileCopy(f.getPath(), des + "\\" + f.getName()); // 调用文件拷贝的方法 16 } else if (f.isDirectory()) { 17 copy(f.getPath(), des + "\\" + f.getName()); 18 } 19 } 20 } 21 } 22 23 /** 24 * 文件拷贝的方法 25 */ 26 private static void fileCopy(String src, String des) { 27 BufferedReader br = null; 28 PrintStream ps = null; 29 try { 30 br = new BufferedReader(new InputStreamReader(new FileInputStream(src))); 31 ps = new PrintStream(new FileOutputStream(des)); 32 String s = null; 33 while ((s = br.readLine()) != null) { 34 ps.println(s); 35 ps.flush(); 36 } 37 38 } catch (FileNotFoundException e) { 39 e.printStackTrace(); 40 } catch (IOException e) { 41 e.printStackTrace(); 42 } finally { 43 try { 44 if (br != null) 45 br.close(); 46 if (ps != null) 47 ps.close(); 48 } catch (IOException e) { 49 e.printStackTrace(); 50 } 51 } 52 }
时间: 2024-10-29 19:10:37