public final class Files extends Object
| Modifier and Type | Field and Description |
|---|---|
static String |
HOME_ALIAS |
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.
|
static String |
SLASH |
| Constructor and Description |
|---|
Files() |
| Modifier and Type | Method and Description |
|---|---|
static String |
canonicalize(String target)
Replace instances of internal tokens with actual file equivalents.
|
static void |
copyDirectory(File srcDir,
File destDir)
Copies a whole directory to a new location.
|
static void |
copyDirectory(File srcDir,
File destDir,
FileFilter filter)
Copies a filtered directory to a new location.
|
static void |
copyFile(File srcFile,
File destFile)
Copies a file to a new location.
|
static void |
copyFileToDirectory(File srcFile,
File destDir)
Copies a file to a directory optionally preserving the file date.
|
static boolean |
delete(File file) |
static boolean |
delete(File file,
boolean recursive) |
static void |
deleteOnExit(File file)
Requests that the file or directory denoted by this resource be deleted when the virtual machine terminates.
|
static File |
getWorkingDirectory() |
public static final String HOME_ALIAS
public static final String SLASH
public static final long ONE_KB
public static final long ONE_MB
public static final long ONE_GB
public static boolean delete(File file)
public static void deleteOnExit(File file)
Once deletion has been requested, it is not possible to cancel the request. This method should therefore be used with care.
public static boolean delete(File file, boolean recursive)
public static String canonicalize(String target)
public static File getWorkingDirectory()
public 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.
Note: Setting preserveFileDate to true 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.
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)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 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 copying#copyFileToDirectory(File, File, boolean)public static void copyDirectory(File srcDir, File destDir) 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.
Note: Setting preserveFileDate to true 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.
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, FileFilter filter) 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.
Note: Setting preserveFileDate to true 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, 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 copyingCopyright © 2013 OCPsoft. All Rights Reserved.