-
-
Notifications
You must be signed in to change notification settings - Fork 7k
/
Copy pathFileUtils.java
296 lines (253 loc) · 8.42 KB
/
FileUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package processing.app.helpers;
import org.apache.commons.compress.utils.IOUtils;
import java.io.*;
import java.util.*;
import java.util.regex.Pattern;
public class FileUtils {
private static final List<String> SOURCE_CONTROL_FOLDERS = Arrays.asList("CVS", "RCS", ".git", ".svn", ".hg", ".bzr");
private static final Pattern BACKSLASH = Pattern.compile("\\\\");
/**
* Checks, whether the child directory is a subdirectory of the base directory.
*
* @param base the base directory.
* @param child the suspected child directory.
* @return true, if the child is a subdirectory of the base directory.
*/
public static boolean isSubDirectory(File base, File child) {
try {
base = base.getCanonicalFile();
child = child.getCanonicalFile();
} catch (IOException e) {
return false;
}
File parentFile = child;
while (parentFile != null) {
if (base.equals(parentFile)) {
return true;
}
parentFile = parentFile.getParentFile();
}
return false;
}
public static void copyFile(File source, File dest) throws IOException {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(source);
fos = new FileOutputStream(dest);
byte[] buf = new byte[4096];
int readBytes = -1;
while ((readBytes = fis.read(buf, 0, buf.length)) != -1) {
fos.write(buf, 0, readBytes);
}
} finally {
IOUtils.closeQuietly(fis);
IOUtils.closeQuietly(fos);
}
}
public static void copy(File sourceFolder, File destFolder) throws IOException {
for (File file : sourceFolder.listFiles()) {
File destFile = new File(destFolder, file.getName());
if (file.isDirectory()) {
if (!destFile.mkdir()) {
throw new IOException("Unable to create folder: " + destFile);
}
copy(file, destFile);
} else {
copyFile(file, destFile);
}
}
}
public static void recursiveDelete(File file) {
if (file == null) {
return;
}
if (file.isDirectory()) {
File[] files = file.listFiles();
if (files == null) {
return;
}
for (File current : files) {
recursiveDelete(current);
}
}
file.delete();
}
public static File createTempFolder() throws IOException {
return createTempFolderIn(new File(System.getProperty("java.io.tmpdir")));
}
public static File createTempFolderIn(File parent) throws IOException {
File tmpFolder = new File(parent, "arduino_" + new Random().nextInt(1000000));
if (!tmpFolder.mkdir()) {
throw new IOException("Unable to create temp folder " + tmpFolder);
}
return tmpFolder;
}
//
// Compute relative path to "target" from a directory "origin".
//
// If "origin" is not absolute, it is relative from the current directory.
// If "target" is not absolute, it is relative from "origin".
//
// by Shigeru KANEMOTO at SWITCHSCIENCE.
//
public static String relativePath(String origin, String target) {
try {
origin = (new File(origin)).getCanonicalPath();
File targetFile = new File(target);
if (targetFile.isAbsolute())
target = targetFile.getCanonicalPath();
else
target = (new File(origin, target)).getCanonicalPath();
} catch (IOException e) {
return null;
}
if (origin.equals(target)) {
// origin and target is identical.
return ".";
}
if (origin.equals(File.separator)) {
// origin is root.
return "." + target;
}
String prefix = "";
String root = File.separator;
if (System.getProperty("os.name").indexOf("Windows") != -1) {
if (origin.startsWith("\\\\") || target.startsWith("\\\\")) {
// Windows UNC path not supported.
return null;
}
char originLetter = origin.charAt(0);
char targetLetter = target.charAt(0);
if (Character.isLetter(originLetter) && Character.isLetter(targetLetter)) {
// Windows only
if (originLetter != targetLetter) {
// Drive letters differ
return null;
}
}
prefix = "" + originLetter + ':';
root = prefix + File.separator;
}
String relative = "";
while (!target.startsWith(origin + File.separator)) {
origin = (new File(origin)).getParent();
if (origin.equals(root))
origin = prefix;
relative += "..";
relative += File.separator;
}
return relative + target.substring(origin.length() + 1);
}
public static String getLinuxPathFrom(File file) {
return BACKSLASH.matcher(file.getAbsolutePath()).replaceAll("/");
}
public static boolean isSCCSOrHiddenFile(File file) {
return isSCCSFolder(file) || isHiddenFile(file);
}
public static boolean isHiddenFile(File file) {
return file.isHidden() || file.getName().charAt(0) == '.';
}
public static boolean isSCCSFolder(File file) {
return file.isDirectory() && SOURCE_CONTROL_FOLDERS.contains(file.getName());
}
public static String readFileToString(File file) throws IOException {
return readFileToString(file, "UTF-8");
}
public static String readFileToString(File file, String encoding) throws IOException {
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding));
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
return sb.toString();
} finally {
IOUtils.closeQuietly(reader);
}
}
public static List<String> readFileToListOfStrings(File file) throws IOException {
List<String> strings = new LinkedList<String>();
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String line;
while ((line = reader.readLine()) != null) {
line = line.replaceAll("\r", "").replaceAll("\n", "").replaceAll(" ", "");
strings.add(line);
}
return strings;
} finally {
if (reader != null) {
reader.close();
}
}
}
/**
* Returns true if the given file has any of the given extensions.
*
* @param file File whose name to look at
* @param extensions Extensions to consider (just the extension, without the
* dot). Should all be lowercase, case insensitive matching
* is used.
*/
public static boolean hasExtension(File file, String... extensions) {
return hasExtension(file, Arrays.asList(extensions));
}
public static boolean hasExtension(File file, List<String> extensions) {
String pieces[] = file.getName().split("\\.");
if (pieces.length < 2) {
return false;
}
String extension = pieces[pieces.length - 1];
return extensions.contains(extension.toLowerCase());
}
/**
* Recursively find all files in a folder with the specified
* extension. Excludes hidden files and folders and
* source control folders.
*
* @param folder Folder to look into
* @param recursive <b>true</b> will recursively find all files in sub-folders
* @param extensions A list of file extensions to search (just the extension,
* without the dot). Should all be lowercase, case
* insensitive matching is used. If no extensions are
* passed, all files are returned.
* @return
*/
public static List<File> listFiles(File folder, boolean recursive,
String... extensions) {
return listFiles(folder, recursive, Arrays.asList(extensions));
}
public static List<File> listFiles(File folder, boolean recursive,
List<String> extensions) {
List<File> result = new ArrayList<File>();
for (File file : folder.listFiles()) {
if (isSCCSOrHiddenFile(file))
continue;
if (file.isDirectory()) {
if (recursive)
result.addAll(listFiles(file, true, extensions));
continue;
}
if (extensions.isEmpty() || hasExtension(file, extensions))
result.add(file);
}
return result;
}
public static File newFile(File parent, String... parts) {
File result = parent;
for (String part : parts) {
result = new File(result, part);
}
return result;
}
public static boolean deleteIfExists(File file) {
if (file == null) {
return true;
}
return file.delete();
}
}