1. 程式人生 > >IO文件夾拷貝(文件內含有文件和文件夾)

IO文件夾拷貝(文件內含有文件和文件夾)

final eno fin 調用 pri buffer post src string

 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 }

IO文件夾拷貝(文件內含有文件和文件夾)