📄 fileutils.java
字号:
tick = 0;
if (timeout++ > seconds) {
return false;
}
}
try {
Thread.sleep(100);
} catch (InterruptedException ignore) {} catch (Exception ex) {
break;
}
}
return true;
}
/**
* <p>
* Reads the contents of a file into a String.
* </p>
* <p>
* There is no readFileToString method without encoding parameter because
* the default encoding can differ between platforms and therefore results
* in inconsistent results.
* </p>
*
* @param file the file to read.
* @param encoding the encoding to use
* @return The file contents or null if read failed.
* @throws IOException in case of an I/O error
* @throws UnsupportedEncodingException if the encoding is not supported
* by the VM
*/
public static String readFileToString(
File file, String encoding) throws IOException {
InputStream in = new java.io.FileInputStream(file);
try {
return IOUtils.toString(in, encoding);
} finally {
IOUtils.closeQuietly(in);
}
}
/**
* <p>
* Writes data to a file. The file will be created if it does not exist.
* </p>
* <p>
* There is no readFileToString method without encoding parameter because
* the default encoding can differ between platforms and therefore results
* in inconsistent results.
* </p>
*
* @param file the file to write.
* @param data The content to write to the file.
* @param encoding encoding to use
* @throws IOException in case of an I/O error
* @throws UnsupportedEncodingException if the encoding is not supported
* by the VM
*/
public static void writeStringToFile(File file,
String data, String encoding) throws IOException {
OutputStream out = new java.io.FileOutputStream(file);
try {
out.write(data.getBytes(encoding));
} finally {
IOUtils.closeQuietly(out);
}
}
/**
* <p>
* Delete a file. If file is a directory, delete it and all sub-directories.
* </p>
* <p>
* The difference between File.delete() and this method are:
* </p>
* <ul>
* <li>A directory to be deleted does not have to be empty.</li>
* <li>You get exceptions when a file or directory cannot be deleted.
* (java.io.File methods returns a boolean)</li>
* </ul>
* @param file file or directory to delete.
* @throws IOException in case deletion is unsuccessful
*/
public static void forceDelete(File file) throws IOException {
if (file.isDirectory()) {
deleteDirectory(file);
} else {
if (!file.exists()) {
throw new FileNotFoundException("File does not exist: " + file);
}
if (!file.delete()) {
String message =
"Unable to delete file: " + file;
throw new IOException(message);
}
}
}
/**
* Schedule a file to be deleted when JVM exits.
* If file is directory delete it and all sub-directories.
* @param file file or directory to delete.
* @throws IOException in case deletion is unsuccessful
*/
public static void forceDeleteOnExit(File file) throws IOException {
if (file.isDirectory()) {
deleteDirectoryOnExit(file);
} else {
file.deleteOnExit();
}
}
/**
* Recursively schedule directory for deletion on JVM exit.
* @param directory directory to delete.
* @throws IOException in case deletion is unsuccessful
*/
private static void deleteDirectoryOnExit(File directory)
throws IOException {
if (!directory.exists()) {
return;
}
cleanDirectoryOnExit(directory);
directory.deleteOnExit();
}
/**
* Clean a directory without deleting it.
* @param directory directory to clean.
* @throws IOException in case cleaning is unsuccessful
*/
private static void cleanDirectoryOnExit(File directory)
throws IOException {
if (!directory.exists()) {
String message = directory + " does not exist";
throw new IllegalArgumentException(message);
}
if (!directory.isDirectory()) {
String message = directory + " is not a directory";
throw new IllegalArgumentException(message);
}
IOException exception = null;
File[] files = directory.listFiles();
for (int i = 0; i < files.length; i++) {
File file = files[i];
try {
forceDeleteOnExit(file);
} catch (IOException ioe) {
exception = ioe;
}
}
if (null != exception) {
throw exception;
}
}
/**
* Make a directory. If there already exists a file with specified name or
* the directory cannot be created then an exception is thrown.
* @param directory directory to create
* @throws IOException if the directory cannot be created.
*/
public static void forceMkdir(File directory) throws IOException {
if (directory.exists()) {
if (directory.isFile()) {
String message =
"File "
+ directory
+ " exists and is "
+ "not a directory. Unable to create directory.";
throw new IOException(message);
}
} else {
if (false == directory.mkdirs()) {
String message =
"Unable to create directory " + directory;
throw new IOException(message);
}
}
}
/**
* Recursively count size of a directory (sum of the length of all files).
*
* @param directory directory to inspect
* @return size of directory in bytes.
*/
public static long sizeOfDirectory(File directory) {
if (!directory.exists()) {
String message = directory + " does not exist";
throw new IllegalArgumentException(message);
}
if (!directory.isDirectory()) {
String message = directory + " is not a directory";
throw new IllegalArgumentException(message);
}
long size = 0;
File[] files = directory.listFiles();
for (int i = 0; i < files.length; i++) {
File file = files[i];
if (file.isDirectory()) {
size += sizeOfDirectory(file);
} else {
size += file.length();
}
}
return size;
}
/**
* Tests if the specified <code>File</code> is newer than the reference
* <code>File</code>.
*
* @param file the <code>File</code> of which the modification date must be compared
* @param reference the <code>File</code> of which the modification date is used
* like reference
* @return true if the <code>File</code> exists and has been modified more recently
* than the reference <code>File</code>.
*/
public static boolean isFileNewer(File file, File reference) {
if (reference == null) {
throw new IllegalArgumentException("No specified reference file");
}
if (!reference.exists()) {
throw new IllegalArgumentException("The reference file '" + file + "' doesn't exist");
}
return isFileNewer(file, reference.lastModified());
}
/**
* Tests if the specified <code>File</code> is newer than the specified
* <code>Date</code>
*
* @param file the <code>File</code> of which the modification date must be compared
* @param date the date reference
* @return true if the <code>File</code> exists and has been modified after
* the given <code>Date</code>.
*/
public static boolean isFileNewer(File file, Date date) {
if (date == null) {
throw new IllegalArgumentException("No specified date");
}
return isFileNewer(file, date.getTime());
}
/**
* Tests if the specified <code>File</code> is newer than the specified
* time reference.
*
* @param file the <code>File</code> of which the modification date must be compared.
* @param timeMillis the time reference measured in milliseconds since the epoch
* (00:00:00 GMT, January 1, 1970)
* @return true if the <code>File</code> exists and has been modified after
* the given time reference.
*/
public static boolean isFileNewer(File file, long timeMillis) {
if (file == null) {
throw new IllegalArgumentException("No specified file");
}
if (!file.exists()) {
return false;
}
return file.lastModified() > timeMillis;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -