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)
Creates a new text file with the given content.
|
static java.nio.file.FileSystem |
createZipFilesystem(java.nio.file.Path archive)
Creates a new
FileSystem for a given ZIP file. |
static void |
delete(java.io.File file)
Deletes an existing file or an existing empty directory.
|
static void |
deleteDirectoryContents(java.io.File directory)
Recursively deletes a directory content (including the sub directories) but not itself.
|
static void |
deleteIfExists(java.io.File file)
Deletes a file or an empty directory if it exists.
|
static void |
deletePath(java.io.File path)
Recursively deletes a path.
|
static void |
deleteRecursivelyIfExists(java.io.File file)
Deletes a file or a directory if it exists.
|
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 |
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 boolean |
parentDirExists(java.io.File file)
Returns
true if the parent directory of the given file/directory exists, and false otherwise. |
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.lang.String path)
Converts a system-dependent path into a /-based path.
|
static void |
writeToFile(java.io.File file,
java.lang.String content)
Creates a new text file or replaces content of an existing file.
|
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 pathpublic static void copyFile(@NonNull
java.io.File from,
@NonNull
java.io.File to)
throws java.io.IOException
java.io.IOExceptionpublic static void copyDirectory(@NonNull
java.io.File from,
@NonNull
java.io.File to)
throws java.io.IOException
java.io.IOExceptionpublic static void copyFileToDirectory(@NonNull
java.io.File from,
@NonNull
java.io.File to)
throws java.io.IOException
java.io.IOExceptionpublic static void copyDirectoryToDirectory(@NonNull
java.io.File from,
@NonNull
java.io.File to)
throws java.io.IOException
java.io.IOExceptioncopyDirectory(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 existfolderpublic static void delete(@NonNull
java.io.File file)
throws java.io.IOException
file - the file or directory to delete. The file/directory must exist, if the directory
exists, it must be empty.java.io.IOExceptionpublic static void deleteIfExists(@NonNull
java.io.File file)
throws java.io.IOException
file - the file or directory to delete. The file/directory may not exist; if the
directory exists, it must be empty.java.io.IOExceptionpublic static void deleteRecursivelyIfExists(@NonNull
java.io.File file)
throws java.io.IOException
file - the file or directory to delete. The file/directory may not exist; if the
directory exists, it may be non-empty.java.io.IOExceptionpublic 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 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
file - the file to write tocontent - the new content of the filejava.io.IOExceptionpublic static void writeToFile(@NonNull
java.io.File file,
@NonNull
java.lang.String content)
throws java.io.IOException
file - the file to write tocontent - the new content of the filejava.io.IOExceptionpublic 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 parentDirExists(@NonNull
java.io.File file)
true if the parent directory of the given file/directory exists, and false otherwise. Note that this method resolves the real path of the given file/directory
first via File.getCanonicalFile().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. Note that this method resolves the real paths
of the given file/directory first via File.getCanonicalFile().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.nio.file.FileSystem createZipFilesystem(@NonNull
java.nio.file.Path archive)
throws java.io.IOException
FileSystem for a given ZIP file.
Note that NIO filesystems are unique per URI, so the returned FileSystem should be
closed as soon as possible.
java.io.IOException