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
20
21
22 public class FileProcess {
23
24 private static final Logger logger = LogManager.getLogger(FileProcess.class.getSimpleName());
25
26
27
28 final static int BUFFERSIZE = 1024 * 5;
29
30
31
32
33
34
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
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
60
61
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
97
98
99
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
122
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
152
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 }