public class FileUtils
extends java.lang.Object
| Constructor and Description |
|---|
FileUtils() |
| Modifier and Type | Method and Description |
|---|---|
static boolean |
canWrite(java.lang.String fileName)
Check if the file is writable.
|
static void |
createDirectories(java.lang.String dir)
Create the directory and all required parent directories.
|
static void |
createDirectory(java.lang.String directoryName)
Create a directory (all required parent directories must already exist).
|
static boolean |
createFile(java.lang.String fileName)
Create a new file.
|
static java.lang.String |
createTempFile(java.lang.String prefix,
java.lang.String suffix,
boolean deleteOnExit,
boolean inTempDir)
Create a new temporary file.
|
static void |
delete(java.lang.String path)
Delete a file or directory if it exists.
|
static void |
deleteRecursive(java.lang.String path,
boolean tryOnly)
Delete a directory or file and all subdirectories and files.
|
static boolean |
exists(java.lang.String fileName)
Checks if a file exists.
|
static java.lang.String |
getName(java.lang.String path)
Get the file or directory name (the last element of the path).
|
static java.lang.String |
getParent(java.lang.String fileName)
Get the parent directory of a file or directory.
|
static boolean |
isAbsolute(java.lang.String fileName)
Check if the file name includes a path.
|
static boolean |
isDirectory(java.lang.String fileName)
Check if it is a file or a directory.
|
static long |
lastModified(java.lang.String fileName)
Get the last modified date of a file.
|
static void |
move(java.lang.String source,
java.lang.String target)
Rename a file if this is allowed.
|
static void |
moveAtomicReplace(java.lang.String source,
java.lang.String target)
Rename a file if this is allowed, and try to atomically replace an
existing file.
|
static java.util.List<java.lang.String> |
newDirectoryStream(java.lang.String path)
List the files and directories in the given directory.
|
static java.io.InputStream |
newInputStream(java.lang.String fileName)
Create an input stream to read from the file.
|
static java.io.OutputStream |
newOutputStream(java.lang.String fileName,
boolean append)
Create an output stream to write into the file.
|
static java.nio.channels.FileChannel |
open(java.lang.String fileName,
java.lang.String mode)
Open a random access file object.
|
static void |
readFully(java.nio.channels.FileChannel channel,
java.nio.ByteBuffer dst)
Fully read from the file.
|
static boolean |
setReadOnly(java.lang.String fileName)
Disable the ability to write.
|
static long |
size(java.lang.String fileName)
Get the size of a file in bytes
This method is similar to Java 7
java.nio.file.attribute.Attributes. |
static java.lang.String |
toRealPath(java.lang.String fileName)
Get the canonical file or directory name.
|
static boolean |
tryDelete(java.lang.String path)
Try to delete a file or directory (ignoring errors).
|
static java.lang.String |
unwrap(java.lang.String fileName)
Get the unwrapped file name (without wrapper prefixes if wrapping /
delegating file systems are used).
|
static void |
writeFully(java.nio.channels.FileChannel channel,
java.nio.ByteBuffer src)
Fully write to the file.
|
public static boolean exists(java.lang.String fileName)
java.nio.file.Path.exists.fileName - the file namepublic static void createDirectory(java.lang.String directoryName)
java.nio.file.Path.createDirectory.directoryName - the directory namepublic static boolean createFile(java.lang.String fileName)
java.nio.file.Path.createFile, but returns false instead of
throwing a exception if the file already existed.fileName - the file namepublic static void delete(java.lang.String path)
java.nio.file.Path.deleteIfExists.path - the file or directory namepublic static java.lang.String toRealPath(java.lang.String fileName)
java.nio.file.Path.toRealPath.fileName - the file namepublic static java.lang.String getParent(java.lang.String fileName)
java.nio.file.Path.getParent.fileName - the file or directory namepublic static boolean isAbsolute(java.lang.String fileName)
java.nio.file.Path.isAbsolute.fileName - the file namepublic static void move(java.lang.String source,
java.lang.String target)
java.nio.file.Files.move.source - the old fully qualified file nametarget - the new fully qualified file namepublic static void moveAtomicReplace(java.lang.String source,
java.lang.String target)
java.nio.file.Files.move.source - the old fully qualified file nametarget - the new fully qualified file namepublic static java.lang.String getName(java.lang.String path)
java.nio.file.Path.getName.path - the directory and file namepublic static java.util.List<java.lang.String> newDirectoryStream(java.lang.String path)
java.nio.file.Path.newDirectoryStream.path - the directorypublic static long lastModified(java.lang.String fileName)
java.nio.file.attribute.Attributes.
readBasicFileAttributes(file).lastModified().toMillis()fileName - the file namepublic static long size(java.lang.String fileName)
java.nio.file.attribute.Attributes.
readBasicFileAttributes(file).size()fileName - the file namepublic static boolean isDirectory(java.lang.String fileName)
java.nio.file.attribute.Attributes.
readBasicFileAttributes(file).isDirectory()fileName - the file or directory namepublic static java.nio.channels.FileChannel open(java.lang.String fileName,
java.lang.String mode)
throws java.io.IOException
java.nio.channels.FileChannel.open.fileName - the file namemode - the access mode. Supported are r, rw, rws, rwdjava.io.IOExceptionpublic static java.io.InputStream newInputStream(java.lang.String fileName)
throws java.io.IOException
java.nio.file.Path.newInputStream.fileName - the file namejava.io.IOExceptionpublic static java.io.OutputStream newOutputStream(java.lang.String fileName,
boolean append)
throws java.io.IOException
java.nio.file.Path.newOutputStream.fileName - the file nameappend - if true, the file will grow, if false, the file will be
truncated firstjava.io.IOExceptionpublic static boolean canWrite(java.lang.String fileName)
java.nio.file.Path.checkAccess(AccessMode.WRITE)fileName - the file namepublic static boolean setReadOnly(java.lang.String fileName)
fileName - the file namepublic static java.lang.String unwrap(java.lang.String fileName)
fileName - the file namepublic static void deleteRecursive(java.lang.String path,
boolean tryOnly)
path - the pathtryOnly - whether errors should be ignoredpublic static void createDirectories(java.lang.String dir)
dir - the directory namepublic static boolean tryDelete(java.lang.String path)
path - the file or directory namepublic static java.lang.String createTempFile(java.lang.String prefix,
java.lang.String suffix,
boolean deleteOnExit,
boolean inTempDir)
throws java.io.IOException
prefix - the prefix of the file name (including directory name if
required)suffix - the suffixdeleteOnExit - if the file should be deleted when the virtual
machine existsinTempDir - if the file should be stored in the temporary directoryjava.io.IOExceptionpublic static void readFully(java.nio.channels.FileChannel channel,
java.nio.ByteBuffer dst)
throws java.io.IOException
channel - the file channeldst - the byte bufferjava.io.IOExceptionpublic static void writeFully(java.nio.channels.FileChannel channel,
java.nio.ByteBuffer src)
throws java.io.IOException
channel - the file channelsrc - the byte bufferjava.io.IOException