View Javadoc
1   package edu.jiangxin.apktoolbox.file.core;
2   
3   import java.io.BufferedInputStream;
4   import java.io.BufferedOutputStream;
5   import java.io.BufferedReader;
6   import java.io.File;
7   import java.io.FileInputStream;
8   import java.io.FileOutputStream;
9   import java.io.IOException;
10  import java.io.InputStreamReader;
11  
12  import org.apache.commons.lang3.StringUtils;
13  import org.apache.logging.log4j.LogManager;
14  import org.apache.logging.log4j.Logger;
15  
16  /**
17   * 文件或文件夹的简单处理
18   * 
19   * @author jiangxin
20   * @author 2018-09-09
21   */
22  public class FileProcess {
23  
24      private static final Logger logger = LogManager.getLogger(FileProcess.class.getSimpleName());
25      /**
26       * The size of the buffer
27       */
28      final static int BUFFERSIZE = 1024 * 5;
29  
30      /**
31       * 复制文件
32       * 
33       * @param srcFileString
34       * @param desFileString
35       */
36      public static void copyFile(String srcFileString, String desFileString) {
37          File srcFileFile = new File(srcFileString);
38          File desFileFile = new File(desFileString);
39  
40          try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(srcFileFile));
41                  BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(desFileFile))) {
42  
43              // 尚未进行覆盖判断 bad
44              byte[] buffer = new byte[BUFFERSIZE];
45              int len;
46              while ((len = in.read(buffer)) != -1) {
47                  out.write(buffer, 0, len);
48              }
49              out.flush();
50          } catch (IOException e) {
51              logger.error("copy file exception", e);
52          }
53          logger.info("Success to copy {} to {}", srcFileString, desFileString);
54      }
55  
56      /**
57       * 复制文件夹
58       * 
59       * @param srcDirString
60       * @param desDirString
61       * @throws IOException
62       */
63      public static void copyDirectory(String srcDirString, String desDirString) {
64          File srcDirFile = new File(srcDirString);
65          File desDirFile = new File(desDirString);
66          if (!desDirFile.exists()) {
67              boolean ret = desDirFile.mkdirs();
68              if (!ret) {
69                  logger.error("mkidrs failed: " + desDirFile);
70                  return;
71              }
72          }
73  
74          File[] files = srcDirFile.listFiles();
75          if (files == null) {
76              return;
77          }
78          for (int i = 0; i < files.length; i++) {
79              if (files[i].isFile()) {
80                  String srcFileTemp = files[i].getAbsolutePath();
81                  String desFileTemp = desDirFile.getAbsolutePath() + File.separator + files[i].getName();
82                  copyFile(srcFileTemp, desFileTemp);
83              }
84              if (files[i].isDirectory()) {
85                  String srcDirTemp = files[i].getAbsolutePath();
86                  String desDirTemp = desDirFile.getAbsolutePath() + File.separator + files[i].getName();
87                  copyDirectory(srcDirTemp, desDirTemp);
88              }
89          }
90          System.out.println("Success to copy " + srcDirString + " to " + desDirString);
91      }
92  
93      /**
94       * 移动文件
95       * 
96       * @param srcFileString
97       * @param desFileString
98       * @param isOverride
99       * @throws IOException
100      */
101     public static void moveFile(String srcFileString, String desFileString, boolean isOverride) {
102         File srcFileFile = new File(srcFileString);
103         File desFileFile = new File(desFileString);
104         if (isOverride) {
105             if (desFileFile.delete()) {
106                 System.out.println("Success to delete desFile");
107             }
108         } else {
109             return;
110         }
111         copyFile(srcFileString, desFileString);
112         if (srcFileFile.delete()) {
113             System.out.println("Success to delete srcFile");
114         }
115 
116     }
117 
118     /**
119      * 递归删除目录
120      * 
121      * @param dir
122      * @return 返回是否删除成功
123      */
124     public static boolean deleteDir(String dir) {
125         File dirFile = new File(dir);
126         if (dirFile.isDirectory()) {
127             String[] files = dirFile.list();
128             if (files != null) {
129                 for (int i = 0; i < files.length; i++) {
130                     String temp = dirFile.getAbsolutePath() + File.separator.toString() + files[i];
131                     boolean success = deleteDir(temp);
132                     if (!success) {
133                         System.out.println("Something error!");
134                         return false;
135                     }
136                 }
137             }
138         }
139         boolean ret = dirFile.delete();
140         if (ret) {
141             System.out.println("Success to delete " + dir);
142         } else {
143             System.out.println("Something error!");
144         }
145         return ret;
146     }
147 
148     /**
149      * 删除目录组
150      * 
151      * @param dirs
152      * @return 返回是否删除成功
153      */
154     public static boolean deleteDirs(String[] dirs) {
155         for (int i = 0; i < dirs.length; i++) {
156             boolean isSuccessful = deleteDir(dirs[i]);
157             if (isSuccessful == true) {
158                 System.out.println("成功删除目录:" + dirs[i]);
159             }
160         }
161         return false;
162     }
163 }