/
FileUtils.java
380 lines (347 loc) · 11.6 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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
* Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0,
* and the EPL 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.lealone.storage.fs;
import java.io.Closeable;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.List;
import org.lealone.common.util.New;
/**
* This utility class contains utility functions that use the file system
* abstraction.
*/
public class FileUtils {
/**
* Checks if a file exists.
* This method is similar to Java 7 <code>java.nio.file.Path.exists</code>.
*
* @param fileName the file name
* @return true if it exists
*/
public static boolean exists(String fileName) {
return FilePath.get(fileName).exists();
}
/**
* Create a directory (all required parent directories must already exist).
* This method is similar to Java 7
* <code>java.nio.file.Path.createDirectory</code>.
*
* @param directoryName the directory name
*/
public static void createDirectory(String directoryName) {
FilePath.get(directoryName).createDirectory();
}
/**
* Create a new file. This method is similar to Java 7
* <code>java.nio.file.Path.createFile</code>, but returns false instead of
* throwing a exception if the file already existed.
*
* @param fileName the file name
* @return true if creating was successful
*/
public static boolean createFile(String fileName) {
return FilePath.get(fileName).createFile();
}
/**
* Delete a file or directory if it exists.
* Directories may only be deleted if they are empty.
* This method is similar to Java 7
* <code>java.nio.file.Path.deleteIfExists</code>.
*
* @param path the file or directory name
*/
public static void delete(String path) {
FilePath.get(path).delete();
}
/**
* Get the canonical file or directory name. This method is similar to Java
* 7 <code>java.nio.file.Path.toRealPath</code>.
*
* @param fileName the file name
* @return the normalized file name
*/
public static String toRealPath(String fileName) {
return FilePath.get(fileName).toRealPath().toString();
}
/**
* Get the parent directory of a file or directory. This method returns null
* if there is no parent. This method is similar to Java 7
* <code>java.nio.file.Path.getParent</code>.
*
* @param fileName the file or directory name
* @return the parent directory name
*/
public static String getParent(String fileName) {
FilePath p = FilePath.get(fileName).getParent();
return p == null ? null : p.toString();
}
/**
* Check if the file name includes a path. This method is similar to Java 7
* <code>java.nio.file.Path.isAbsolute</code>.
*
* @param fileName the file name
* @return if the file name is absolute
*/
public static boolean isAbsolute(String fileName) {
return FilePath.get(fileName).isAbsolute();
}
/**
* Rename a file if this is allowed. This method is similar to Java 7
* <code>java.nio.file.Files.move</code>.
*
* @param source the old fully qualified file name
* @param target the new fully qualified file name
*/
public static void move(String source, String target) {
FilePath.get(source).moveTo(FilePath.get(target), false);
}
/**
* Rename a file if this is allowed, and try to atomically replace an
* existing file. This method is similar to Java 7
* <code>java.nio.file.Files.move</code>.
*
* @param source the old fully qualified file name
* @param target the new fully qualified file name
*/
public static void moveAtomicReplace(String source, String target) {
FilePath.get(source).moveTo(FilePath.get(target), true);
}
/**
* Get the file or directory name (the last element of the path).
* This method is similar to Java 7 <code>java.nio.file.Path.getName</code>.
*
* @param path the directory and file name
* @return just the file name
*/
public static String getName(String path) {
return FilePath.get(path).getName();
}
/**
* List the files and directories in the given directory.
* This method is similar to Java 7
* <code>java.nio.file.Path.newDirectoryStream</code>.
*
* @param path the directory
* @return the list of fully qualified file names
*/
public static List<String> newDirectoryStream(String path) {
List<FilePath> list = FilePath.get(path).newDirectoryStream();
int len = list.size();
List<String> result = New.arrayList(len);
for (int i = 0; i < len; i++) {
result.add(list.get(i).toString());
}
return result;
}
/**
* Get the last modified date of a file.
* This method is similar to Java 7
* <code>java.nio.file.attribute.Attributes.
* readBasicFileAttributes(file).lastModified().toMillis()</code>
*
* @param fileName the file name
* @return the last modified date
*/
public static long lastModified(String fileName) {
return FilePath.get(fileName).lastModified();
}
/**
* Get the size of a file in bytes
* This method is similar to Java 7
* <code>java.nio.file.attribute.Attributes.
* readBasicFileAttributes(file).size()</code>
*
* @param fileName the file name
* @return the size in bytes
*/
public static long size(String fileName) {
return FilePath.get(fileName).size();
}
/**
* Check if it is a file or a directory.
* <code>java.nio.file.attribute.Attributes.
* readBasicFileAttributes(file).isDirectory()</code>
*
* @param fileName the file or directory name
* @return true if it is a directory
*/
public static boolean isDirectory(String fileName) {
return FilePath.get(fileName).isDirectory();
}
/**
* Open a random access file object.
* This method is similar to Java 7
* <code>java.nio.channels.FileChannel.open</code>.
*
* @param fileName the file name
* @param mode the access mode. Supported are r, rw, rws, rwd
* @return the file object
*/
public static FileChannel open(String fileName, String mode) throws IOException {
return FilePath.get(fileName).open(mode);
}
/**
* Create an input stream to read from the file.
* This method is similar to Java 7
* <code>java.nio.file.Path.newInputStream</code>.
*
* @param fileName the file name
* @return the input stream
*/
public static InputStream newInputStream(String fileName) throws IOException {
return FilePath.get(fileName).newInputStream();
}
/**
* Create an output stream to write into the file.
* This method is similar to Java 7
* <code>java.nio.file.Path.newOutputStream</code>.
*
* @param fileName the file name
* @param append if true, the file will grow, if false, the file will be
* truncated first
* @return the output stream
*/
public static OutputStream newOutputStream(String fileName, boolean append) throws IOException {
return FilePath.get(fileName).newOutputStream(append);
}
/**
* Check if the file is writable.
* This method is similar to Java 7
* <code>java.nio.file.Path.checkAccess(AccessMode.WRITE)</code>
*
* @param fileName the file name
* @return if the file is writable
*/
public static boolean canWrite(String fileName) {
return FilePath.get(fileName).canWrite();
}
// special methods =======================================
/**
* Disable the ability to write. The file can still be deleted afterwards.
*
* @param fileName the file name
* @return true if the call was successful
*/
public static boolean setReadOnly(String fileName) {
return FilePath.get(fileName).setReadOnly();
}
/**
* Get the unwrapped file name (without wrapper prefixes if wrapping /
* delegating file systems are used).
*
* @param fileName the file name
* @return the unwrapped
*/
public static String unwrap(String fileName) {
return FilePath.get(fileName).unwrap().toString();
}
// utility methods =======================================
/**
* Delete a directory or file and all subdirectories and files.
*
* @param path the path
* @param tryOnly whether errors should be ignored
*/
public static void deleteRecursive(String path, boolean tryOnly) {
if (exists(path)) {
if (isDirectory(path)) {
for (String s : newDirectoryStream(path)) {
deleteRecursive(s, tryOnly);
}
}
if (tryOnly) {
tryDelete(path);
} else {
delete(path);
}
}
}
/**
* Create the directory and all required parent directories.
*
* @param dir the directory name
*/
public static void createDirectories(String dir) {
if (dir != null) {
if (exists(dir)) {
if (!isDirectory(dir)) {
// this will fail
createDirectory(dir);
}
} else {
String parent = getParent(dir);
createDirectories(parent);
createDirectory(dir);
}
}
}
/**
* Try to delete a file (ignore errors).
*
* @param fileName the file name
* @return true if it worked
*/
public static boolean tryDelete(String fileName) {
try {
FilePath.get(fileName).delete();
return true;
} catch (Exception e) {
return false;
}
}
/**
* Create a new temporary file.
*
* @param prefix the prefix of the file name (including directory name if
* required)
* @param suffix the suffix
* @param deleteOnExit if the file should be deleted when the virtual
* machine exists
* @param inTempDir if the file should be stored in the temporary directory
* @return the name of the created file
*/
public static String createTempFile(String prefix, String suffix, boolean deleteOnExit, boolean inTempDir)
throws IOException {
return FilePath.get(prefix).createTempFile(suffix, deleteOnExit, inTempDir).toString();
}
/**
* Fully read from the file. This will read all remaining bytes,
* or throw an EOFException if not successful.
*
* @param channel the file channel
* @param dst the byte buffer
*/
public static void readFully(FileChannel channel, ByteBuffer dst) throws IOException {
do {
int r = channel.read(dst);
if (r < 0) {
throw new EOFException();
}
} while (dst.remaining() > 0);
}
/**
* Fully write to the file. This will write all remaining bytes.
*
* @param channel the file channel
* @param src the byte buffer
*/
public static void writeFully(FileChannel channel, ByteBuffer src) throws IOException {
do {
channel.write(src);
} while (src.remaining() > 0);
}
public static void closeQuietly(Closeable c) {
try {
if (c != null)
c.close();
} catch (Exception e) {
}
}
}