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 |
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 java.lang.String |
getValidFileName(java.lang.String baseName,
java.lang.String extension,
java.io.File directory)
Returns a valid file name modified from the requested file name.
|
static boolean |
isFileNameTooLong(java.lang.String fileName)
Returns
true if the file name is too long. |
static boolean |
isFilePathTooLong(java.lang.String fileName,
java.io.File directory)
Returns
true if the file name or file path is too long. |
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 byte[] |
readSegment(java.io.File file,
long start,
int length)
Reads a portion of a file to memory.
|
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 |
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.
|
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 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 byte[] readSegment(@NonNull java.io.File file, long start, int length) throws java.lang.Exception
file
- the file to read data fromstart
- the offset in the file to start readinglength
- the number of bytes to readjava.lang.Exception
- failed to read the file@NonNull public static java.lang.String joinFilePaths(@NonNull java.lang.Iterable<java.io.File> files)
@NonNull public static java.lang.String getValidFileName(@NonNull java.lang.String baseName, @NonNull java.lang.String extension, @NonNull java.io.File directory) throws java.io.IOException
baseName
- the base name of the requested file nameextension
- the extension of the requested file name (empty string if not available)directory
- the directory where the file will be locatedjava.io.IOException
- if the requested file name or file path is too longpublic static boolean isFileNameTooLong(@NonNull java.lang.String fileName)
true
if the file name is too long.fileName
- the file namepublic static boolean isFilePathTooLong(@NonNull java.lang.String fileName, @NonNull java.io.File directory)
true
if the file name or file path is too long.fileName
- the file namedirectory
- the directory where the file will be located