@Public
@Evolving
public class FileUtil
extends java.lang.Object
| Modifier and Type | Class | Description |
|---|---|---|
static class |
FileUtil.HardLink |
Deprecated.
Use
HardLink |
| Modifier and Type | Field | Description |
|---|---|---|
static int |
SYMLINK_NO_PRIVILEGE |
| Constructor | Description |
|---|---|
FileUtil() |
| Modifier and Type | Method | Description |
|---|---|---|
static boolean |
canExecute(java.io.File f) |
Platform independent implementation for
File.canExecute() |
static boolean |
canRead(java.io.File f) |
Platform independent implementation for
File.canRead() |
static boolean |
canWrite(java.io.File f) |
Platform independent implementation for
File.canWrite() |
static PathData |
checkExistItem(PathData item) |
|
static PathData |
checkItemForSymlink(PathData item) |
|
static PathData |
checkPathForSymlink(Path path,
Configuration conf) |
|
static int |
chmod(java.lang.String filename,
java.lang.String perm) |
Change the permissions on a filename.
|
static int |
chmod(java.lang.String filename,
java.lang.String perm,
boolean recursive) |
Change the permissions on a file / directory, recursively, if
needed.
|
static boolean |
compareFs(FileSystem srcFs,
FileSystem destFs) |
|
static boolean |
copy(java.io.File src,
FileSystem dstFS,
Path dst,
boolean deleteSource,
Configuration conf) |
Copy local files to a FileSystem.
|
static boolean |
copy(FileSystem srcFS,
FileStatus srcStatus,
FileSystem dstFS,
Path dst,
boolean deleteSource,
boolean overwrite,
Configuration conf) |
Copy a file/directory tree within/between filesystems.
|
static boolean |
copy(FileSystem srcFS,
Path[] srcs,
FileSystem dstFS,
Path dst,
boolean deleteSource,
boolean overwrite,
Configuration conf) |
|
static boolean |
copy(FileSystem srcFS,
Path src,
java.io.File dst,
boolean deleteSource,
Configuration conf) |
Copy FileSystem files to local files.
|
static boolean |
copy(FileSystem srcFS,
Path src,
FileSystem dstFS,
Path dst,
boolean deleteSource,
boolean overwrite,
Configuration conf) |
Copy files between FileSystems.
|
static boolean |
copy(FileSystem srcFS,
Path src,
FileSystem dstFS,
Path dst,
boolean deleteSource,
Configuration conf) |
Copy files between FileSystems.
|
static boolean |
copyAsMove(FileSystem srcFS,
FileStatus srcStatus,
FileSystem dstFS,
Path dst,
boolean deleteSource,
boolean overwrite,
Configuration conf) |
Copy files between FileSystems with preserved permissions, ownership, links and ACEs.
|
static boolean |
copyMerge(FileSystem srcFS,
Path srcDir,
FileSystem dstFS,
Path dstFile,
boolean deleteSource,
Configuration conf,
java.lang.String addString) |
Copy all files in a directory to one output file (merge).
|
static java.lang.String[] |
createJarWithClassPath(java.lang.String inputClassPath,
Path pwd,
java.util.Map<java.lang.String,java.lang.String> callerEnv) |
|
static java.lang.String[] |
createJarWithClassPath(java.lang.String inputClassPath,
Path pwd,
Path targetDir,
java.util.Map<java.lang.String,java.lang.String> callerEnv) |
Create a jar file at the given path, containing a manifest with a classpath
that references all specified entries.
|
static java.io.File |
createLocalTempFile(java.io.File basefile,
java.lang.String prefix,
boolean isDeleteOnExit) |
Create a tmp file for a base file.
|
static Path |
fixSymlinkFileStatus(FileStatus stat) |
|
static Path |
fixSymlinkPath(PathData path) |
|
static boolean |
fullyDelete(java.io.File dir) |
Delete a directory and all its contents.
|
static boolean |
fullyDelete(java.io.File dir,
boolean tryGrantPermissions) |
Delete a directory and all its contents.
|
static void |
fullyDelete(FileSystem fs,
Path dir) |
Deprecated.
|
static boolean |
fullyDeleteContents(java.io.File dir) |
Delete the contents of a directory, not the directory itself.
|
static boolean |
fullyDeleteContents(java.io.File dir,
boolean tryGrantPermissions) |
Delete the contents of a directory, not the directory itself.
|
static void |
fullyDeleteOnExit(java.io.File file) |
Register all files recursively to be deleted on exit.
|
static long |
getDU(java.io.File dir) |
Takes an input dir and returns the du on that local directory.
|
static java.util.List<Path> |
getJarsInDirectory(java.lang.String path) |
Returns all jars that are in the directory.
|
static java.util.List<Path> |
getJarsInDirectory(java.lang.String path,
boolean useLocal) |
Returns all jars that are in the directory.
|
static boolean |
isRegularFile(java.io.File file) |
|
static boolean |
isRegularFile(java.io.File file,
boolean allowLinks) |
Check if the file is regular.
|
static java.lang.String[] |
list(java.io.File dir) |
A wrapper for
File.list(). |
static java.io.File[] |
listFiles(java.io.File dir) |
A wrapper for
File.listFiles(). |
static java.lang.String |
makeSecureShellPath(java.io.File file) |
Convert a os-native filename to a path that works for the shell
and avoids script injection attacks.
|
static java.lang.String |
makeShellPath(java.io.File file) |
Convert a os-native filename to a path that works for the shell.
|
static java.lang.String |
makeShellPath(java.io.File file,
boolean makeCanonicalPath) |
Convert a os-native filename to a path that works for the shell.
|
static java.lang.String |
makeShellPath(java.lang.String filename) |
Convert a os-native filename to a path that works for the shell.
|
static void |
maybeIgnoreMissingDirectory(FileSystem fs,
Path path,
java.io.FileNotFoundException e) |
Method to call after a FNFE has been raised on a treewalk, so as to
decide whether to throw the exception (default), or, if the FS
supports inconsistent directory listings, to log and ignore it.
|
static java.lang.String |
readLink(java.io.File f) |
Returns the target of the given symlink.
|
static void |
rename(FileSystem srcFs,
Path src,
Path dst,
Options.Rename... options) |
|
static void |
replaceFile(java.io.File src,
java.io.File target) |
Move the src file to the name specified by target.
|
static boolean |
setExecutable(java.io.File f,
boolean executable) |
Platform independent implementation for
File.setExecutable(boolean)
File#setExecutable does not work as expected on Windows. |
static void |
setOwner(java.io.File file,
java.lang.String username,
java.lang.String groupname) |
Set the ownership on a file / directory.
|
static void |
setPermission(java.io.File f,
FsPermission permission) |
Set permissions to the required value.
|
static boolean |
setReadable(java.io.File f,
boolean readable) |
Platform independent implementation for
File.setReadable(boolean)
File#setReadable does not work as expected on Windows. |
static boolean |
setWritable(java.io.File f,
boolean writable) |
Platform independent implementation for
File.setWritable(boolean)
File#setWritable does not work as expected on Windows. |
static Path[] |
stat2Paths(FileStatus[] stats) |
convert an array of FileStatus to an array of Path
|
static Path[] |
stat2Paths(FileStatus[] stats,
Path path) |
convert an array of FileStatus to an array of Path.
|
static int |
symLink(java.lang.String target,
java.lang.String linkname) |
Create a soft link between a src and destination
only on a local disk.
|
static void |
unTar(java.io.File inFile,
java.io.File untarDir) |
Given a Tar File as input it will untar the file in a the untar directory
passed as the second parameter
This utility will untar ".tar" files and ".tar.gz","tgz" files.
|
static void |
unTar(java.io.InputStream inputStream,
java.io.File untarDir,
boolean gzipped) |
Given a Tar File as input it will untar the file in a the untar directory
passed as the second parameter
This utility will untar ".tar" files and ".tar.gz","tgz" files.
|
static void |
unZip(java.io.File inFile,
java.io.File unzipDir) |
Given a File input it will unzip it in the unzip directory.
|
static void |
unZip(java.io.InputStream inputStream,
java.io.File toDir) |
Given a stream input it will unzip the it in the unzip directory.
|
static FileContext |
write(FileContext fileContext,
Path path,
byte[] bytes) |
Writes bytes to a file.
|
static FileContext |
write(FileContext fileContext,
Path path,
java.lang.CharSequence charseq) |
Write a line of text to a file.
|
static FileContext |
write(FileContext fs,
Path path,
java.lang.CharSequence charseq,
java.nio.charset.Charset cs) |
Write a line of text to a file.
|
static FileContext |
write(FileContext fileContext,
Path path,
java.lang.Iterable<? extends java.lang.CharSequence> lines,
java.nio.charset.Charset cs) |
Write lines of text to a file.
|
static FileSystem |
write(FileSystem fs,
Path path,
byte[] bytes) |
Writes bytes to a file.
|
static FileSystem |
write(FileSystem fs,
Path path,
java.lang.CharSequence charseq) |
Write a line of text to a file.
|
static FileSystem |
write(FileSystem fs,
Path path,
java.lang.CharSequence charseq,
java.nio.charset.Charset cs) |
Write a line of text to a file.
|
static FileSystem |
write(FileSystem fs,
Path path,
java.lang.Iterable<? extends java.lang.CharSequence> lines,
java.nio.charset.Charset cs) |
Write lines of text to a file.
|
public static final int SYMLINK_NO_PRIVILEGE
public static Path[] stat2Paths(FileStatus[] stats)
stats - an array of FileStatus objectspublic static Path[] stat2Paths(FileStatus[] stats, Path path)
stats - an array of FileStatus objectspath - default path to return in stats is nullpublic static void fullyDeleteOnExit(java.io.File file)
file - File/directory to be deletedpublic static boolean fullyDelete(java.io.File dir)
dir - dir.public static boolean fullyDelete(java.io.File dir,
boolean tryGrantPermissions)
dir - the file or directory to be deletedtryGrantPermissions - true if permissions should be modified to delete a file.public static java.lang.String readLink(java.io.File f)
f - File representing the symbolic link.public static Path fixSymlinkPath(PathData path) throws java.io.IOException
java.io.IOExceptionpublic static Path fixSymlinkFileStatus(FileStatus stat) throws java.io.IOException
java.io.IOExceptionpublic static PathData checkItemForSymlink(PathData item) throws java.io.IOException
java.io.IOExceptionpublic static PathData checkExistItem(PathData item) throws java.io.IOException
java.io.IOExceptionpublic static PathData checkPathForSymlink(Path path, Configuration conf) throws java.io.IOException
java.io.IOExceptionpublic static boolean fullyDeleteContents(java.io.File dir)
dir - dir.public static boolean fullyDeleteContents(java.io.File dir,
boolean tryGrantPermissions)
dir - dir.tryGrantPermissions - if 'true', try grant +rwx permissions to this
and all the underlying directories before trying to delete their contents.@Deprecated public static void fullyDelete(FileSystem fs, Path dir) throws java.io.IOException
fs - FileSystem on which the path is presentdir - directory to recursively deletejava.io.IOException - raised on errors performing I/O.public static boolean copy(FileSystem srcFS, Path src, FileSystem dstFS, Path dst, boolean deleteSource, Configuration conf) throws java.io.IOException
srcFS - src fs.src - src.dstFS - dst fs.dst - dst.deleteSource - delete source.conf - configuration.java.io.IOException - raised on errors performing I/O.public static boolean copy(FileSystem srcFS, Path[] srcs, FileSystem dstFS, Path dst, boolean deleteSource, boolean overwrite, Configuration conf) throws java.io.IOException
java.io.IOExceptionpublic static boolean copy(FileSystem srcFS, Path src, FileSystem dstFS, Path dst, boolean deleteSource, boolean overwrite, Configuration conf) throws java.io.IOException
srcFS - srcFs.src - src.dstFS - dstFs.dst - dst.deleteSource - delete source.overwrite - overwrite.conf - configuration.java.io.IOException - raised on errors performing I/O.public static boolean copy(FileSystem srcFS, FileStatus srcStatus, FileSystem dstFS, Path dst, boolean deleteSource, boolean overwrite, Configuration conf) throws java.io.IOException
returns true if the operation succeeded. When deleteSource is true, this means "after the copy, delete(source) returned true" If the destination is a directory, and mkdirs (dest) fails, the operation will return false rather than raise any exception.
The overwrite flag is about overwriting files; it has no effect about handing an attempt to copy a file atop a directory (expect an IOException), or a directory over a path which contains a file (mkdir will fail, so "false").The operation is recursive, and the deleteSource operation takes place as each subdirectory is copied. Therefore, if an operation fails partway through, the source tree may be partially deleted.
srcFS - source filesystemsrcStatus - status of sourcedstFS - destination filesystemdst - path of sourcedeleteSource - delete the source?overwrite - overwrite files at destination?conf - configuration to use when opening filesjava.io.IOException - failurepublic static boolean copyAsMove(FileSystem srcFS, FileStatus srcStatus, FileSystem dstFS, Path dst, boolean deleteSource, boolean overwrite, Configuration conf) throws java.io.IOException
java.io.IOExceptionpublic static boolean copyMerge(FileSystem srcFS, Path srcDir, FileSystem dstFS, Path dstFile, boolean deleteSource, Configuration conf, java.lang.String addString) throws java.io.IOException
java.io.IOExceptionpublic static boolean copy(java.io.File src,
FileSystem dstFS,
Path dst,
boolean deleteSource,
Configuration conf)
throws java.io.IOException
src - src.dstFS - dstFs.dst - dst.deleteSource - delete source.conf - configuration.java.io.IOException - raised on errors performing I/O.public static boolean copy(FileSystem srcFS, Path src, java.io.File dst, boolean deleteSource, Configuration conf) throws java.io.IOException
srcFS - srcFs.src - src.dst - dst.deleteSource - delete source.conf - configuration.java.io.IOException - raised on errors performing I/O.public static boolean isRegularFile(java.io.File file)
public static boolean isRegularFile(java.io.File file,
boolean allowLinks)
file - The file being checked.allowLinks - Whether to allow matching links.public static java.lang.String makeShellPath(java.lang.String filename)
throws java.io.IOException
filename - The filename to convertjava.io.IOException - on windows, there can be problems with the subprocesspublic static java.lang.String makeShellPath(java.io.File file)
throws java.io.IOException
file - The filename to convertjava.io.IOException - on windows, there can be problems with the subprocesspublic static java.lang.String makeSecureShellPath(java.io.File file)
throws java.io.IOException
file - The filename to convertjava.io.IOException - on windows, there can be problems with the subprocesspublic static java.lang.String makeShellPath(java.io.File file,
boolean makeCanonicalPath)
throws java.io.IOException
file - The filename to convertmakeCanonicalPath - Whether to make canonical path for the file passedjava.io.IOException - on windows, there can be problems with the subprocesspublic static long getDU(java.io.File dir)
dir - The input dir to get the disk space of this local dirpublic static void unZip(java.io.InputStream inputStream,
java.io.File toDir)
throws java.io.IOException
inputStream - The zip file as inputtoDir - The unzip directory where to unzip the zip file.java.io.IOException - an exception occurredpublic static void unZip(java.io.File inFile,
java.io.File unzipDir)
throws java.io.IOException
inFile - The zip file as inputunzipDir - The unzip directory where to unzip the zip file.java.io.IOException - An I/O exception has occurredpublic static void unTar(java.io.InputStream inputStream,
java.io.File untarDir,
boolean gzipped)
throws java.io.IOException,
java.lang.InterruptedException,
java.util.concurrent.ExecutionException
inputStream - The tar file as input.untarDir - The untar directory where to untar the tar file.gzipped - The input stream is gzipped
TODO Use magic number and PusbackInputStream to identifyjava.io.IOException - an exception occurredjava.lang.InterruptedException - command interruptedjava.util.concurrent.ExecutionException - task submit failedpublic static void unTar(java.io.File inFile,
java.io.File untarDir)
throws java.io.IOException
inFile - The tar file as input.untarDir - The untar directory where to untar the tar file.java.io.IOException - an exception occurred.public static int symLink(java.lang.String target,
java.lang.String linkname)
throws java.io.IOException
target - the target for symlinklinkname - the symlinkjava.io.IOException - raised on errors performing I/O.public static int chmod(java.lang.String filename,
java.lang.String perm)
throws java.io.IOException,
java.lang.InterruptedException
filename - the name of the file to changeperm - the permission stringjava.io.IOException - raised on errors performing I/O.java.lang.InterruptedException - command interrupted.public static int chmod(java.lang.String filename,
java.lang.String perm,
boolean recursive)
throws java.io.IOException
filename - name of the file whose permissions are to changeperm - permission stringrecursive - true, if permissions should be changed recursivelyjava.io.IOException - raised on errors performing I/O.public static void setOwner(java.io.File file,
java.lang.String username,
java.lang.String groupname)
throws java.io.IOException
file - the file to changeusername - the new user owner namegroupname - the new group owner namejava.io.IOException - raised on errors performing I/O.public static boolean setReadable(java.io.File f,
boolean readable)
File.setReadable(boolean)
File#setReadable does not work as expected on Windows.f - input filereadable - readable.public static boolean setWritable(java.io.File f,
boolean writable)
File.setWritable(boolean)
File#setWritable does not work as expected on Windows.f - input filewritable - writable.public static boolean setExecutable(java.io.File f,
boolean executable)
File.setExecutable(boolean)
File#setExecutable does not work as expected on Windows.
Note: revoking execute permission on folders does not have the same
behavior on Windows as on Unix platforms. Creating, deleting or renaming
a file within that folder will still succeed on Windows.f - input fileexecutable - executable.public static boolean canRead(java.io.File f)
File.canRead()f - input fileFile.canRead()
On Windows, true if process has read access on the pathpublic static boolean canWrite(java.io.File f)
File.canWrite()f - input fileFile.canWrite()
On Windows, true if process has write access on the pathpublic static boolean canExecute(java.io.File f)
File.canExecute()f - input fileFile.canExecute()
On Windows, true if process has execute access on the pathpublic static void setPermission(java.io.File f,
FsPermission permission)
throws java.io.IOException
f - the file to changepermission - the new permissionsjava.io.IOException - raised on errors performing I/O.public static final java.io.File createLocalTempFile(java.io.File basefile,
java.lang.String prefix,
boolean isDeleteOnExit)
throws java.io.IOException
basefile - the base file of the tmpprefix - file name prefix of tmpisDeleteOnExit - if true, the tmp will be deleted when the VM exitsjava.io.IOException - If a tmp file cannot createdFile.createTempFile(String, String, File),
File.deleteOnExit()public static void replaceFile(java.io.File src,
java.io.File target)
throws java.io.IOException
src - the source filetarget - the target filejava.io.IOException - If this operation failspublic static java.io.File[] listFiles(java.io.File dir)
throws java.io.IOException
File.listFiles(). This java.io API returns null
when a dir is not a directory or for any I/O error. Instead of having
null check everywhere File#listFiles() is used, we will add utility API
to get around this problem. For the majority of cases where we prefer
an IOException to be thrown.dir - directory for which listing should be performedjava.io.IOException - for invalid directory or for a bad disk.public static java.lang.String[] list(java.io.File dir)
throws java.io.IOException
File.list(). This java.io API returns null
when a dir is not a directory or for any I/O error. Instead of having
null check everywhere File#list() is used, we will add utility API
to get around this problem. For the majority of cases where we prefer
an IOException to be thrown.dir - directory for which listing should be performedjava.nio.file.AccessDeniedException - for unreadable directoryjava.io.IOException - for invalid directory or for bad diskpublic static java.lang.String[] createJarWithClassPath(java.lang.String inputClassPath,
Path pwd,
java.util.Map<java.lang.String,java.lang.String> callerEnv)
throws java.io.IOException
java.io.IOExceptionpublic static java.lang.String[] createJarWithClassPath(java.lang.String inputClassPath,
Path pwd,
Path targetDir,
java.util.Map<java.lang.String,java.lang.String> callerEnv)
throws java.io.IOException
inputClassPath - String input classpath to bundle into the jar manifestpwd - Path to working directory to save jartargetDir - path to where the jar execution will have its working dircallerEnv - Map <String, String> caller's
environment variables to use for expansionjava.io.IOException - if there is an I/O error while writing the jar filepublic static java.util.List<Path> getJarsInDirectory(java.lang.String path)
path - the path to the directory. The path may include the wildcard.public static java.util.List<Path> getJarsInDirectory(java.lang.String path, boolean useLocal)
path - the path to the directory. The path may include the wildcard.useLocal - use local.public static boolean compareFs(FileSystem srcFs, FileSystem destFs)
public static FileSystem write(FileSystem fs, Path path, byte[] bytes) throws java.io.IOException
fs - the file system with which to create the filepath - the path to the filebytes - the byte array with the bytes to writejava.lang.NullPointerException - if any of the arguments are nulljava.io.IOException - if an I/O error occurs creating or writing to the filepublic static FileContext write(FileContext fileContext, Path path, byte[] bytes) throws java.io.IOException
fileContext - the file context with which to create the filepath - the path to the filebytes - the byte array with the bytes to writejava.lang.NullPointerException - if any of the arguments are nulljava.io.IOException - if an I/O error occurs creating or writing to the filepublic static FileSystem write(FileSystem fs, Path path, java.lang.Iterable<? extends java.lang.CharSequence> lines, java.nio.charset.Charset cs) throws java.io.IOException
line.separator. Characters are encoded into bytes using the specified
charset. This utility method opens the file for writing, creating the file
if it does not exist, or overwrites an existing file.fs - the file system with which to create the filepath - the path to the filelines - a Collection to iterate over the char sequencescs - the charset to use for encodingjava.lang.NullPointerException - if any of the arguments are nulljava.io.IOException - if an I/O error occurs creating or writing to the filepublic static FileContext write(FileContext fileContext, Path path, java.lang.Iterable<? extends java.lang.CharSequence> lines, java.nio.charset.Charset cs) throws java.io.IOException
line.separator. Characters are encoded into bytes using the specified
charset. This utility method opens the file for writing, creating the file
if it does not exist, or overwrites an existing file.fileContext - the file context with which to create the filepath - the path to the filelines - a Collection to iterate over the char sequencescs - the charset to use for encodingjava.lang.NullPointerException - if any of the arguments are nulljava.io.IOException - if an I/O error occurs creating or writing to the filepublic static FileSystem write(FileSystem fs, Path path, java.lang.CharSequence charseq, java.nio.charset.Charset cs) throws java.io.IOException
fs - the file system with which to create the filepath - the path to the filecharseq - the char sequence to write to the filecs - the charset to use for encodingjava.lang.NullPointerException - if any of the arguments are nulljava.io.IOException - if an I/O error occurs creating or writing to the filepublic static FileContext write(FileContext fs, Path path, java.lang.CharSequence charseq, java.nio.charset.Charset cs) throws java.io.IOException
fs - the file context with which to create the filepath - the path to the filecharseq - the char sequence to write to the filecs - the charset to use for encodingjava.lang.NullPointerException - if any of the arguments are nulljava.io.IOException - if an I/O error occurs creating or writing to the filepublic static FileSystem write(FileSystem fs, Path path, java.lang.CharSequence charseq) throws java.io.IOException
fs - the files system with which to create the filepath - the path to the filecharseq - the char sequence to write to the filejava.lang.NullPointerException - if any of the arguments are nulljava.io.IOException - if an I/O error occurs creating or writing to the filepublic static FileContext write(FileContext fileContext, Path path, java.lang.CharSequence charseq) throws java.io.IOException
fileContext - the files system with which to create the filepath - the path to the filecharseq - the char sequence to write to the filejava.lang.NullPointerException - if any of the arguments are nulljava.io.IOException - if an I/O error occurs creating or writing to the file@LimitedPrivate("ViewDistributedFileSystem")
@Unstable
public static void rename(FileSystem srcFs,
Path src,
Path dst,
Options.Rename... options)
throws java.io.IOException
java.io.IOExceptionpublic static void maybeIgnoreMissingDirectory(FileSystem fs, Path path, java.io.FileNotFoundException e) throws java.io.FileNotFoundException
fs - filesystempath - pathe - exception caughtjava.io.FileNotFoundException - the exception passed in, if rethrown.Copyright © 2008–2025 Apache Software Foundation. All rights reserved.