@ThreadSafe public final class FileX extends Object
Modifier and Type | Field and Description |
---|---|
static Pattern |
BODY_DOTX_PATTERN
A pattern to split a filename (or path) into two groups: body and dot-extension.
|
Modifier and Type | Method and Description |
---|---|
static Appendable |
appendTo(Appendable a,
File file,
Charset fileCharset)
Appends the entire file to the specified appendable.
|
static void |
copyAs(File target,
File source)
Copies a file or directory to a new file.
|
static void |
copyAs(File target,
File source,
FileFilter fileFilter)
Copies a file or directory to a new file.
|
static void |
copyTo(File targetDirectory,
File source)
Copies a file or directory to a directory.
|
static void |
copyTo(File targetDirectory,
File source,
FileFilter fileFilter)
Copies a file or directory to a directory.
|
static boolean |
createNewDirectory(File directory)
Atomically creates a new, empty directory if and only if the directory does not
yet exist.
|
static boolean |
deleteRecursive(File fileOrDirectory)
The same as delete() except it works with non-empty
directories.
|
static boolean |
deleteRecursiveFrom(File directory)
The same as
deleteRecursive (), but it works only on
the contents of the specified directory. |
static void |
deleteRecursiveSure(File fileOrDirectory)
The same as deleteRecursive() except it throws
an exception if it fails.
|
static void |
ensuredirs(File directory)
Ensures that the specified directory is created, including any necessary but
nonexistent parent directories.
|
static File[] |
listFilesNoNull(File file)
The same as listFiles(), but it never returns null.
|
static File[] |
listFilesNoNull(File file,
FileFilter filter)
The same as listFiles(filter), but it
never returns null.
|
static String[] |
listNoNull(File file)
The same as list(), but it never returns null.
|
static String[] |
listNoNull(File file,
FilenameFilter filter)
The same as list(filter), but it never
returns null.
|
static Object |
readObject(File file)
Deserializes an object from a file.
|
static void |
renameFromDefaultToMv(File oldFile,
File newFile)
Renames a file, defaulting to a Unix
mv command if it fails. |
static void |
symlink(String targetPath,
String linkPath)
Creates a new symbolic link at
linkPath , pointing to
targetPath . |
static boolean |
testsCaseSensitive()
Tests whether or not the file system is case sensitive, and returns true iff it
is.
|
static boolean |
traverse(File bottomFile,
FileFilter upFilter,
FileFilter downFilter)
Traverses the file and its ancestors; first from the bottom up, then from the top
down.
|
static boolean |
traverseDown(File topFile,
FileFilter filter)
Traverses the file and its descendants from the top down, beginning with the
topmost file itself.
|
static void |
traverseUp(File bottomFile,
FileFilter filter)
Traverses the file and its ancestors from the bottom up, beginning with the
bottom-most file itself.
|
static void |
writeObject(Object object,
File file)
Serializes an object to a file.
|
public static final Pattern BODY_DOTX_PATTERN
The extension group (2) will either include the preceding dot '.', or it will be empty. It may comprise a single dot. It will not be null.
public static Appendable appendTo(Appendable a, File file, Charset fileCharset) throws IOException
IOException
public static void copyAs(File target, File source) throws IOException
target
- pathname to create as copy. It will be overwritten if it
already exists.source
- file or directory to copy. Directory contents are recursively
copied using copyTo
.IOException
public static void copyAs(File target, File source, FileFilter fileFilter) throws IOException
target
- pathname to create as copy. It will be overwritten if it
already exists.source
- file or directory to copy. Directory contents are recursively
copied using copyTo
.fileFilter
- the filter to use in case the source is a directory. The
filter is applied to the content of the source directory, and recursively to
the content of any sub-directories that themselves pass the filter.IOException
public static void copyTo(File targetDirectory, File source) throws IOException
targetDirectory
- in which to make copy. It must already exist.source
- file or directory to copy. A directory is recursively copied,
overwriting any existing file or directory of the same name.IOException
public static void copyTo(File targetDirectory, File source, FileFilter fileFilter) throws IOException
targetDirectory
- the directory in which to make the copy. It must
already exist.source
- file or directory to copy. A directory is recursively copied,
overwriting any existing file or directory of the same name.fileFilter
- the filter to use in case the source is a directory. The
filter is applied to the content of the source directory, and recursively to
the content of any sub-directories that themselves pass the filter.IOException
public static boolean createNewDirectory(File directory) throws IOException
directory
- the directory to create, if it does not already exist.IOException
- if the directory did not exist, and could not be created.public static boolean deleteRecursive(File fileOrDirectory)
SecurityException
- if the application does not have permission
to delete the fileOrDirectory.public static boolean deleteRecursiveFrom(File directory)
deleteRecursive
(), but it works only on
the contents of the specified directory.directory
- the directory whose contents to delete.SecurityException
- if the application does not have permission
to delete the directory.public static void deleteRecursiveSure(File fileOrDirectory) throws IOException
SecurityException
- if the application does not have permission
to delete the fileOrDirectory.IOException
public static void ensuredirs(File directory) throws IOException
directory
- the directory to ensure. If it is null, this method is a
no-op.IOException
File.mkdirs()
public static String[] listNoNull(File file)
public static String[] listNoNull(File file, FilenameFilter filter)
public static File[] listFilesNoNull(File file)
public static File[] listFilesNoNull(File file, FileFilter filter)
public static Object readObject(File file) throws ClassNotFoundException, IOException
ClassNotFoundException
IOException
writeObject(Object,File)
public static void renameFromDefaultToMv(File oldFile, File newFile) throws IOException
mv
command if it fails.IOException
- if the file cannot be renamed.public static void symlink(String targetPath, String linkPath) throws IOException
linkPath
, pointing to
targetPath
. This is likely to fail on non-Unix platforms. It is
equivalent to the Unix command:
ln --force --no-dereference --symbolic targetPath linkPath
targetPath
- the path to which the link file will point.
If the path is relative, then it is anchored at the link file.linkPath
- the path of the link file to create.IOException
- if the attempt fails.public static boolean testsCaseSensitive()
public static boolean traverse(File bottomFile, FileFilter upFilter, FileFilter downFilter)
bottomFile
- the bottom-most file.upFilter
- the upward traversal filter. It returns true
if upward traversal is to continue; false if it is to stop.downFilter
- the downward traversal filter. It returns true
if downward traversal is to continue; false if it is to stop.public static boolean traverseDown(File topFile, FileFilter filter)
topFile
- the top-most file.filter
- the traversal filter. It returns true if traversal is to
continue; false if it is to stop.public static void traverseUp(File bottomFile, FileFilter filter)
bottomFile
- the bottom-most file.filter
- the traversal filter. It returns true if traversal is to
continue; false if it is to stop.public static void writeObject(Object object, File file) throws IOException
IOException
readObject(File)