public final class FileUtils extends Object
Modifier and Type | Field and Description |
---|---|
static String |
CLASS_FILE_EXTENSION |
static String |
PACKAGE_SEPARATOR |
Modifier and Type | Method and Description |
---|---|
static Path |
absolutizePath(Path pathToAbsolutize)
Absolutize the given path if it is relative.
|
static void |
cleanDirectory(File directory)
Removes all files contained within a directory, without removing the directory itself.
|
static Path |
compressDirectory(Path directory,
Path target) |
static void |
copy(Path sourcePath,
Path targetPath,
boolean executable)
Copies all files from source to target and sets executable flag.
|
static void |
deleteDirectory(File directory)
Deletes the given directory recursively.
|
static void |
deleteDirectoryQuietly(File directory)
Deletes the given directory recursively, not reporting any I/O exceptions that occur.
|
static void |
deleteFileOrDirectory(File file)
Removes the given file or directory recursively.
|
static Path |
expandDirectory(Path file,
Path targetDirectory) |
static Path |
getCurrentWorkingDirectory()
Returns the current working directory as specified by the
user.dir system property. |
static String |
getRandomFilename(String prefix)
Constructs a random filename with the given prefix and a random part generated from hex
characters.
|
static Path |
getTargetPathIfContainsSymbolicPath(Path path)
Get a target path(the path that replaced symbolic links with linked path) if the original
path contains symbolic path, return the original path otherwise.
|
static boolean |
isJarFile(Path file)
Checks whether the given file has a jar extension.
|
static Path[] |
listDirectory(Path directory)
Lists the given directory in a resource-leak-safe way.
|
static Collection<Path> |
listFilesInDirectory(Path directory,
java.util.function.Predicate<Path> fileFilter)
List the
directory recursively and return the files that satisfy the fileFilter . |
static byte[] |
readAllBytes(Path path)
Reads all the bytes from a file.
|
static String |
readFile(File file,
String charsetName) |
static String |
readFileUtf8(File file) |
static Path |
relativizePath(Path basePath,
Path pathToRelativize)
Relativize the given path with respect to the given base path if it is absolute.
|
static String |
stripFileExtension(String fileName)
Remove the extension of the file name.
|
static URL |
toURL(Path path)
|
static void |
writeCompletely(WritableByteChannel channel,
ByteBuffer src) |
static void |
writeFile(File file,
String contents,
String encoding) |
static void |
writeFileUtf8(File file,
String contents) |
public static final String CLASS_FILE_EXTENSION
public static final String PACKAGE_SEPARATOR
public static void writeCompletely(WritableByteChannel channel, ByteBuffer src) throws IOException
IOException
public static Path[] listDirectory(Path directory) throws IOException
IOException
public static String getRandomFilename(String prefix)
prefix
- the prefix to the filename to be constructedpublic static String readFile(File file, String charsetName) throws IOException
IOException
public static String readFileUtf8(File file) throws IOException
IOException
public static void writeFile(File file, String contents, String encoding) throws IOException
IOException
public static void writeFileUtf8(File file, String contents) throws IOException
IOException
public static byte[] readAllBytes(Path path) throws IOException
This is an implementation that follow Files.readAllBytes(java.nio.file.Path)
, and the difference is that it limits
the size of the direct buffer to avoid direct-buffer OutOfMemoryError. When Files.readAllBytes(java.nio.file.Path)
or other interfaces in java API can do
this in the future, we should remove it.
path
- the path to the fileIOException
- if an I/O error occurs reading from the streamOutOfMemoryError
- if an array of the required size cannot be allocated, for example
the file is larger that 2GB
public static void deleteFileOrDirectory(File file) throws IOException
If the file or directory does not exist, this does not throw an exception, but simply does nothing. It considers the fact that a file-to-be-deleted is not present a success.
This method is safe against other concurrent deletion attempts.
file
- The file or directory to delete.IOException
- Thrown if the directory could not be cleaned for some reason, for example
due to missing access/write permissions.public static void deleteDirectory(File directory) throws IOException
If the directory does not exist, this does not throw an exception, but simply does nothing. It considers the fact that a directory-to-be-deleted is not present a success.
This method is safe against other concurrent deletion attempts.
directory
- The directory to be deleted.IOException
- Thrown if the given file is not a directory, or if the directory could
not be deleted for some reason, for example due to missing access/write permissions.public static void deleteDirectoryQuietly(File directory)
This method is identical to deleteDirectory(File)
, except that it
swallows all exceptions and may leave the job quietly incomplete.
directory
- The directory to delete.public static void cleanDirectory(File directory) throws IOException
This method is safe against other concurrent deletion attempts.
directory
- The directory to remove all files from.FileNotFoundException
- Thrown if the directory itself does not exist.IOException
- Thrown if the file indicates a proper file and not a directory, or if the
directory could not be cleaned for some reason, for example due to missing access/write
permissions.public static void copy(Path sourcePath, Path targetPath, boolean executable) throws IOException
sourcePath
- source path to copy fromtargetPath
- target path to copy toexecutable
- if target file should be executableIOException
- if the copy failspublic static Path compressDirectory(Path directory, Path target) throws IOException
IOException
public static Path expandDirectory(Path file, Path targetDirectory) throws IOException
IOException
public static Collection<Path> listFilesInDirectory(Path directory, java.util.function.Predicate<Path> fileFilter) throws IOException
directory
recursively and return the files that satisfy the fileFilter
.directory
- the directory to be listedfileFilter
- a file filterFile
sIOException
- if an I/O error occurs while listing the files in the given directorypublic static Path absolutizePath(Path pathToAbsolutize) throws IOException
pathToAbsolutize
- path which is being absolutized if it is a relative pathIOException
public static Path relativizePath(Path basePath, Path pathToRelativize)
basePath
- to relativize againstpathToRelativize
- path which is being relativized if it is an absolute pathpublic static Path getCurrentWorkingDirectory()
user.dir
system property.public static boolean isJarFile(Path file)
file
- to checkpublic static String stripFileExtension(String fileName)
fileName
- to strippublic static Path getTargetPathIfContainsSymbolicPath(Path path) throws IOException
path
- the original path.IOException
public static URL toURL(Path path) throws MalformedURLException
Path
into a file URL
. The resulting url is
relative iff the given path is relative.path
- to convert into a URL
.MalformedURLException
- if the path could not be converted into a file URL
Copyright © 2014–2024 The Apache Software Foundation. All rights reserved.