Class FileUtils
- java.lang.Object
-
- com.therouter.plugin.utils.FileUtils
-
public class FileUtils extends java.lang.ObjectGeneral file manipulation utilities.Facilities are provided in the following areas:
- writing to a file
- reading from a file
- make a directory including parent directories
- copying files and directories
- deleting files and directories
- converting to and from a URL
- listing files and directories by filter and extension
- comparing file content
- file last changed date
- calculating a checksum
Note that a specific charset should be specified whenever possible. Relying on the platform default means that the code is Locale-dependent. Only use the default if the files are known to always use the platform default.
Origin of code: Excalibur, Alexandria, Commons-Utils
-
-
Constructor Summary
Constructors Constructor Description FileUtils()Instances should NOT be constructed in standard programming.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidcleanDirectory(java.io.File directory)Cleans a directory without deleting it.static voidcopyDirectory(java.io.File srcDir, java.io.File destDir)Copies a whole directory to a new location preserving the file dates.static voidcopyDirectory(java.io.File srcDir, java.io.File destDir, boolean preserveFileDate)Copies a whole directory to a new location.static voidcopyDirectory(java.io.File srcDir, java.io.File destDir, java.io.FileFilter filter)Copies a filtered directory to a new location preserving the file dates.static voidcopyDirectory(java.io.File srcDir, java.io.File destDir, java.io.FileFilter filter, boolean preserveFileDate)Copies a filtered directory to a new location.static voidcopyFile(java.io.File srcFile, java.io.File destFile)Copies a file to a new location preserving the file date.static voidcopyFile(java.io.File srcFile, java.io.File destFile, boolean preserveFileDate)Copies a file to a new location.static voidcopyFileToDirectory(java.io.File srcFile, java.io.File destDir)Copies a file to a directory preserving the file date.static voidcopyFileToDirectory(java.io.File srcFile, java.io.File destDir, boolean preserveFileDate)Copies a file to a directory optionally preserving the file date.static voiddeleteDirectory(java.io.File directory)Deletes a directory recursively.static voidforceDelete(java.io.File file)Deletes a file.static voidforceDeleteOnExit(java.io.File file)Schedules a file to be deleted when JVM exits.static voidforceMkdir(java.io.File directory)Makes a directory, including any necessary but nonexistent parent directories.static booleanisSymlink(java.io.File file)static java.io.FileOutputStreamopenOutputStream(java.io.File file)Opens aFileOutputStreamfor the specified file, checking and creating the parent directory if it does not exist.static java.io.FileOutputStreamopenOutputStream(java.io.File file, boolean append)Opens aFileOutputStreamfor the specified file, checking and creating the parent directory if it does not exist.
-
-
-
Field Detail
-
ONE_KB
public static final long ONE_KB
The number of bytes in a kilobyte.- See Also:
- Constant Field Values
-
ONE_MB
public static final long ONE_MB
The number of bytes in a megabyte.- See Also:
- Constant Field Values
-
-
Method Detail
-
openOutputStream
public static java.io.FileOutputStream openOutputStream(java.io.File file) throws java.io.IOExceptionOpens aFileOutputStreamfor 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.
- Parameters:
file- the file to open for output, must not benull- Returns:
- a new
FileOutputStreamfor the specified file - Throws:
java.io.IOException- if the file object is a directoryjava.io.IOException- if the file cannot be written tojava.io.IOException- if a parent directory needs creating but that fails- Since:
- 1.3
-
openOutputStream
public static java.io.FileOutputStream openOutputStream(java.io.File file, boolean append) throws java.io.IOExceptionOpens aFileOutputStreamfor 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.
- Parameters:
file- the file to open for output, must not benullappend- iftrue, then bytes will be added to the end of the file rather than overwriting- Returns:
- a new
FileOutputStreamfor the specified file - Throws:
java.io.IOException- if the file object is a directoryjava.io.IOException- if the file cannot be written tojava.io.IOException- if a parent directory needs creating but that fails- Since:
- 2.1
-
copyFileToDirectory
public static void copyFileToDirectory(java.io.File srcFile, java.io.File destDir) throws java.io.IOExceptionCopies a file to a directory preserving the file date.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.
Note: This method tries to preserve the file's last modified date/times using
File.setLastModified(long), however it is not guaranteed that the operation will succeed. If the modification operation fails, no indication is provided.- Parameters:
srcFile- an existing file to copy, must not benulldestDir- the directory to place the copy in, must not benull- Throws:
java.lang.NullPointerException- if source or destination is nulljava.io.IOException- if source or destination is invalidjava.io.IOException- if an IO error occurs during copying- See Also:
copyFile(File, File, boolean)
-
copyFileToDirectory
public static void copyFileToDirectory(java.io.File srcFile, java.io.File destDir, boolean preserveFileDate) throws java.io.IOExceptionCopies a file to a directory optionally preserving the file date.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.
Note: Setting
preserveFileDatetotruetries to preserve the file's last modified date/times usingFile.setLastModified(long), however it is not guaranteed that the operation will succeed. If the modification operation fails, no indication is provided.- Parameters:
srcFile- an existing file to copy, must not benulldestDir- the directory to place the copy in, must not benullpreserveFileDate- true if the file date of the copy should be the same as the original- Throws:
java.lang.NullPointerException- if source or destination isnulljava.io.IOException- if source or destination is invalidjava.io.IOException- if an IO error occurs during copyingjava.io.IOException- if the output file length is not the same as the input file length after the copy completes- Since:
- 1.3
- See Also:
copyFile(File, File, boolean)
-
copyFile
public static void copyFile(java.io.File srcFile, java.io.File destFile) throws java.io.IOExceptionCopies a file to a new location preserving the file date.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.
Note: This method tries to preserve the file's last modified date/times using
File.setLastModified(long), however it is not guaranteed that the operation will succeed. If the modification operation fails, no indication is provided.- Parameters:
srcFile- an existing file to copy, must not benulldestFile- the new file, must not benull- Throws:
java.lang.NullPointerException- if source or destination isnulljava.io.IOException- if source or destination is invalidjava.io.IOException- if an IO error occurs during copyingjava.io.IOException- if the output file length is not the same as the input file length after the copy completes- See Also:
copyFileToDirectory(File, File),copyFile(File, File, boolean)
-
copyFile
public static void copyFile(java.io.File srcFile, java.io.File destFile, boolean preserveFileDate) throws java.io.IOExceptionCopies a file to a new location.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.
Note: Setting
preserveFileDatetotruetries to preserve the file's last modified date/times usingFile.setLastModified(long), however it is not guaranteed that the operation will succeed. If the modification operation fails, no indication is provided.- Parameters:
srcFile- an existing file to copy, must not benulldestFile- the new file, must not benullpreserveFileDate- true if the file date of the copy should be the same as the original- Throws:
java.lang.NullPointerException- if source or destination isnulljava.io.IOException- if source or destination is invalidjava.io.IOException- if an IO error occurs during copyingjava.io.IOException- if the output file length is not the same as the input file length after the copy completes- See Also:
copyFileToDirectory(File, File, boolean),doCopyFile(File, File, boolean)
-
copyDirectory
public static void copyDirectory(java.io.File srcDir, java.io.File destDir) throws java.io.IOExceptionCopies a whole directory to a new location preserving the file dates.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.
Note: This method tries to preserve the files' last modified date/times using
File.setLastModified(long), however it is not guaranteed that those operations will succeed. If the modification operation fails, no indication is provided.- Parameters:
srcDir- an existing directory to copy, must not benulldestDir- the new directory, must not benull- Throws:
java.lang.NullPointerException- if source or destination isnulljava.io.IOException- if source or destination is invalidjava.io.IOException- if an IO error occurs during copying- Since:
- 1.1
-
copyDirectory
public static void copyDirectory(java.io.File srcDir, java.io.File destDir, boolean preserveFileDate) throws java.io.IOExceptionCopies a whole directory to a new location.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.
Note: Setting
preserveFileDatetotruetries to preserve the files' last modified date/times usingFile.setLastModified(long), however it is not guaranteed that those operations will succeed. If the modification operation fails, no indication is provided.- Parameters:
srcDir- an existing directory to copy, must not benulldestDir- the new directory, must not benullpreserveFileDate- true if the file date of the copy should be the same as the original- Throws:
java.lang.NullPointerException- if source or destination isnulljava.io.IOException- if source or destination is invalidjava.io.IOException- if an IO error occurs during copying- Since:
- 1.1
-
copyDirectory
public static void copyDirectory(java.io.File srcDir, java.io.File destDir, java.io.FileFilter filter) throws java.io.IOExceptionCopies a filtered directory to a new location preserving the file dates.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.
Note: This method tries to preserve the files' last modified date/times using
File.setLastModified(long), however it is not guaranteed that those operations will succeed. If the modification operation fails, no indication is provided.// only copy the directory structure FileUtils.copyDirectory(srcDir, destDir, DirectoryFileFilter.DIRECTORY);
// 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);- Parameters:
srcDir- an existing directory to copy, must not benulldestDir- the new directory, must not benullfilter- the filter to apply, null means copy all directories and files should be the same as the original- Throws:
java.lang.NullPointerException- if source or destination isnulljava.io.IOException- if source or destination is invalidjava.io.IOException- if an IO error occurs during copying- Since:
- 1.4
-
copyDirectory
public static void copyDirectory(java.io.File srcDir, java.io.File destDir, java.io.FileFilter filter, boolean preserveFileDate) throws java.io.IOExceptionCopies a filtered directory to a new location.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.
Note: Setting
preserveFileDatetotruetries to preserve the files' last modified date/times usingFile.setLastModified(long), however it is not guaranteed that those operations will succeed. If the modification operation fails, no indication is provided.// 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);- Parameters:
srcDir- an existing directory to copy, must not benulldestDir- the new directory, must not benullfilter- 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 original- Throws:
java.lang.NullPointerException- if source or destination isnulljava.io.IOException- if source or destination is invalidjava.io.IOException- if an IO error occurs during copying- Since:
- 1.4
-
deleteDirectory
public static void deleteDirectory(java.io.File directory) throws java.io.IOExceptionDeletes a directory recursively.- Parameters:
directory- directory to delete- Throws:
java.io.IOException- in case deletion is unsuccessfuljava.lang.IllegalArgumentException- ifdirectorydoes not exist or is not a directory
-
cleanDirectory
public static void cleanDirectory(java.io.File directory) throws java.io.IOExceptionCleans a directory without deleting it.- Parameters:
directory- directory to clean- Throws:
java.io.IOException- in case cleaning is unsuccessfuljava.lang.IllegalArgumentException- ifdirectorydoes not exist or is not a directory
-
forceDelete
public static void forceDelete(java.io.File file) throws java.io.IOExceptionDeletes a file. If file is a directory, delete it and all sub-directories.The difference between File.delete() and this method are:
- A directory to be deleted does not have to be empty.
- You get exceptions when a file or directory cannot be deleted. (java.io.File methods returns a boolean)
- Parameters:
file- file or directory to delete, must not benull- Throws:
java.lang.NullPointerException- if the directory isnulljava.io.FileNotFoundException- if the file was not foundjava.io.IOException- in case deletion is unsuccessful
-
forceDeleteOnExit
public static void forceDeleteOnExit(java.io.File file) throws java.io.IOExceptionSchedules a file to be deleted when JVM exits. If file is directory delete it and all sub-directories.- Parameters:
file- file or directory to delete, must not benull- Throws:
java.lang.NullPointerException- if the file isnulljava.io.IOException- in case deletion is unsuccessful
-
forceMkdir
public static void forceMkdir(java.io.File directory) throws java.io.IOExceptionMakes a directory, including any necessary but nonexistent parent directories. If a file already exists with specified name but it is not a directory then an IOException is thrown. If the directory cannot be created (or does not already exist) then an IOException is thrown.- Parameters:
directory- directory to create, must not benull- Throws:
java.lang.NullPointerException- if the directory isnulljava.io.IOException- if the directory cannot be created or the file already exists but is not a directory
-
isSymlink
public static boolean isSymlink(java.io.File file) throws java.io.IOException- Throws:
java.io.IOException
-
-