public class FileUtils extends Object
| Modifier and Type | Method and Description |
|---|---|
static File |
absolutise(File file)
Checks if a
File is really a java.io.File, and
if not converts it to one using File.getAbsolutePath(). |
static <T extends Collection<File>> |
absolutise(T collection)
Ensures that a collection of
Files only contains instances of
java.io.File and not subclasses, by converting subclasses
using absolutise(File). |
static <T extends Map<?,?>> |
absolutise(T map)
Ensures that a map with
Files as keys and/or values only contains
instances of java.io.File and not subclasses, by converting
subclasses using absolutise(File). |
static void |
assertEquals(File expected,
File actual)
Asserts that the contents of two files or directories are equal.
|
static void |
copyDir(File dir,
File destDir)
Recursively copy a directory including all contents.
|
static void |
copyFileToDir(File file,
File destDir)
Copy a file to another directory.
|
static void |
copyFileToDir(File file,
File destDir,
ProgressReceiver progressReceiver)
Copy a file to another directory with optional progress reporting.
|
static void |
copyFileToFile(File file,
File destFile,
boolean overwrite)
Copy a file to another file.
|
static boolean |
deleteDir(File dir)
Recursively delete a directory and all its contents.
|
static boolean |
emptyDir(File dir)
Recursively delete all contents of a directory.
|
static long |
getFreeSpace(File path)
Determine the free space on the file system containing a specific file or
directory.
|
static Checksum |
getMD5(File file) |
static long |
getTreeSize(File dir)
Calculate the total size of the files in the specified directory,
recursively.
|
static String |
load(File file,
Charset charset) |
static String |
load(InputStream inputStream,
Charset charset) |
static void |
main(String[] args) |
static void |
rotateFile(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 String |
sanitiseName(@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 String |
stripDirectory(String path)
Strip the directory from a path, if any.
|
static String |
stripExtension(String filename)
Strip the extension off a filename, if any.
|
static void |
visitFilesRecursively(File directory,
Consumer<File> visitor)
Visit all files contained in a directory, recursively.
|
public static Checksum getMD5(File file) throws IOException
IOExceptionpublic static String load(File file, Charset charset) throws IOException
IOExceptionpublic static String load(InputStream inputStream, Charset charset) throws IOException
IOExceptionpublic static void copyDir(File dir, File destDir) throws IOException
dir - The directory to copy. Must exist.destDir - The directory into which to copy the contents of
dir. Must not exist yet and will be created.IOException - If there is an I/O error while performing the copy.public static void copyFileToFile(File file, File destFile, boolean overwrite) throws IOException
file - The file to copy.destFile - The file to copy the file to. If overwrite
is false it must not exist yet. In either
case it may not be an existing directory.overwrite - Whether destFile should be overwritten if
it already exists. If this is false and the file does
already exist an IllegalStateException will be
thrown.IOException - If there is an I/O error while performing the copy.IllegalStateException - If overwrite was
false and destFile already existed.public static void copyFileToDir(File file, File destDir) throws IOException
file - The file to copy.destDir - The directory to copy the file into, using the same name
as the source file.IOException - If there is an I/O error while performing the copy.public static void copyFileToDir(File file, File destDir, ProgressReceiver progressReceiver) throws IOException, ProgressReceiver.OperationCancelled
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 be null.IOException - If there is an I/O error while performing the copy.ProgressReceiver.OperationCancelledpublic static boolean deleteDir(File dir)
dir - The directory to delete.true if and only if the directory is successfully deleted; false otherwisepublic static boolean emptyDir(File dir)
dir - The directory to empty.true if and only if all contents of the directory
were successfully deleted; false otherwisepublic static String sanitiseName(@NonNls @NonNls String filename)
filename - The filename to sanitise.public static File absolutise(File file)
File is really a java.io.File, and
if not converts it to one using File.getAbsolutePath().file - The file to absolutise. May be null.java.io.File, or null if the input was
null.public static <T extends Collection<File>> T absolutise(T collection)
Files only contains instances of
java.io.File and not subclasses, by converting subclasses
using absolutise(File). The collection is transformed in-place
if possible; otherwise a new collection with the same basic
characteristics is created.T - The type of collection.collection - The collection to transform.public static <T extends Map<?,?>> T absolutise(T map)
Files as keys and/or values only contains
instances of java.io.File and not subclasses, by converting
subclasses using absolutise(File). The map is transformed
in-place if possible; otherwise a new map with the same basic
characteristics is created.T - The type of map.map - The map to transform.public static void rotateFile(File file, String namePattern, int index, int maxIndex) throws IOException
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.IOException - If a file could not be renamed or deleted.public static void assertEquals(File expected, File actual) throws IOException
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.
expected - The file or directory that is expected.actual - The actual file or directory.AssertionError - If the specified files or directories are not
equal.IOException - If an I/O error occurred while comparing the files or
directories.public static long getTreeSize(File dir)
dir - The directory of which to calculate the total size.public static long getFreeSpace(File path) throws IOException
path - The file or directory for which to determine its containing
filesystem's free space.IOException - If an I/O error occurs while determining the free
space.public static String stripExtension(String filename)
filename - The filename to strip.public static String stripDirectory(String path)
path - The path to strip.public static void visitFilesRecursively(File directory, Consumer<File> visitor) throws IOException
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.
directory - The directory to visit.visitor - The visitor that will be invoked for every file in the specified directory, recursively.IOExceptionpublic static void main(String[] args) throws IOException
IOExceptionCopyright © 2011–2023 pepsoft.org. All rights reserved.