forked from getsentry/sentry-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileUtils.java
More file actions
63 lines (58 loc) · 2.02 KB
/
Copy pathFileUtils.java
File metadata and controls
63 lines (58 loc) · 2.02 KB
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
package io.sentry.util;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;
@ApiStatus.Internal
public final class FileUtils {
/**
* Deletes the file or directory denoted by a path. If it is a directory, all files and directory
* inside it are deleted recursively. Note that if this operation fails then partial deletion may
* have taken place.
*
* @param file file or directory to delete
* @return true if the file/directory is successfully deleted, false otherwise
*/
public static boolean deleteRecursively(@Nullable File file) {
if (file == null || !file.exists()) {
return true;
}
if (file.isFile()) {
return file.delete();
}
File[] children = file.listFiles();
if (children == null) return true;
for (File f : children) {
if (!deleteRecursively(f)) return false;
}
return file.delete();
}
/**
* Reads the content of a File into a String. If the file does not exist or is not a file, null is
* returned. Do not use with large files, as the String is kept in memory!
*
* @param file file to read
* @return a String containing all the content of the file, or null if it doesn't exists
* @throws IOException In case of error reading the file
*/
@SuppressWarnings("DefaultCharset")
public static @Nullable String readText(@Nullable File file) throws IOException {
if (file == null || !file.exists() || !file.isFile() || !file.canRead()) {
return null;
}
StringBuilder contentBuilder = new StringBuilder();
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line;
// The first line doesn't need the leading \n
if ((line = br.readLine()) != null) {
contentBuilder.append(line);
}
while ((line = br.readLine()) != null) {
contentBuilder.append("\n").append(line);
}
}
return contentBuilder.toString();
}
}