public final class FileUtils
extends java.lang.Object
Modifier and Type | Method and Description |
---|---|
static void |
cleanOutputDir(java.io.File path)
Makes sure
path is an empty directory. |
static void |
copyDirectory(java.io.File from,
java.io.File to)
Copies a directory from one path to another.
|
static void |
copyDirectoryContentToDirectory(java.io.File from,
java.io.File to)
Makes a copy of the directory's content, in the specified location, while maintaining the
directory structure.
|
static void |
copyDirectoryToDirectory(java.io.File from,
java.io.File to)
Makes a copy of the given directory in the specified destination directory.
|
static void |
copyFile(java.io.File from,
java.io.File to)
Copies a regular file from one path to another, preserving file attributes.
|
static void |
copyFileToDirectory(java.io.File from,
java.io.File to)
Makes a copy of the given file in the specified directory, preserving the name and file
attributes.
|
static void |
createFile(java.io.File file,
java.lang.String content) |
static void |
delete(java.io.File file)
Deletes a file.
|
static void |
deleteDirectoryContents(java.io.File directory)
Recursively deletes a directory or file.
|
static void |
deleteIfExists(java.io.File file) |
static void |
deletePath(java.io.File path)
Recursively deletes a path.
|
static java.util.List<java.io.File> |
find(java.io.File base,
java.util.regex.Pattern pattern)
Find a list of files in a directory, using a specified path pattern.
|
static com.google.common.base.Optional<java.io.File> |
find(java.io.File base,
java.lang.String name)
Find a file with the specified name in a given directory .
|
static com.google.common.collect.FluentIterable<java.io.File> |
getAllFiles(java.io.File dir) |
static java.lang.String |
getCaseSensitivityAwareCanonicalPath(java.io.File file)
Returns the modified canonical path of a file with consideration of the case sensitivity of
the underlying file system.
|
static java.lang.String |
getDirectoryNameForJar(java.io.File inputFile)
Chooses a directory name, based on a JAR file name, considering exploded-aar and classes.jar.
|
static java.lang.String |
getNamesAsCommaSeparatedList(java.lang.Iterable<java.io.File> files) |
static boolean |
isFileInDirectory(java.io.File file,
java.io.File directory)
Returns
true if a file/directory is in a given directory or in a subdirectory of the
given directory, and false otherwise. |
static boolean |
isSameFile(java.io.File file1,
java.io.File file2)
Returns
true if the two files refer to the same physical file, and false
otherwise. |
static java.io.File |
join(java.io.File dir,
java.lang.Iterable<java.lang.String> paths)
Joins a list of path segments to a given File object.
|
static java.io.File |
join(java.io.File dir,
java.lang.String... paths)
Joins a list of path segments to a given File object.
|
static java.lang.String |
join(java.lang.Iterable<java.lang.String> paths)
Joins a set of segment into a string, separating each segments with a host-specific
path separator.
|
static java.lang.String |
join(java.lang.String... paths)
Joins a set of segment into a string, separating each segments with a host-specific
path separator.
|
static java.lang.String |
joinFilePaths(java.lang.Iterable<java.io.File> files)
Join multiple file paths as String.
|
static java.lang.String |
loadFileWithUnixLineSeparators(java.io.File file)
Loads a text file forcing the line separator to be of Unix style '\n' rather than being
Windows style '\r\n'.
|
static java.io.File |
mkdirs(java.io.File folder)
Creates a directory, if it doesn't exist.
|
static java.lang.String |
relativePath(java.io.File file,
java.io.File dir)
Computes the relative of a file or directory with respect to a directory.
|
static java.lang.String |
relativePossiblyNonExistingPath(java.io.File file,
java.io.File dir)
Computes the relative of a file or directory with respect to a directory.
|
static void |
renameTo(java.io.File file,
java.io.File to) |
static java.lang.String |
sha1(java.io.File file) |
static java.lang.String |
toExportableSystemDependentPath(java.io.File file)
Returns an absolute path that can be open by system APIs for all platforms.
|
static java.lang.String |
toSystemDependentPath(java.lang.String path)
Converts a /-based path into a path using the system dependent separator.
|
static java.lang.String |
toSystemIndependentPath(java.nio.file.Path path)
|
static java.lang.String |
toSystemIndependentPath(java.lang.String path)
Converts a system-dependent path into a /-based path.
|
public static void deletePath(@NonNull java.io.File path) throws java.io.IOException
path
- the path delete, may exist or notjava.io.IOException
- failed to delete the file / directorypublic static void deleteDirectoryContents(@NonNull java.io.File directory) throws java.io.IOException
directory
- the directory, that must exist and be a valid directoryjava.io.IOException
- failed to delete the file / directorypublic static void cleanOutputDir(@NonNull java.io.File path) throws java.io.IOException
path
is an empty directory. If path
is a directory, its contents
are removed recursively, leaving an empty directory. If path
is not a directory,
it is removed and a directory created with the given path. If path
does not
exist, a directory is created with the given path.path
- the path, that may exist or not and may be a file or directoryjava.io.IOException
- failed to delete directory contents, failed to delete path
or
failed to create a directory at path
public static void copyFile(@NonNull java.io.File from, @NonNull java.io.File to) throws java.io.IOException
java.io.IOException
public static void copyDirectory(@NonNull java.io.File from, @NonNull java.io.File to) throws java.io.IOException
java.io.IOException
public static void copyFileToDirectory(@NonNull java.io.File from, @NonNull java.io.File to) throws java.io.IOException
java.io.IOException
public static void copyDirectoryToDirectory(@NonNull java.io.File from, @NonNull java.io.File to) throws java.io.IOException
java.io.IOException
copyDirectory(File, File)
public static void copyDirectoryContentToDirectory(@NonNull java.io.File from, @NonNull java.io.File to) throws java.io.IOException
from
- directory from which the content is copiedto
- destination directory, will be created if does not existjava.io.IOException
@NonNull public static java.io.File mkdirs(@NonNull java.io.File folder)
folder
- the directory to create, may already existfolder
public static void delete(@NonNull java.io.File file) throws java.io.IOException
file
- the file to delete; the file must existjava.io.IOException
- failed to delete the filepublic static void deleteIfExists(@NonNull java.io.File file) throws java.io.IOException
java.io.IOException
public static void renameTo(@NonNull java.io.File file, @NonNull java.io.File to) throws java.io.IOException
java.io.IOException
@NonNull public static java.io.File join(@NonNull java.io.File dir, @NonNull java.lang.String... paths)
dir
- the file object.paths
- the segments.@NonNull public static java.io.File join(@NonNull java.io.File dir, @NonNull java.lang.Iterable<java.lang.String> paths)
dir
- the file object.paths
- the segments.@NonNull public static java.lang.String join(@NonNull java.lang.String... paths)
paths
- the segments.@NonNull public static java.lang.String join(@NonNull java.lang.Iterable<java.lang.String> paths)
paths
- the segments.@NonNull public static java.lang.String loadFileWithUnixLineSeparators(@NonNull java.io.File file) throws java.io.IOException
java.io.IOException
@NonNull public static java.lang.String relativePath(@NonNull java.io.File file, @NonNull java.io.File dir)
file
- the file or directory, which must exist in the filesystemdir
- the directory to compute the path relative todir
to file
; if file
is a directory
the path comes appended with the file separator (see documentation on relativize
on java's URI
class)@NonNull public static java.lang.String relativePossiblyNonExistingPath(@NonNull java.io.File file, @NonNull java.io.File dir)
/a/b/c
and the directory
is /a
, this method returns b/c
.file
- the path that may not correspond to any existing path in the filesystemdir
- the directory to compute the path relative todir
to file
; if file
is a directory
the path comes appended with the file separator (see documentation on relativize
on java's URI
class)@NonNull public static java.lang.String toSystemDependentPath(@NonNull java.lang.String path)
path
- the system independent path to convert@NonNull public static java.lang.String toSystemIndependentPath(@NonNull java.lang.String path)
path
- the system dependent path@NonNull public static java.lang.String toSystemIndependentPath(@NonNull java.nio.file.Path path)
@NonNull public static java.lang.String toExportableSystemDependentPath(@NonNull java.io.File file)
file
- The file whose path needs to be converted.@NonNull public static java.lang.String sha1(@NonNull java.io.File file) throws java.io.IOException
java.io.IOException
@NonNull public static com.google.common.collect.FluentIterable<java.io.File> getAllFiles(@NonNull java.io.File dir)
@NonNull public static java.lang.String getNamesAsCommaSeparatedList(@NonNull java.lang.Iterable<java.io.File> files)
@NonNull public static java.lang.String getDirectoryNameForJar(@NonNull java.io.File inputFile)
public static void createFile(@NonNull java.io.File file, @NonNull java.lang.String content) throws java.io.IOException
java.io.IOException
public static java.util.List<java.io.File> find(@NonNull java.io.File base, @NonNull java.util.regex.Pattern pattern)
public static com.google.common.base.Optional<java.io.File> find(@NonNull java.io.File base, @NonNull java.lang.String name)
@NonNull public static java.lang.String joinFilePaths(@NonNull java.lang.Iterable<java.io.File> files)
public static boolean isFileInDirectory(@NonNull java.io.File file, @NonNull java.io.File directory)
true
if a file/directory is in a given directory or in a subdirectory of the
given directory, and false
otherwise.public static boolean isSameFile(@NonNull java.io.File file1, @NonNull java.io.File file2)
true
if the two files refer to the same physical file, and false
otherwise. This is the correct way to compare physical files, instead of comparing using
File.equals(Object)
directly.
Unlike Files.isSameFile(Path, Path)
, this method does not require
the files to exist.
Internally, this method delegates to Files.isSameFile(Path, Path)
if
the files exist.
If either of the files does not exist, this method instead compares the canonical files of
the two files, since Files.isSameFile(Path, Path)
in some cases require
that the files exist and therefore cannot be used. The downside of using File.getCanonicalFile()
is that it may not handle hard links and symbolic links correctly as
with Files.isSameFile(Path, Path)
.
@NonNull public static java.lang.String getCaseSensitivityAwareCanonicalPath(@NonNull java.io.File file) throws java.io.IOException
This method addresses the scenario where we want to compute a unique path of a file such
that two files with different computed paths are guaranteed to be different physical files,
and vice versa. In such cases, using Java's File.getCanonicalPath()
would not work
because in case-insensitive file systems like Windows, two files having different canonical
paths may actually refer to the same physical file (e.g., "/foo"
and "/Foo"
).
To address this issue, this method first detects whether the underlying file system is
case-sensitive or not. If it is, this method returns the canonical path of the file, as would
be returned by File.getCanonicalPath()
. If it isn't, this method returns the
lower-case canonical path of the file.
java.io.IOException