public class FileUtils extends Object
Facilities are provided in the following areas:
Origin of code: Excalibur, Alexandria, Commons-Utils
| Modifier and Type | Field and Description |
|---|---|
static File[] |
EMPTY_FILE_ARRAY
An empty array of type
File. |
static long |
ONE_GB
The number of bytes in a gigabyte.
|
static long |
ONE_KB
The number of bytes in a kilobyte.
|
static long |
ONE_MB
The number of bytes in a megabyte.
|
| Constructor and Description |
|---|
FileUtils()
Instances should NOT be constructed in standard programming.
|
| Modifier and Type | Method and Description |
|---|---|
static void |
cleanDirectory(File directory)
Cleans a directory without deleting it.
|
static boolean |
contentEquals(File file1,
File file2)
Compares the contents of two files to determine if they are equal or not.
|
static void |
copy(File file,
OutputStream out)
Copies the given file into an output stream.
|
static void |
copy(InputStream in,
File file)
Copies the given input stream into a file.
|
static void |
copyDirectory(File srcDir,
File destDir)
Copies a whole directory to a new location preserving the file dates.
|
static void |
copyDirectory(File srcDir,
File destDir,
boolean preserveFileDate)
Copies a whole directory to a new location.
|
static void |
copyDirectory(File srcDir,
File destDir,
FileFilter filter,
boolean preserveFileDate)
Copies a filtered directory to a new location.
|
static void |
copyFile(File srcFile,
File destFile)
Copies a file to a new location preserving the file date.
|
static void |
copyFile(File srcFile,
File destFile,
boolean preserveFileDate)
Copies a file to a new location.
|
static void |
copyFileToDirectory(File srcFile,
File destDir)
Copies a file to a directory preserving the file date.
|
static void |
copyFileToDirectory(File srcFile,
File destDir,
boolean preserveFileDate)
Copies a file to a directory optionally preserving the file date.
|
static void |
deleteDirectory(File directory)
Deletes a directory recursively.
|
static boolean |
deleteQuietly(File file)
Deletes a file, never throwing an exception.
|
static void |
forceDelete(File file)
Deletes a file.
|
static void |
forceDeleteOnExit(File file)
Schedules a file to be deleted when JVM exits.
|
static void |
forceMkdir(File directory)
Makes a directory, including any necessary but nonexistent parent
directories.
|
static File |
getTempFileFor(File file)
Find a non-existing file in the same directory using the same name as prefix.
|
static void |
moveDirectory(File srcDir,
File destDir)
Moves a directory.
|
static void |
moveFile(File srcFile,
File destFile)
Moves a file.
|
static FileInputStream |
openInputStream(File file)
Opens a
FileInputStream for the specified file, providing better
error messages than simply calling new FileInputStream(file). |
static FileOutputStream |
openOutputStream(File file)
Opens a
FileOutputStream for the specified file, checking and
creating the parent directory if it does not exist. |
static String |
readFileToString(File file)
Reads the contents of a file into a String using the default encoding for the VM.
|
static String |
readFileToString(File file,
String encoding)
Reads the contents of a file into a String.
|
static long |
sizeOfDirectory(File directory)
Counts the size of a directory recursively (sum of the length of all files).
|
public static final long ONE_KB
public static final long ONE_MB
public static final long ONE_GB
public static final File[] EMPTY_FILE_ARRAY
File.public FileUtils()
public static void copy(File file, OutputStream out) throws IOException
file - input file (must exist).out - output stream.IOExceptionpublic static void copy(InputStream in, File file) throws IOException
The target file must not be a directory and its parent must exist.
in - source stream.file - output file to be created or overwritten.IOExceptionpublic static File getTempFileFor(File file)
file - file used for the name and location (it is not read or written).public static FileInputStream openInputStream(File file) throws IOException
FileInputStream for the specified file, providing better
error messages than simply calling new FileInputStream(file).
At the end of the method either the stream will be successfully opened, or an exception will have been thrown.
An exception is thrown if the file does not exist. An exception is thrown if the file object exists but is a directory. An exception is thrown if the file exists but cannot be read.
file - the file to open for input, must not be nullFileInputStream for the specified fileFileNotFoundException - if the file does not existIOException - if the file object is a directoryIOException - if the file cannot be readpublic static FileOutputStream openOutputStream(File file) throws IOException
FileOutputStream for the specified file, checking and
creating the parent directory if it does not exist.
At the end of the method either the stream will be successfully opened, or an exception will have been thrown.
The parent directory will be created if it does not exist. The file will be created if it does not exist. An exception is thrown if the file object exists but is a directory. An exception is thrown if the file exists but cannot be written to. An exception is thrown if the parent directory cannot be created.
file - the file to open for output, must not be nullFileOutputStream for the specified fileIOException - if the file object is a directoryIOException - if the file cannot be written toIOException - if a parent directory needs creating but that failspublic static boolean contentEquals(File file1, File file2) throws IOException
This method checks to see if the two files are different lengths or if they point to the same file, before resorting to byte-by-byte comparison of the contents.
Code origin: Avalon
file1 - the first filefile2 - the second fileIOException - in case of an I/O errorpublic static void copyFileToDirectory(File srcFile, File destDir) throws IOException
This method copies the contents of the specified source file to a file of the same name in the specified destination directory. The destination directory is created if it does not exist. If the destination file exists, then this method will overwrite it.
srcFile - an existing file to copy, must not be nulldestDir - the directory to place the copy in, must not be nullNullPointerException - if source or destination is nullIOException - if source or destination is invalidIOException - if an IO error occurs during copyingcopyFile(File, File, boolean)public static void copyFileToDirectory(File srcFile, File destDir, boolean preserveFileDate) throws IOException
This method copies the contents of the specified source file to a file of the same name in the specified destination directory. The destination directory is created if it does not exist. If the destination file exists, then this method will overwrite it.
srcFile - an existing file to copy, must not be nulldestDir - the directory to place the copy in, must not be nullpreserveFileDate - true if the file date of the copy
should be the same as the originalNullPointerException - if source or destination is nullIOException - if source or destination is invalidIOException - if an IO error occurs during copyingcopyFile(File, File, boolean)public static void copyFile(File srcFile, File destFile) throws IOException
This method copies the contents of the specified source file to the specified destination file. The directory holding the destination file is created if it does not exist. If the destination file exists, then this method will overwrite it.
srcFile - an existing file to copy, must not be nulldestFile - the new file, must not be nullNullPointerException - if source or destination is nullIOException - if source or destination is invalidIOException - if an IO error occurs during copyingcopyFileToDirectory(File, File)public static void copyFile(File srcFile, File destFile, boolean preserveFileDate) throws IOException
This method copies the contents of the specified source file to the specified destination file. The directory holding the destination file is created if it does not exist. If the destination file exists, then this method will overwrite it.
srcFile - an existing file to copy, must not be nulldestFile - the new file, must not be nullpreserveFileDate - true if the file date of the copy
should be the same as the originalNullPointerException - if source or destination is nullIOException - if source or destination is invalidIOException - if an IO error occurs during copyingcopyFileToDirectory(File, File, boolean)public static void copyDirectory(File srcDir, File destDir) throws IOException
This method copies the specified directory and all its child directories and files to the specified destination. The destination is the new location and name of the directory.
The destination directory is created if it does not exist. If the destination directory did exist, then this method merges the source with the destination, with the source taking precedence.
srcDir - an existing directory to copy, must not be nulldestDir - the new directory, must not be nullNullPointerException - if source or destination is nullIOException - if source or destination is invalidIOException - if an IO error occurs during copyingpublic static void copyDirectory(File srcDir, File destDir, boolean preserveFileDate) throws IOException
This method copies the contents of the specified source directory to within the specified destination directory.
The destination directory is created if it does not exist. If the destination directory did exist, then this method merges the source with the destination, with the source taking precedence.
srcDir - an existing directory to copy, must not be nulldestDir - the new directory, must not be nullpreserveFileDate - true if the file date of the copy
should be the same as the originalNullPointerException - if source or destination is nullIOException - if source or destination is invalidIOException - if an IO error occurs during copyingpublic static void copyDirectory(File srcDir, File destDir, FileFilter filter, boolean preserveFileDate) throws IOException
This method copies the contents of the specified source directory to within the specified destination directory.
The destination directory is created if it does not exist. If the destination directory did exist, then this method merges the source with the destination, with the source taking precedence.
// only copy the directory structure FileUtils.copyDirectory(srcDir, destDir, DirectoryFileFilter.DIRECTORY, false);
// Create a filter for ".txt" files
IOFileFilter txtSuffixFilter = FileFilterUtils.suffixFileFilter(".txt");
IOFileFilter txtFiles = FileFilterUtils.andFileFilter(FileFileFilter.FILE, txtSuffixFilter);
// Create a filter for either directories or ".txt" files
FileFilter filter = FileFilterUtils.orFileFilter(DirectoryFileFilter.DIRECTORY, txtFiles);
// Copy using the filter
FileUtils.copyDirectory(srcDir, destDir, filter, false);
srcDir - an existing directory to copy, must not be nulldestDir - the new directory, must not be nullfilter - the filter to apply, null means copy all directories and filespreserveFileDate - true if the file date of the copy
should be the same as the originalNullPointerException - if source or destination is nullIOException - if source or destination is invalidIOException - if an IO error occurs during copyingpublic static void deleteDirectory(File directory) throws IOException
directory - directory to deleteIOException - in case deletion is unsuccessfulpublic static boolean deleteQuietly(File file)
The difference between File.delete() and this method are:
file - file or directory to delete, can be nulltrue if the file or directory was deleted, otherwise falsepublic static void cleanDirectory(File directory) throws IOException
directory - directory to cleanIOException - in case cleaning is unsuccessfulpublic static String readFileToString(File file, String encoding) throws IOException
file - the file to read, must not be nullencoding - the encoding to use, null means platform defaultnullIOException - in case of an I/O errorUnsupportedEncodingException - if the encoding is not supported by the VMpublic static String readFileToString(File file) throws IOException
file - the file to read, must not be nullnullIOException - in case of an I/O errorpublic static void forceDelete(File file) throws IOException
The difference between File.delete() and this method are:
file - file or directory to delete, must not be nullNullPointerException - if the directory is nullFileNotFoundException - if the file was not foundIOException - in case deletion is unsuccessfulpublic static void forceDeleteOnExit(File file) throws IOException
file - file or directory to delete, must not be nullNullPointerException - if the file is nullIOException - in case deletion is unsuccessfulpublic static void forceMkdir(File directory) throws IOException
directory - directory to create, must not be nullNullPointerException - if the directory is nullIOException - if the directory cannot be createdpublic static long sizeOfDirectory(File directory)
directory - directory to inspect, must not be nullNullPointerException - if the directory is nullpublic static void moveDirectory(File srcDir, File destDir) throws IOException
When the destination directory is on another file system, do a "copy and delete".
srcDir - the directory to be moveddestDir - the destination directoryNullPointerException - if source or destination is nullIOException - if source or destination is invalidIOException - if an IO error occurs moving the filepublic static void moveFile(File srcFile, File destFile) throws IOException
When the destination file is on another file system, do a "copy and delete".
srcFile - the file to be moveddestFile - the destination fileNullPointerException - if source or destination is nullIOException - if source or destination is invalidIOException - if an IO error occurs moving the fileCopyright © 2014 ZeroTurnaround. All rights reserved.