-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathIOUtil.java
More file actions
97 lines (83 loc) · 2.82 KB
/
Copy pathIOUtil.java
File metadata and controls
97 lines (83 loc) · 2.82 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
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
package rprocessing;
import static java.nio.file.FileVisitResult.CONTINUE;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.file.FileVisitOption;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.EnumSet;
/**
*
* @author github.com/gaocegege
*/
public class IOUtil { // Ignore CheckstyleBear (AbbreviationAsWordInName)
private static final Charset UTF8 = Charset.forName("utf-8");
public static class ResourceReader {
private final Class<?> clazz;
public ResourceReader(final Class<?> clazz) {
this.clazz = clazz;
}
/**
* read text from the resource
*/
public String readText(final String resource) throws IOException {
return IOUtil.readResourceAsText(clazz, resource);
}
}
public static String readResourceAsText(final Class<?> clazz, final String resource)
throws IOException {
final InputStream in = clazz.getResourceAsStream(resource);
return readText(in);
}
public static String readText(final InputStream in) throws IOException {
return new String(readFully(in), UTF8);
}
public static String readText(final Path path) throws IOException {
return new String(Files.readAllBytes(path), UTF8);
}
/**
* Recursively deletes the given directory or file.
*
* @param target Path of file to be deleted.
* @throws IOException
*/
public static void rm(final Path target) throws IOException {
Files.walkFileTree(target, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs)
throws IOException {
Files.delete(file);
return CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(final Path dir, final IOException exc)
throws IOException {
if (exc != null) {
throw exc;
}
Files.delete(dir);
return CONTINUE;
}
});
}
public static void copy(final Path src, final Path target) throws IOException {
final Path dest = Files.isDirectory(target) ? target.resolve(src.getFileName()) : target;
final EnumSet<FileVisitOption> doNotResolveLinks = EnumSet.noneOf(FileVisitOption.class);
Files.walkFileTree(src, doNotResolveLinks, Integer.MAX_VALUE, new TreeCopier(src, dest));
}
public static byte[] readFully(final InputStream in) throws IOException {
try (final ByteArrayOutputStream bytes = new ByteArrayOutputStream(1024)) {
final byte[] buf = new byte[1024];
int n;
while ((n = in.read(buf)) != -1) {
bytes.write(buf, 0, n);
}
return bytes.toByteArray();
}
}
}