public class FileUtils
extends java.lang.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.
|
private static org.slf4j.Logger |
LOG |
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
|
private static java.util.Random |
RNG |
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(java.io.File file)
Whether the given file can be executed.
|
static java.io.File |
canonicalize(java.io.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(java.io.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 java.nio.file.Path |
createSymLink(java.io.File path,
java.lang.String target)
Create a symbolic link
|
static java.io.File |
createTempDir(java.lang.String prefix,
java.lang.String suffix,
java.io.File dir)
Create a temporary directory.
|
static long |
delay(long last,
long min,
long max)
Compute a delay in a
min..max interval with random jitter. |
static void |
delete(java.io.File f)
Delete file or empty folder
|
static void |
delete(java.io.File f,
int options)
Delete file or folder
|
(package private) static boolean |
exists(java.io.File file) |
(package private) static java.nio.file.attribute.BasicFileAttributes |
fileAttributes(java.io.File file)
Return all the attributes of a file, without following symbolic links.
|
(package private) static FS.Attributes |
getFileAttributesBasic(FS fs,
java.io.File file) |
static FS.Attributes |
getFileAttributesPosix(FS fs,
java.io.File file)
Get file system attributes for the given file.
|
static long |
getLength(java.io.File file)
Get file length
|
(package private) static boolean |
isDirectory(java.io.File file) |
(package private) static boolean |
isFile(java.io.File file) |
(package private) static boolean |
isHidden(java.io.File file) |
static boolean |
isStaleFileHandle(java.io.IOException ioe)
Determine if an IOException is a Stale NFS File Handle
|
static boolean |
isStaleFileHandleInCausalChain(java.lang.Throwable throwable)
Determine if a throwable or a cause in its causal chain is a Stale NFS
File Handle
|
(package private) static boolean |
isSymlink(java.io.File file) |
(package private) static long |
lastModified(java.io.File file)
Deprecated.
use
lastModifiedInstant(Path) instead which returns
FileTime |
(package private) static java.time.Instant |
lastModifiedInstant(java.nio.file.Path path) |
static void |
mkdir(java.io.File d)
Creates the directory named by this abstract pathname.
|
static void |
mkdir(java.io.File d,
boolean skipExisting)
Creates the directory named by this abstract pathname.
|
static void |
mkdirs(java.io.File d)
Creates the directory named by this abstract pathname, including any
necessary but nonexistent parent directories.
|
static void |
mkdirs(java.io.File d,
boolean skipExisting)
Creates the directory named by this abstract pathname, including any
necessary but nonexistent parent directories.
|
static java.io.File |
normalize(java.io.File file)
NFC normalize a file (on Mac), otherwise do nothing
|
static java.lang.String |
normalize(java.lang.String name)
On Mac: get NFC normalized form of given name, otherwise the given name.
|
static java.lang.String |
pathToString(java.io.File file)
Convert a path to String, replacing separators as necessary.
|
static java.lang.String |
readSymLink(java.io.File path)
Read target path of the symlink.
|
static java.lang.String |
relativizeGitPath(java.lang.String base,
java.lang.String other)
Expresses
other as a relative file path from
base . |
static java.lang.String |
relativizeNativePath(java.lang.String base,
java.lang.String other)
Expresses
other as a relative file path from
base . |
static java.lang.String |
relativizePath(java.lang.String base,
java.lang.String other,
java.lang.String dirSeparator,
boolean caseSensitive)
Expresses
other as a relative file path from base |
static void |
rename(java.io.File src,
java.io.File dst)
Rename a file or folder.
|
static void |
rename(java.io.File src,
java.io.File dst,
java.nio.file.CopyOption... options)
Rename a file or folder using the passed
CopyOption s. |
static void |
setHidden(java.io.File file,
boolean hidden)
Set a file hidden (on Windows)
|
(package private) static void |
setLastModified(java.io.File file,
long time)
Deprecated.
|
(package private) static void |
setLastModified(java.nio.file.Path path,
java.time.Instant time) |
static java.nio.file.Path |
toPath(java.io.File f)
Safe conversion from
File to Path . |
static void |
touch(java.nio.file.Path f)
Touch the given file
|
private static final org.slf4j.Logger LOG
private static final java.util.Random RNG
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 java.nio.file.Path toPath(java.io.File f) throws java.io.IOException
File
to Path
.f
- File
to be converted to Path
java.io.IOException
- in case the path represented by the file is not valid (
InvalidPathException
)public static void delete(java.io.File f) throws java.io.IOException
f
- File
to be deletedjava.io.IOException
- if deletion of f
fails. This may occur if f
didn't exist when the method was called. This can therefore
cause java.io.IOExceptions during race conditions when
multiple concurrent threads all try to delete the same file.public static void delete(java.io.File f, int options) throws java.io.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.java.io.IOException
- if deletion of f
fails. This may occur if f
didn't exist when the method was called. This can therefore
cause java.io.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(java.io.File src, java.io.File dst) throws java.io.IOException
This operation is not atomic.
src
- the old File
dst
- the new File
java.io.IOException
- if the rename has failedFS.retryFailedLockFileCommit()
public static void rename(java.io.File src, java.io.File dst, java.nio.file.CopyOption... options) throws java.nio.file.AtomicMoveNotSupportedException, java.io.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...)
java.nio.file.AtomicMoveNotSupportedException
- if file cannot be moved as an atomic file system operationjava.io.IOException
public static void mkdir(java.io.File d) throws java.io.IOException
d
- directory to be createdjava.io.IOException
- if creation of d
fails. This may occur if d
did exist when the method was called. This can therefore
cause java.io.IOExceptions during race conditions when
multiple concurrent threads all try to create the same
directory.public static void mkdir(java.io.File d, boolean skipExisting) throws java.io.IOException
d
- directory to be createdskipExisting
- if true
skip creation of the given directory if it
already exists in the file systemjava.io.IOException
- if creation of d
fails. This may occur if d
did exist when the method was called. This can therefore
cause java.io.IOExceptions during race conditions when
multiple concurrent threads all try to create the same
directory.public static void mkdirs(java.io.File d) throws java.io.IOException
d
- directory to be createdjava.io.IOException
- if creation of d
fails. This may occur if d
did exist when the method was called. This can therefore
cause java.io.IOExceptions during race conditions when
multiple concurrent threads all try to create the same
directory.public static void mkdirs(java.io.File d, boolean skipExisting) throws java.io.IOException
d
- directory to be createdskipExisting
- if true
skip creation of the given directory if it
already exists in the file systemjava.io.IOException
- if creation of d
fails. This may occur if d
did exist when the method was called. This can therefore
cause java.io.IOExceptions during race conditions when
multiple concurrent threads all try to create the same
directory.public static void createNewFile(java.io.File f) throws java.io.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 createdjava.io.IOException
- if the named file already exists or if an I/O error occurredpublic static java.nio.file.Path createSymLink(java.io.File path, java.lang.String target) throws java.io.IOException
path
- the path of the symbolic link to createtarget
- the target of the symbolic linkjava.io.IOException
public static java.lang.String readSymLink(java.io.File path) throws java.io.IOException
path
- a File
object.java.io.IOException
public static java.io.File createTempDir(java.lang.String prefix, java.lang.String suffix, java.io.File dir) throws java.io.IOException
prefix
- prefix stringsuffix
- suffix stringdir
- The parent dir, can be null to use system default temp dir.java.io.IOException
public static java.lang.String relativizeNativePath(java.lang.String base, java.lang.String other)
other
as a relative file path from
base
. File-separator and case sensitivity are based on the
current file system.
See also
relativizePath(String, String, String, boolean)
.base
- Base pathother
- Destination pathbase
to other
public static java.lang.String relativizeGitPath(java.lang.String base, java.lang.String other)
other
as a relative file path from
base
. File-separator and case sensitivity are based on Git's
internal representation of files (which matches Unix).
See also
relativizePath(String, String, String, boolean)
.base
- Base pathother
- Destination pathbase
to other
public static java.lang.String relativizePath(java.lang.String base, java.lang.String other, java.lang.String dirSeparator, boolean caseSensitive)
other
as a relative file path from base
For example, if 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"
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
.dirSeparator
- A string that separates components of the path. In practice, this is "/" or "\\".caseSensitive
- Whether to consider differently-cased directory names as distinctbase
,
will yield the original other
.public static boolean isStaleFileHandle(java.io.IOException ioe)
ioe
- an IOException
object.public static boolean isStaleFileHandleInCausalChain(java.lang.Throwable throwable)
throwable
- a Throwable
object.static boolean isSymlink(java.io.File file)
file
- true
if the passed file is a symbolic link@Deprecated static long lastModified(java.io.File file) throws java.io.IOException
lastModifiedInstant(Path)
instead which returns
FileTimefile
- java.io.IOException
static java.time.Instant lastModifiedInstant(java.nio.file.Path path)
path
- static java.nio.file.attribute.BasicFileAttributes fileAttributes(java.io.File file) throws java.io.IOException
file
- BasicFileAttributes
of the filejava.io.IOException
- in case of any I/O errors accessing the file@Deprecated static void setLastModified(java.io.File file, long time) throws java.io.IOException
file
- time
- java.io.IOException
static void setLastModified(java.nio.file.Path path, java.time.Instant time) throws java.io.IOException
path
- time
- java.io.IOException
static boolean exists(java.io.File file)
file
- true
if the given file exists, not following symbolic
linksstatic boolean isHidden(java.io.File file) throws java.io.IOException
file
- true
if the given file is hiddenjava.io.IOException
public static void setHidden(java.io.File file, boolean hidden) throws java.io.IOException
file
- a File
object.hidden
- a boolean.java.io.IOException
public static long getLength(java.io.File file) throws java.io.IOException
file
- a File
.java.io.IOException
static boolean isDirectory(java.io.File file)
file
- true
if the given file is a directory, not following
symbolic linksstatic boolean isFile(java.io.File file)
file
- true
if the given file is a file, not following symbolic
linkspublic static boolean canExecute(java.io.File file)
file
- a File
object.true
if the given file can be executed.static FS.Attributes getFileAttributesBasic(FS fs, java.io.File file)
fs
- file
- public static FS.Attributes getFileAttributesPosix(FS fs, java.io.File file)
fs
- a FS
object.file
- a File
.public static java.io.File normalize(java.io.File file)
file
- a File
.File
, otherwise the passed
filepublic static java.lang.String normalize(java.lang.String name)
name
- a String
object.public static java.io.File canonicalize(java.io.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
java.lang.SecurityException
- if File.getCanonicalFile()
throws onepublic static java.lang.String pathToString(java.io.File file)
file
- a File
.public static void touch(java.nio.file.Path f) throws java.io.IOException
f
- the file to touchjava.io.IOException
public static long delay(long last, long min, long max)
min..max
interval with random jitter.last
- amount of delay waited before the last attempt. This is used
to seed the next delay interval. Should be 0 if there was no
prior delay.min
- shortest amount of allowable delay between attempts.max
- longest amount of allowable delay between attempts.