@InterfaceAudience.Public @InterfaceStability.Evolving public class FileUtil extends Object
Modifier and Type | Field and Description |
---|---|
static int |
SYMLINK_NO_PRIVILEGE |
Constructor and Description |
---|
FileUtil() |
Modifier and Type | Method and Description |
---|---|
static boolean |
canExecute(File f)
Platform independent implementation for
File.canExecute() |
static boolean |
canRead(File f)
Platform independent implementation for
File.canRead() |
static boolean |
canWrite(File f)
Platform independent implementation for
File.canWrite() |
static int |
chmod(String filename,
String perm)
Change the permissions on a filename.
|
static int |
chmod(String filename,
String perm,
boolean recursive)
Change the permissions on a file / directory, recursively, if
needed.
|
static boolean |
compareFs(FileSystem srcFs,
FileSystem destFs) |
static boolean |
copy(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 files 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,
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 |
copyMerge(FileSystem srcFS,
Path srcDir,
FileSystem dstFS,
Path dstFile,
boolean deleteSource,
Configuration conf,
String addString)
Deprecated.
|
static String[] |
createJarWithClassPath(String inputClassPath,
Path pwd,
Map<String,String> callerEnv) |
static String[] |
createJarWithClassPath(String inputClassPath,
Path pwd,
Path targetDir,
Map<String,String> callerEnv)
Create a jar file at the given path, containing a manifest with a classpath
that references all specified entries.
|
static File |
createLocalTempFile(File basefile,
String prefix,
boolean isDeleteOnExit)
Create a tmp file for a base file.
|
static boolean |
fullyDelete(File dir)
Delete a directory and all its contents.
|
static boolean |
fullyDelete(File dir,
boolean tryGrantPermissions)
Delete a directory and all its contents.
|
static void |
fullyDelete(FileSystem fs,
Path dir)
Deprecated.
|
static boolean |
fullyDeleteContents(File dir)
Delete the contents of a directory, not the directory itself.
|
static boolean |
fullyDeleteContents(File dir,
boolean tryGrantPermissions)
Delete the contents of a directory, not the directory itself.
|
static long |
getDU(File dir)
Takes an input dir and returns the du on that local directory.
|
static String[] |
list(File dir)
A wrapper for
File.list() . |
static File[] |
listFiles(File dir)
A wrapper for
File.listFiles() . |
static String |
makeShellPath(File file)
Convert a os-native filename to a path that works for the shell.
|
static String |
makeShellPath(File file,
boolean makeCanonicalPath)
Convert a os-native filename to a path that works for the shell.
|
static String |
makeShellPath(String filename)
Convert a os-native filename to a path that works for the shell.
|
static String |
readLink(File f)
Returns the target of the given symlink.
|
static void |
replaceFile(File src,
File target)
Move the src file to the name specified by target.
|
static boolean |
setExecutable(File f,
boolean executable)
Platform independent implementation for
File.setExecutable(boolean)
File#setExecutable does not work as expected on Windows. |
static void |
setOwner(File file,
String username,
String groupname)
Set the ownership on a file / directory.
|
static void |
setPermission(File f,
FsPermission permission)
Set permissions to the required value.
|
static boolean |
setReadable(File f,
boolean readable)
Platform independent implementation for
File.setReadable(boolean)
File#setReadable does not work as expected on Windows. |
static boolean |
setWritable(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(String target,
String linkname)
Create a soft link between a src and destination
only on a local disk.
|
static void |
unTar(File inFile,
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 |
unZip(File inFile,
File unzipDir)
Given a File input it will unzip the file in a the unzip directory
passed as the second parameter
|
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 boolean fullyDelete(File dir)
public static boolean fullyDelete(File dir, boolean tryGrantPermissions)
dir
- the file or directory to be deletedtryGrantPermissions
- true if permissions should be modified to delete a file.public static String readLink(File f)
f
- File representing the symbolic link.public static boolean fullyDeleteContents(File dir)
public static boolean fullyDeleteContents(File dir, boolean tryGrantPermissions)
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 IOException
FileSystem.delete(Path, boolean)
fs
- FileSystem
on which the path is presentdir
- directory to recursively deleteIOException
public static boolean copy(FileSystem srcFS, Path src, FileSystem dstFS, Path dst, boolean deleteSource, Configuration conf) throws IOException
IOException
public static boolean copy(FileSystem srcFS, Path[] srcs, FileSystem dstFS, Path dst, boolean deleteSource, boolean overwrite, Configuration conf) throws IOException
IOException
public static boolean copy(FileSystem srcFS, Path src, FileSystem dstFS, Path dst, boolean deleteSource, boolean overwrite, Configuration conf) throws IOException
IOException
public static boolean copy(FileSystem srcFS, FileStatus srcStatus, FileSystem dstFS, Path dst, boolean deleteSource, boolean overwrite, Configuration conf) throws IOException
IOException
@Deprecated public static boolean copyMerge(FileSystem srcFS, Path srcDir, FileSystem dstFS, Path dstFile, boolean deleteSource, Configuration conf, String addString) throws IOException
IOException
public static boolean copy(File src, FileSystem dstFS, Path dst, boolean deleteSource, Configuration conf) throws IOException
IOException
public static boolean copy(FileSystem srcFS, Path src, File dst, boolean deleteSource, Configuration conf) throws IOException
IOException
public static String makeShellPath(String filename) throws IOException
filename
- The filename to convertIOException
- on windows, there can be problems with the subprocesspublic static String makeShellPath(File file) throws IOException
file
- The filename to convertIOException
- on windows, there can be problems with the subprocesspublic static String makeShellPath(File file, boolean makeCanonicalPath) throws IOException
file
- The filename to convertmakeCanonicalPath
- Whether to make canonical path for the file passedIOException
- on windows, there can be problems with the subprocesspublic static long getDU(File dir)
dir
- The input dir to get the disk space of this local dirpublic static void unZip(File inFile, File unzipDir) throws IOException
inFile
- The zip file as inputunzipDir
- The unzip directory where to unzip the zip file.IOException
public static void unTar(File inFile, File untarDir) throws IOException
inFile
- The tar file as input.untarDir
- The untar directory where to untar the tar file.IOException
public static int symLink(String target, String linkname) throws IOException
target
- the target for symlinklinkname
- the symlinkIOException
public static int chmod(String filename, String perm) throws IOException, InterruptedException
filename
- the name of the file to changeperm
- the permission stringIOException
InterruptedException
public static int chmod(String filename, String perm, boolean recursive) throws IOException
filename
- name of the file whose permissions are to changeperm
- permission stringrecursive
- true, if permissions should be changed recursivelyIOException
public static void setOwner(File file, String username, String groupname) throws IOException
file
- the file to changeusername
- the new user owner namegroupname
- the new group owner nameIOException
public static boolean setReadable(File f, boolean readable)
File.setReadable(boolean)
File#setReadable does not work as expected on Windows.f
- input filereadable
- public static boolean setWritable(File f, boolean writable)
File.setWritable(boolean)
File#setWritable does not work as expected on Windows.f
- input filewritable
- public static boolean setExecutable(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
- public static boolean canRead(File f)
File.canRead()
f
- input fileFile.canRead()
On Windows, true if process has read access on the pathpublic static boolean canWrite(File f)
File.canWrite()
f
- input fileFile.canWrite()
On Windows, true if process has write access on the pathpublic static boolean canExecute(File f)
File.canExecute()
f
- input fileFile.canExecute()
On Windows, true if process has execute access on the pathpublic static void setPermission(File f, FsPermission permission) throws IOException
f
- the file to changepermission
- the new permissionsIOException
public static final File createLocalTempFile(File basefile, String prefix, boolean isDeleteOnExit) throws IOException
basefile
- the base file of the tmpprefix
- file name prefix of tmpisDeleteOnExit
- if true, the tmp will be deleted when the VM exitsIOException
- If a tmp file cannot createdFile.createTempFile(String, String, File)
,
File.deleteOnExit()
public static void replaceFile(File src, File target) throws IOException
src
- the source filetarget
- the target fileIOException
- If this operation failspublic static File[] listFiles(File dir) throws 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 performedIOException
- for invalid directory or for a bad disk.public static String[] list(File dir) throws 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 performedIOException
- for invalid directory or for a bad disk.public static String[] createJarWithClassPath(String inputClassPath, Path pwd, Map<String,String> callerEnv) throws IOException
IOException
public static String[] createJarWithClassPath(String inputClassPath, Path pwd, Path targetDir, Map<String,String> callerEnv) throws 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
- MapIOException
- if there is an I/O error while writing the jar filepublic static boolean compareFs(FileSystem srcFs, FileSystem destFs)
Copyright © 2017 Apache Software Foundation. All rights reserved.