public class FileUtils extends Object
Modifier and Type | Field and Description |
---|---|
static int |
EMPTY_DIRECTORIES_ONLY
Option to only delete empty directories.
|
static int |
IGNORE_ERRORS
Option not to throw exceptions when a deletion finally doesn't succeed.
|
static int |
NONE
Option to delete given
File |
static int |
RECURSIVE
Option to recursively delete given
File |
static int |
RETRY
Option to retry deletion if not successful
|
static int |
SKIP_MISSING
Option to skip deletion if file doesn't exist
|
Constructor and Description |
---|
FileUtils() |
Modifier and Type | Method and Description |
---|---|
static boolean |
canExecute(File file) |
static File |
canonicalize(File file)
Best-effort variation of
File.getCanonicalFile() returning the
input file if the file cannot be canonicalized instead of throwing
IOException . |
static void |
createNewFile(File f)
Atomically creates a new, empty file named by this abstract pathname if
and only if a file with this name does not yet exist.
|
static Path |
createSymLink(File path,
String target)
Create a symbolic link
|
static File |
createTempDir(String prefix,
String suffix,
File dir)
Create a temporary directory.
|
static void |
delete(File f)
Delete file or empty folder
|
static void |
delete(File f,
int options)
Delete file or folder
|
static FS.Attributes |
getFileAttributesPosix(FS fs,
File file) |
static long |
getLength(File file) |
static boolean |
isStaleFileHandle(IOException ioe)
Determine if an IOException is a Stale NFS File Handle
|
static void |
mkdir(File d)
Creates the directory named by this abstract pathname.
|
static void |
mkdir(File d,
boolean skipExisting)
Creates the directory named by this abstract pathname.
|
static void |
mkdirs(File d)
Creates the directory named by this abstract pathname, including any
necessary but nonexistent parent directories.
|
static void |
mkdirs(File d,
boolean skipExisting)
Creates the directory named by this abstract pathname, including any
necessary but nonexistent parent directories.
|
static File |
normalize(File file) |
static String |
normalize(String name) |
static String |
readSymLink(File path) |
static String |
relativize(String base,
String other)
This will try and make a given path relative to another.
|
static void |
rename(File src,
File dst)
Rename a file or folder.
|
static void |
rename(File src,
File dst,
CopyOption... options)
Rename a file or folder using the passed
CopyOption s. |
static void |
setHidden(File file,
boolean hidden) |
public static final int NONE
File
public static final int RECURSIVE
File
public static final int RETRY
public static final int SKIP_MISSING
public static final int IGNORE_ERRORS
public static final int EMPTY_DIRECTORIES_ONLY
RECURSIVE
public static void delete(File f) throws IOException
f
- File
to be deletedIOException
- if deletion of f
fails. This may occur if f
didn't exist when the method was called. This can therefore
cause IOExceptions during race conditions when multiple
concurrent threads all try to delete the same file.public static void delete(File f, int options) throws IOException
f
- File
to be deletedoptions
- deletion options, RECURSIVE
for recursive deletion of
a subtree, RETRY
to retry when deletion failed.
Retrying may help if the underlying file system doesn't allow
deletion of files being read by another thread.IOException
- if deletion of f
fails. This may occur if f
didn't exist when the method was called. This can therefore
cause IOExceptions during race conditions when multiple
concurrent threads all try to delete the same file. This
exception is not thrown when IGNORE_ERRORS is set.public static void rename(File src, File dst) throws IOException
This operation is not atomic.
src
- the old File
dst
- the new File
IOException
- if the rename has failedFS.retryFailedLockFileCommit()
public static void rename(File src, File dst, CopyOption... options) throws AtomicMoveNotSupportedException, IOException
CopyOption
s. If the
rename fails and if we are running on a filesystem where it makes sense
to repeat a failing rename then repeat the rename operation up to 9 times
with 100ms sleep time between two calls. Furthermore if the destination
exists and is a directory hierarchy with only directories in it, the
whole directory hierarchy will be deleted. If the target represents a
non-empty directory structure, empty subdirectories within that structure
may or may not be deleted even if the method fails. Furthermore if the
destination exists and is a file then the file will be replaced if
StandardCopyOption.REPLACE_EXISTING
has been set. If
StandardCopyOption.ATOMIC_MOVE
has been set the rename will be
done atomically or fail with an AtomicMoveNotSupportedException
src
- the old filedst
- the new fileoptions
- options to pass to
Files.move(java.nio.file.Path, java.nio.file.Path, CopyOption...)
AtomicMoveNotSupportedException
- if file cannot be moved as an atomic file system operationIOException
public static void mkdir(File d) throws IOException
d
- directory to be createdIOException
- if creation of d
fails. This may occur if d
did exist when the method was called. This can therefore
cause IOExceptions during race conditions when multiple
concurrent threads all try to create the same directory.public static void mkdir(File d, boolean skipExisting) throws IOException
d
- directory to be createdskipExisting
- if true
skip creation of the given directory if it
already exists in the file systemIOException
- if creation of d
fails. This may occur if d
did exist when the method was called. This can therefore
cause IOExceptions during race conditions when multiple
concurrent threads all try to create the same directory.public static void mkdirs(File d) throws IOException
d
- directory to be createdIOException
- if creation of d
fails. This may occur if d
did exist when the method was called. This can therefore
cause IOExceptions during race conditions when multiple
concurrent threads all try to create the same directory.public static void mkdirs(File d, boolean skipExisting) throws IOException
d
- directory to be createdskipExisting
- if true
skip creation of the given directory if it
already exists in the file systemIOException
- if creation of d
fails. This may occur if d
did exist when the method was called. This can therefore
cause IOExceptions during race conditions when multiple
concurrent threads all try to create the same directory.public static void createNewFile(File f) throws IOException
Note: this method should not be used for file-locking, as the resulting
protocol cannot be made to work reliably. The FileLock
facility
should be used instead.
f
- the file to be createdIOException
- if the named file already exists or if an I/O error occurredpublic static Path createSymLink(File path, String target) throws IOException
path
- the path of the symbolic link to createtarget
- the target of the symbolic linkIOException
public static String readSymLink(File path) throws IOException
path
- IOException
public static File createTempDir(String prefix, String suffix, File dir) throws IOException
prefix
- suffix
- dir
- The parent dir, can be null to use system default temp dir.IOException
public static String relativize(String base, String other)
For example, if this is called with the two following paths :
This will return "..\\another_project\\pom.xml".base = "c:\\Users\\jdoe\\eclipse\\git\\project"
other = "c:\\Users\\jdoe\\eclipse\\git\\another_project\\pom.xml"
This method uses File.separator
to split the paths into segments.
Note that this will return the empty String if base
and other
are equal.
base
- The path against which other
should be
relativized. This will be assumed to denote the path to a
folder and not a file.other
- The path that will be made relative to base
.base
,
will yield the original other
.public static boolean isStaleFileHandle(IOException ioe)
ioe
- public static void setHidden(File file, boolean hidden) throws IOException
file
- hidden
- IOException
public static long getLength(File file) throws IOException
file
- IOException
public static boolean canExecute(File file)
file
- true
if the given file can be executedpublic static FS.Attributes getFileAttributesPosix(FS fs, File file)
fs
- file
- public static File normalize(File file)
file
- File
, otherwise the passed filepublic static String normalize(String name)
name
- public static File canonicalize(File file)
File.getCanonicalFile()
returning the
input file if the file cannot be canonicalized instead of throwing
IOException
.file
- to be canonicalized; may be null
file == null
SecurityException
- if File.getCanonicalFile()
throws oneCopyright © 2016 Eclipse JGit Project. All rights reserved.