Package org.pepsoft.util
Class FileUtils
java.lang.Object
org.pepsoft.util.FileUtils
-
Method Summary
Modifier and TypeMethodDescriptionstatic Fileabsolutise(File file) Checks if aFileis really ajava.io.File, and if not converts it to one usingFile.getAbsolutePath().static <T extends Collection<File>>
Tabsolutise(T collection) Ensures that a collection ofFiles only contains instances ofjava.io.Fileand not subclasses, by converting subclasses usingabsolutise(File).static voidassertEquals(File expected, File actual) Asserts that the contents of two files or directories are equal.static voidRecursively copy a directory including all contents.static voidcopyFileToDir(File file, File destDir) Copy a file to another directory.static voidcopyFileToDir(File file, File destDir, ProgressReceiver progressReceiver) Copy a file to another directory with optional progress reporting.static voidcopyFileToFile(File file, File destFile, boolean overwrite) Copy a file to another file.static booleanRecursively delete a directory and all its contents.static booleanRecursively delete all contents of a directory.static longgetFreeSpace(File path) Determine the free space on the file system containing a specific file or directory.static Checksumstatic longgetTreeSize(File dir) Calculate the total size of the files in the specified directory, recursively.static Stringstatic Stringload(InputStream inputStream, Charset charset) static voidstatic voidrotateFile(File file, String namePattern, int index, int maxIndex) Recursively move a file out of the way by using increasing index numbers, renaming each file to the next number and deleting the last one.static StringsanitiseName(@NonNls String filename) Sanitises a filename by replacing characters which are illegal for Windows, Linux or Mac OS filenames with underscores and enforcing other rules.static StringstripDirectory(String path) Strip the directory from a path, if any.static StringstripExtension(String filename) Strip the extension off a filename, if any.static voidvisitFilesRecursively(File directory, Consumer<File> visitor) Visit all files contained in a directory, recursively.
-
Method Details
-
getMD5
- Throws:
IOException
-
load
- Throws:
IOException
-
load
- Throws:
IOException
-
copyDir
Recursively copy a directory including all contents.- Parameters:
dir- The directory to copy. Must exist.destDir- The directory into which to copy the contents ofdir. Must not exist yet and will be created.- Throws:
IOException- If there is an I/O error while performing the copy.
-
copyFileToFile
Copy a file to another file.- Parameters:
file- The file to copy.destFile- The file to copy the file to. Ifoverwriteisfalseit must not exist yet. In either case it may not be an existing directory.overwrite- WhetherdestFileshould be overwritten if it already exists. If this is false and the file does already exist anIllegalStateExceptionwill be thrown.- Throws:
IOException- If there is an I/O error while performing the copy.IllegalStateException- IfoverwritewasfalseanddestFilealready existed.
-
copyFileToDir
Copy a file to another directory.- Parameters:
file- The file to copy.destDir- The directory to copy the file into, using the same name as the source file.- Throws:
IOException- If there is an I/O error while performing the copy.
-
copyFileToDir
public static void copyFileToDir(File file, File destDir, ProgressReceiver progressReceiver) throws IOException, ProgressReceiver.OperationCancelled Copy a file to another directory with optional progress reporting.- Parameters:
file- The file to copy.destDir- The directory to copy the file into, using the same name as the source file.progressReceiver- The progress receiver to report copying progress to. May benull.- Throws:
IOException- If there is an I/O error while performing the copy.ProgressReceiver.OperationCancelled
-
deleteDir
Recursively delete a directory and all its contents.- Parameters:
dir- The directory to delete.- Returns:
trueif and only if the directory is successfully deleted;falseotherwise
-
emptyDir
Recursively delete all contents of a directory.- Parameters:
dir- The directory to empty.- Returns:
trueif and only if all contents of the directory were successfully deleted;falseotherwise
-
sanitiseName
Sanitises a filename by replacing characters which are illegal for Windows, Linux or Mac OS filenames with underscores and enforcing other rules.- Parameters:
filename- The filename to sanitise.- Returns:
- The sanitised filename.
-
absolutise
Checks if aFileis really ajava.io.File, and if not converts it to one usingFile.getAbsolutePath().- Parameters:
file- The file to absolutise. May benull.- Returns:
- A file with the same absolute path as the input and guaranteed to
be of class
java.io.File, ornullif the input wasnull.
-
absolutise
Ensures that a collection ofFiles only contains instances ofjava.io.Fileand not subclasses, by converting subclasses usingabsolutise(File). The collection is transformed in-place if possible; otherwise a new collection with the same basic characteristics is created.- Type Parameters:
T- The type of collection.- Parameters:
collection- The collection to transform.- Returns:
- Either the same collection, transformed in-place, or a new collection of the same basic type as the input containing the transformed results.
-
absolutise
Ensures that a map withFiles as keys and/or values only contains instances ofjava.io.Fileand not subclasses, by converting subclasses usingabsolutise(File). The map is transformed in-place if possible; otherwise a new map with the same basic characteristics is created.- Type Parameters:
T- The type of map.- Parameters:
map- The map to transform.- Returns:
- Either the same map, transformed in-place, or a new map of the same basic type as the input containing the transformed results.
-
rotateFile
public static void rotateFile(File file, String namePattern, int index, int maxIndex) throws IOException Recursively move a file out of the way by using increasing index numbers, renaming each file to the next number and deleting the last one.- Parameters:
file- The file to rotate or delete.namePattern- The pattern to use to create an indexed filename. Must contain a"{0}"which will be replaced with the index number.index- The index of the current file.maxIndex- The maximum index which should exist. Beyond this the file will be deleted instead of renamed.- Throws:
IOException- If a file could not be renamed or deleted.
-
assertEquals
Asserts that the contents of two files or directories are equal.Files have to have the same length and have the exact same contents.
Directories have to contain the same files and directories and are compared recursively.
- Parameters:
expected- The file or directory that is expected.actual- The actual file or directory.- Throws:
AssertionError- If the specified files or directories are not equal.IOException- If an I/O error occurred while comparing the files or directories.
-
getTreeSize
Calculate the total size of the files in the specified directory, recursively. Note that this method does not take the size of directories into account, nor the block or sector size of the filesystem.- Parameters:
dir- The directory of which to calculate the total size.- Returns:
- The total size of the files in the specified directory and all its subdirectories, in bytes.
-
getFreeSpace
Determine the free space on the file system containing a specific file or directory.- Parameters:
path- The file or directory for which to determine its containing filesystem's free space.- Returns:
- The free space on the specified path's filesystem in bytes.
- Throws:
IOException- If an I/O error occurs while determining the free space.
-
stripExtension
Strip the extension off a filename, if any.- Parameters:
filename- The filename to strip.- Returns:
- The filename without its extension, or the original filename if it has no extension.
-
stripDirectory
Strip the directory from a path, if any.- Parameters:
path- The path to strip.- Returns:
- The path without the directory, or the original path if it was not in a directory.
-
visitFilesRecursively
Visit all files contained in a directory, recursively. The order is undefined.This method tries to guard against endless loops caused by symbolic or hard links creating a cycle, by keeping track of the canonical path of all visited directories.
- Parameters:
directory- The directory to visit.visitor- The visitor that will be invoked for every file in the specified directory, recursively.- Throws:
IOException
-
main
- Throws:
IOException
-