簡單IO流應用之文件目錄拷貝
阿新 • • 發佈:2019-02-08
subst des tput get reader fileread style 文件夾大小 關閉
文件目錄的拷貝
在學習IO流通常遇到目錄的操作,涉及到一層層的目錄和子孫級文件,往往使用遞歸思想。
遞歸思想之巧妙,但要處理大量的函數壓棧和出棧問題,效率並不高。
主要思路:
將問題模塊化:文件目錄的拷貝其實就是分為
模塊1:從源操作目錄拷貝文件到目的操作目錄
模塊2:在目的操作文件目錄下創建新的目錄
模塊1實現:
1 public static void test(String srcPath,String destPath) {
2
3 /**方法1、
4 * BufferedReader 封裝成緩存流
5 * InputStreamReader 字符流輸入
6 * FileInputStream 字節流輸入
7 * 就相當於 把字符內容(文件)-->字節流(程序可以識別)-->轉換成字符流丟進緩存區
8 */
9 try(BufferedReader reader = new BufferedReader(
10 new InputStreamReader(new FileInputStream(srcPath)));
11 BufferedWriter writer =
12 new BufferedWriter(
13 new OutputStreamWriter(
14 new FileOutputStream(destPath)))){
15 String line = null;
16 while((line = reader.readLine())!=null) {
17
18 writer.write(line);
19 writer.flush();
20 }
21 }catch(IOException e) {
22 e.printStackTrace();
23 }
24 }
25
26 public static void test2(String srcPath,String destPath) {
27
28 /**方法2、
29 * BufferedReader 封裝成緩存流
30 * FileReader 字符流讀取輸入,相當於把字符(文件內容)扔進去了
31 * FileInputStream 字節流輸入
32 * 就相當於 把字符內容a(文件)-->字節流(程序可以識別)-->轉換成字符流丟進緩存區
33 */
34 try(BufferedReader reader = new BufferedReader(
35 new FileReader(srcPath));
36 BufferedWriter writer = new BufferedWriter(
37 new FileWriter(destPath))){
38 String line = null;
39 while((line = reader.readLine())!=null) {
40
41 writer.write(line);
42 writer.flush();
43 }
44 }catch(IOException e) {
45 e.printStackTrace();
46 }
47 }
模塊2實現:
1 public static void Copy(String srcPath,String destPath) {
2
3 File src = new File(srcPath); //創建源操作目錄文件
4 File[] files = src.listFiles(); //列出子文件(裏面包括目錄)
5 for(int i = 0; i<files.length; i++) {
6 if(files[i].isDirectory()) { //如果是目錄
7 File destNext = new File(destPath+File.separator+files[i].getName());
8 destNext.mkdirs(); //在目標文件夾中創建目錄
9 //遞歸
10 Copy(srcPath+File.separator+files[i].getName(),destPath+File.separator+files[i].getName());
11 }else {//如果是文件,那就拷貝
12 test2(files[i].getAbsolutePath(),destPath+File.separator+files[i].getName());
13 }
14 }
15 }
全部代碼:
1 package io應用之文件夾拷貝; 2 3 import java.io.BufferedReader; 4 import java.io.BufferedWriter; 5 import java.io.File; 6 import java.io.FileInputStream; 7 import java.io.FileOutputStream; 8 import java.io.FileReader; 9 import java.io.FileWriter; 10 import java.io.IOException; 11 import java.io.InputStreamReader; 12 import java.io.OutputStream; 13 import java.io.OutputStreamWriter; 14 15 /** 16 * 文件夾的拷貝,包括裏面的子文件和子文件夾 17 * 思路: 18 * 1、拿到一個文件對象(目錄),在目的路徑下創建一個目錄 19 * 2、列出所有的文件對象,有遇到文件copy到新的目錄,如果遇到目錄 20 * 則進入目的的同級的文件目錄下再創建目錄,依次遞歸... 21 * 3、遞歸出口是沒有發現目錄和文件 22 * @author liuzeyu12a 23 * 24 */ 25 public class CopyDir { 26 27 public static void main(String[] args) { 28 29 String srcPath="C:/Users/liuzeyu12a/Desktop/洛谷OJ"; 30 String destPath = "E:/JAVA/workspac/測試"; 31 32 int index = srcPath.lastIndexOf(‘/‘); 33 String subSrcPath = srcPath.substring(index, srcPath.length()-1); 34 destPath = destPath+subSrcPath; //此時已經為創建"洛谷"OJ文件夾做準備了 35 Copy(srcPath,destPath); 36 } 37 38 public static void test(String srcPath,String destPath) { 39 /**方法1、 40 * BufferedReader 封裝成緩存流 41 * InputStreamReader 字符流輸入 42 * FileInputStream 字節流輸入 43 * 就相當於 把字符內容(文件)-->字節流(程序可以識別)-->轉換成字符流丟進緩存區 44 */ 45 try(BufferedReader reader = new BufferedReader( 46 new InputStreamReader(new FileInputStream(srcPath))); 47 BufferedWriter writer = 48 new BufferedWriter( 49 new OutputStreamWriter( 50 new FileOutputStream(destPath)))){ 51 String line = null; 52 while((line = reader.readLine())!=null) { 53 54 writer.write(line); 55 writer.flush(); 56 } 57 }catch(IOException e) { 58 e.printStackTrace(); 59 } 60 } 61 62 public static void test2(String srcPath,String destPath) { 63 /**方法2、 64 * BufferedReader 封裝成緩存流 65 * FileReader 字符流讀取輸入,相當於把字符(文件內容)扔進去了 66 * FileInputStream 字節流輸入 67 * 就相當於 把字符內容a(文件)-->字節流(程序可以識別)-->轉換成字符流丟進緩存區 68 */ 69 try(BufferedReader reader = new BufferedReader( 70 new FileReader(srcPath)); 71 BufferedWriter writer = new BufferedWriter( 72 new FileWriter(destPath))){ 73 String line = null; 74 while((line = reader.readLine())!=null) { 75 76 writer.write(line); 77 writer.flush(); 78 } 79 }catch(IOException e) { 80 e.printStackTrace(); 81 } 82 } 83 84 public static void Copy(String srcPath,String destPath) { 85 File src = new File(srcPath); //創建源操作目錄文件 86 File[] files = src.listFiles(); //列出子文件(裏面包括目錄) 87 for(int i = 0; i<files.length; i++) { 88 if(files[i].isDirectory()) { //如果是目錄 89 File destNext = new File(destPath+File.separator+files[i].getName()); 90 destNext.mkdirs(); //在目標文件夾中創建目錄 91 //遞歸 92 Copy(srcPath+File.separator+files[i].getName(),destPath+File.separator+files[i].getName()); 93 }else {//如果是文件,那就拷貝 94 test2(files[i].getAbsolutePath(),destPath+File.separator+files[i].getName()); 95 } 96 } 97 } 98 }View Code
參考資料:
https://www.cnblogs.com/rainmer/p/4605064.html
1 package io深入學習; 2 3 import java.io.BufferedReader; 4 import java.io.BufferedWriter; 5 import java.io.File; 6 import java.io.FileInputStream; 7 import java.io.FileOutputStream; 8 import java.io.FileReader; 9 import java.io.FileWriter; 10 import java.io.IOException; 11 import java.io.InputStream; 12 import java.io.OutputStream; 13 14 /** 15 * 比較當我們使用緩沖流和沒有使用緩存流之間的對比 16 * @author liuzeyu12a 17 * 18 */ 19 public class BufferedCharIoCopy { 20 21 public static void main(String[] args) { 22 long time1 = System.currentTimeMillis(); 23 copy2("char.txt","cde.txt"); 24 long time2 = System.currentTimeMillis(); 25 System.out.println(time2-time1); 26 } 27 public static void copy(String srcPath,String destPath) { 28 //1、創建源 29 File src = new File(srcPath); //源頭 30 File dest = new File(destPath);//目的地 31 //2、選擇流 32 InputStream is =null; 33 OutputStream os =null; 34 try { 35 is =new FileInputStream(src); 36 os = new FileOutputStream(dest); 37 //3、操作 (分段讀取) 38 byte[] flush = new byte[1024]; //緩沖容器 39 int len = -1; //接收長度 40 while((len=is.read(flush))!=-1) { 41 os.write(flush,0,len); //分段寫出 42 } 43 os.flush(); 44 }catch (IOException e) { 45 e.printStackTrace(); 46 }finally{ 47 //4、釋放資源 分別關閉 先打開的後關閉 48 try { 49 if (null != os) { 50 os.close(); 51 } 52 } catch (IOException e) { 53 e.printStackTrace(); 54 } 55 56 try { 57 if(null!=is) { 58 is.close(); 59 } 60 } catch (IOException e) { 61 e.printStackTrace(); 62 } 63 } 64 } 65 66 //套上字符緩存流 67 public static void copy2(String srcPath,String destPath) { 68 try(BufferedReader is = new BufferedReader(new FileReader(srcPath)); 69 BufferedWriter os = new BufferedWriter( new FileWriter(destPath))) { 70 try { 71 String line =null; 72 while((line=is.readLine())!=null) { 73 os.write(line); 74 } 75 } catch (IOException e) { 76 e.printStackTrace(); 77 } 78 } catch (IOException e1) { 79 e1.printStackTrace(); 80 } 81 } 82 }View Code
1 package 測試io; 2 3 import java.io.File; 4 5 public class FileDirDemo01 { 6 7 // 文件夾長度 8 private int len; 9 // 文件夾路徑 10 private String path; 11 // 文件夾個數 12 private int dirSize; 13 // 文件個數 14 private int fileSize; 15 16 // 文件源 17 private File src; 18 19 // 構造方法,創建file對象 20 21 public FileDirDemo01(String path) { 22 this.path = path; 23 this.src = new File(path); 24 dirCount(this.src); 25 } 26 27 public static void main(String[] args) { 28 FileDirDemo01 dirDemo = new FileDirDemo01("E:\\JAVA\\workspac\\BF算法與KMP算法"); 29 System.out.println("文件夾大小" + dirDemo.len + "文件個數" + dirDemo.fileSize + "文件夾個數" + dirDemo.dirSize); 30 } 31 32 //文件計數,文件夾 33 public void dirCount(File f) { 34 35 if (f.exists() && f != null) { 36 37 if (f.isFile()) { 38 len += f.length(); 39 this.fileSize++; // 文件個數 40 } else { 41 this.dirSize++; 42 for (File flies : f.listFiles()) { 43 dirCount(flies); 44 } 45 } 46 } 47 } 48 49 }View Code
簡單IO流應用之文件目錄拷貝