Class FileUtils
- java.lang.Object
-
- org.apache.flink.util.FileUtils
-
public final class FileUtils extends Object
This is a utility class to deal files and directories. Contains utilities for recursive deletion and creation of temporary files.
-
-
Field Summary
Fields Modifier and Type Field Description static String
CLASS_FILE_EXTENSION
static String
PACKAGE_SEPARATOR
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method 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)
Un-archives files inside the target directory.static Path
getCurrentWorkingDirectory()
Returns the current working directory as specified by theuser.dir
system property.static long
getDirectoryFilesSize(Path path, FileVisitOption... options)
Computes the sum of sizes of all files in the directory and it's subdirectories.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, Predicate<Path> fileFilter)
List thedirectory
recursively and return the files that satisfy thefileFilter
.static byte[]
readAllBytes(Path path)
Reads all the bytes from a file.static String
readFile(File file, Charset charset)
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, Charset charset)
static void
writeFileUtf8(File file, String contents)
-
-
-
Field Detail
-
CLASS_FILE_EXTENSION
public static final String CLASS_FILE_EXTENSION
- See Also:
- Constant Field Values
-
PACKAGE_SEPARATOR
public static final String PACKAGE_SEPARATOR
- See Also:
- Constant Field Values
-
-
Method Detail
-
writeCompletely
public static void writeCompletely(WritableByteChannel channel, ByteBuffer src) throws IOException
- Throws:
IOException
-
listDirectory
public static Path[] listDirectory(Path directory) throws IOException
Lists the given directory in a resource-leak-safe way.- Throws:
IOException
-
getRandomFilename
public static String getRandomFilename(String prefix)
Constructs a random filename with the given prefix and a random part generated from hex characters.- Parameters:
prefix
- the prefix to the filename to be constructed- Returns:
- the generated random filename with the given prefix
-
readFile
public static String readFile(File file, Charset charset) throws IOException
- Throws:
IOException
-
readFileUtf8
public static String readFileUtf8(File file) throws IOException
- Throws:
IOException
-
writeFile
public static void writeFile(File file, String contents, Charset charset) throws IOException
- Throws:
IOException
-
writeFileUtf8
public static void writeFileUtf8(File file, String contents) throws IOException
- Throws:
IOException
-
readAllBytes
public static byte[] readAllBytes(Path path) throws IOException
Reads all the bytes from a file. The method ensures that the file is closed when all bytes have been read or an I/O error, or other runtime exception, is thrown.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. WhenFiles.readAllBytes(java.nio.file.Path)
or other interfaces in java API can do this in the future, we should remove it.- Parameters:
path
- the path to the file- Returns:
- a byte array containing the bytes read from the file
- Throws:
IOException
- 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 that2GB
-
deleteFileOrDirectory
public static void deleteFileOrDirectory(File file) throws IOException
Removes the given file or directory recursively.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.
- Parameters:
file
- The file or directory to delete.- Throws:
IOException
- Thrown if the directory could not be cleaned for some reason, for example due to missing access/write permissions.
-
deleteDirectory
public static void deleteDirectory(File directory) throws IOException
Deletes the given directory recursively.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.
- Parameters:
directory
- The directory to be deleted.- Throws:
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.
-
deleteDirectoryQuietly
public static void deleteDirectoryQuietly(File directory)
Deletes the given directory recursively, not reporting any I/O exceptions that occur.This method is identical to
deleteDirectory(File)
, except that it swallows all exceptions and may leave the job quietly incomplete.- Parameters:
directory
- The directory to delete.
-
cleanDirectory
public static void cleanDirectory(File directory) throws IOException
Removes all files contained within a directory, without removing the directory itself.This method is safe against other concurrent deletion attempts.
- Parameters:
directory
- The directory to remove all files from.- Throws:
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.
-
copy
public static void copy(Path sourcePath, Path targetPath, boolean executable) throws IOException
Copies all files from source to target and sets executable flag. Paths might be on different systems.- Parameters:
sourcePath
- source path to copy fromtargetPath
- target path to copy toexecutable
- if target file should be executable- Throws:
IOException
- if the copy fails
-
compressDirectory
public static Path compressDirectory(Path directory, Path target) throws IOException
- Throws:
IOException
-
expandDirectory
public static Path expandDirectory(Path file, Path targetDirectory) throws IOException
Un-archives files inside the target directory. Illegal fs access outside target directory is not permitted.- Parameters:
file
- path to zipped/archived filetargetDirectory
- directory path where file needs to be unarchived- Returns:
- path to folder with unarchived contents
- Throws:
IOException
- if file open fails or in case of unsafe access outside target directory
-
listFilesInDirectory
public static Collection<Path> listFilesInDirectory(Path directory, Predicate<Path> fileFilter) throws IOException
List thedirectory
recursively and return the files that satisfy thefileFilter
.- Parameters:
directory
- the directory to be listedfileFilter
- a file filter- Returns:
- a collection of
File
s - Throws:
IOException
- if an I/O error occurs while listing the files in the given directory
-
getDirectoryFilesSize
public static long getDirectoryFilesSize(Path path, FileVisitOption... options) throws IOException
Computes the sum of sizes of all files in the directory and it's subdirectories.- Parameters:
path
- the root path from which to start the calculation.options
- visitation options for the directory traversal.- Returns:
- sum of sizes of all files in the directory and it's subdirectories.
- Throws:
IOException
- if the size cannot be determined.
-
absolutizePath
public static Path absolutizePath(Path pathToAbsolutize) throws IOException
Absolutize the given path if it is relative.- Parameters:
pathToAbsolutize
- path which is being absolutized if it is a relative path- Returns:
- the absolutized path
- Throws:
IOException
-
relativizePath
public static Path relativizePath(Path basePath, Path pathToRelativize)
Relativize the given path with respect to the given base path if it is absolute.- Parameters:
basePath
- to relativize againstpathToRelativize
- path which is being relativized if it is an absolute path- Returns:
- the relativized path
-
getCurrentWorkingDirectory
public static Path getCurrentWorkingDirectory()
Returns the current working directory as specified by theuser.dir
system property.- Returns:
- current working directory
-
isJarFile
public static boolean isJarFile(Path file)
Checks whether the given file has a jar extension.- Parameters:
file
- to check- Returns:
- true if the file has a jar extension, otherwise false
-
stripFileExtension
public static String stripFileExtension(String fileName)
Remove the extension of the file name.- Parameters:
fileName
- to strip- Returns:
- the file name without extension
-
getTargetPathIfContainsSymbolicPath
public static Path getTargetPathIfContainsSymbolicPath(Path path) throws IOException
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.- Parameters:
path
- the original path.- Returns:
- the path that replaced symbolic links with real path.
- Throws:
IOException
-
toURL
public static URL toURL(Path path) throws MalformedURLException
Converts the givenPath
into a fileURL
. The resulting url is relative iff the given path is relative.- Parameters:
path
- to convert into aURL
.- Returns:
- URL
- Throws:
MalformedURLException
- if the path could not be converted into a fileURL
-
-