This project has retired. For details please refer to its
Attic page.
FileUtils xref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.giraph.utils;
20
21 import org.apache.hadoop.conf.Configuration;
22 import org.apache.hadoop.fs.FileSystem;
23 import org.apache.hadoop.fs.Path;
24 import org.apache.log4j.Logger;
25
26 import com.google.common.base.Charsets;
27 import com.google.common.io.Closeables;
28 import com.google.common.io.Files;
29
30 import java.io.File;
31 import java.io.FileFilter;
32 import java.io.IOException;
33 import java.io.Writer;
34
35
36
37
38 public class FileUtils {
39
40 private static final Logger LOG = Logger.getLogger(FileUtils.class);
41
42
43
44
45 private FileUtils() {
46 }
47
48
49
50
51
52
53
54 public static File createTestDir(String computationName)
55 throws IOException {
56 String systemTmpDir = System.getProperty("java.io.tmpdir");
57 long simpleRandomLong = (long) (Long.MAX_VALUE * Math.random());
58 File testTempDir = new File(systemTmpDir, "giraph-" +
59 computationName + '-' + simpleRandomLong);
60 if (!testTempDir.mkdir()) {
61 throw new IOException("Could not create " + testTempDir);
62 }
63 testTempDir.deleteOnExit();
64 return testTempDir;
65 }
66
67
68
69
70
71
72
73
74
75 public static File createTempFile(File parent, String name)
76 throws IOException {
77 return createTestTempFileOrDir(parent, name, false);
78 }
79
80
81
82
83
84
85
86
87
88 public static File createTempDir(File parent, String name)
89 throws IOException {
90 File dir = createTestTempFileOrDir(parent, name, true);
91 if (!dir.delete()) {
92 LOG.error("createTempDir: Failed to create directory " + dir);
93 }
94 return dir;
95 }
96
97
98
99
100
101
102
103
104
105
106 public static File createTestTempFileOrDir(File parent, String name,
107 boolean dir) throws IOException {
108 File f = new File(parent, name);
109 f.deleteOnExit();
110 if (dir && !f.mkdirs()) {
111 throw new IOException("Could not make directory " + f);
112 }
113 return f;
114 }
115
116
117
118
119
120
121
122
123 public static void writeLines(File file, String[] lines)
124 throws IOException {
125 Writer writer = Files.newWriter(file, Charsets.UTF_8);
126 try {
127 for (String line : lines) {
128 writer.write(line);
129 writer.write('\n');
130 }
131 } finally {
132 Closeables.close(writer, true);
133 }
134 }
135
136
137
138
139
140
141 public static void delete(File dir) {
142 if (dir != null) {
143 new DeletingVisitor().accept(dir);
144 }
145 }
146
147
148
149
150 private static class DeletingVisitor implements FileFilter {
151 @Override
152 public boolean accept(File f) {
153 if (!f.isFile()) {
154 if (f.listFiles(this) == null) {
155 LOG.error("accept: Failed to list files of " + f);
156 }
157 }
158 if (!f.delete()) {
159 LOG.error("accept: Failed to delete file " + f);
160 }
161 return false;
162 }
163 }
164
165
166
167
168
169
170
171
172 public static void deletePath(Configuration conf, String path)
173 throws IOException {
174 deletePath(conf, new Path(path));
175 }
176
177
178
179
180
181
182
183
184 public static void deletePath(Configuration conf, Path path)
185 throws IOException {
186 FileSystem fs = FileSystem.get(conf);
187 fs.delete(path, true);
188 }
189 }