public class FileUtils extends Object
Constructor and Description |
---|
FileUtils() |
Modifier and Type | Method and Description |
---|---|
static boolean |
canWrite(String fileName)
Check if the file is writable.
|
static void |
copy(String original,
String copy)
Copy a file from one directory to another, or to another file.
|
static void |
createDirectories(String dir)
Create the directory and all required parent directories.
|
static void |
createDirectory(String directoryName)
Create a directory (all required parent directories must already exist).
|
static boolean |
createFile(String fileName)
Create a new file.
|
static String |
createTempFile(String prefix,
String suffix,
boolean deleteOnExit,
boolean inTempDir)
Create a new temporary file.
|
static void |
delete(String path)
Delete a file or directory if it exists.
|
static void |
deleteRecursive(String path,
boolean tryOnly)
Delete a directory or file and all subdirectories and files.
|
static boolean |
exists(String fileName)
Checks if a file exists.
|
static String |
getName(String path)
Get the file or directory name (the last element of the path).
|
static String |
getParent(String fileName)
Get the parent directory of a file or directory.
|
static boolean |
isAbsolute(String fileName)
Check if the file name includes a path.
|
static boolean |
isDirectory(String fileName)
Check if it is a file or a directory.
|
static long |
lastModified(String fileName)
Get the last modified date of a file.
|
static void |
moveTo(String oldName,
String newName)
Rename a file if this is allowed.
|
static List<String> |
newDirectoryStream(String path)
List the files and directories in the given directory.
|
static InputStream |
newInputStream(String fileName)
Create an input stream to read from the file.
|
static OutputStream |
newOutputStream(String fileName,
boolean append)
Create an output stream to write into the file.
|
static FileChannel |
open(String fileName,
String mode)
Open a random access file object.
|
static void |
readFully(FileChannel channel,
ByteBuffer dst)
Fully read from the file.
|
static boolean |
setReadOnly(String fileName)
Disable the ability to write.
|
static long |
size(String fileName)
Get the size of a file in bytes
This method is similar to Java 7
java.nio.file.attribute.Attributes. |
static String |
toRealPath(String fileName)
Get the canonical file or directory name.
|
static boolean |
tryDelete(String fileName)
Try to delete a file (ignore errors).
|
static String |
unwrap(String fileName)
Get the unwrapped file name (without wrapper prefixes if wrapping /
delegating file systems are used).
|
static void |
writeFully(FileChannel channel,
ByteBuffer src)
Fully write to the file.
|
public static boolean exists(String fileName)
java.nio.file.Path.exists
.fileName
- the file namepublic static void createDirectory(String directoryName)
java.nio.file.Path.createDirectory
.directoryName
- the directory namepublic static boolean createFile(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(String path)
java.nio.file.Path.deleteIfExists
.path
- the file or directory namepublic static String toRealPath(String fileName)
java.nio.file.Path.toRealPath
.fileName
- the file namepublic static String getParent(String fileName)
java.nio.file.Path.getParent
.fileName
- the file or directory namepublic static boolean isAbsolute(String fileName)
java.nio.file.Path.isAbsolute
.fileName
- the file namepublic static void moveTo(String oldName, String newName)
java.nio.file.Path.moveTo
.oldName
- the old fully qualified file namenewName
- the new fully qualified file namepublic static String getName(String path)
java.nio.file.Path.getName
.path
- the directory and file namepublic static List<String> newDirectoryStream(String path)
java.nio.file.Path.newDirectoryStream
.path
- the directorypublic static long lastModified(String fileName)
java.nio.file.attribute.Attributes.
readBasicFileAttributes(file).lastModified().toMillis()
fileName
- the file namepublic static long size(String fileName)
java.nio.file.attribute.Attributes.
readBasicFileAttributes(file).size()
fileName
- the file namepublic static boolean isDirectory(String fileName)
java.nio.file.attribute.Attributes.
readBasicFileAttributes(file).isDirectory()
fileName
- the file or directory namepublic static FileChannel open(String fileName, String mode) throws IOException
java.nio.channels.FileChannel.open
.fileName
- the file namemode
- the access mode. Supported are r, rw, rws, rwdIOException
public static InputStream newInputStream(String fileName) throws IOException
java.nio.file.Path.newInputStream
.fileName
- the file nameIOException
public static OutputStream newOutputStream(String fileName, boolean append)
java.nio.file.Path.newOutputStream
.fileName
- the file nameappend
- if true, the file will grow, if false, the file will be
truncated firstpublic static boolean canWrite(String fileName)
java.nio.file.Path.checkAccess(AccessMode.WRITE)
fileName
- the file namepublic static boolean setReadOnly(String fileName)
fileName
- the file namepublic static String unwrap(String fileName)
fileName
- the file namepublic static void deleteRecursive(String path, boolean tryOnly)
path
- the pathtryOnly
- whether errors should be ignoredpublic static void createDirectories(String dir)
dir
- the directory namepublic static void copy(String original, String copy) throws IOException
original
- the original file namecopy
- the file name of the copyIOException
public static boolean tryDelete(String fileName)
fileName
- the file namepublic static String createTempFile(String prefix, String suffix, boolean deleteOnExit, boolean inTempDir) throws 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 directoryIOException
public static void readFully(FileChannel channel, ByteBuffer dst) throws IOException
channel
- the file channeldst
- the byte bufferIOException
public static void writeFully(FileChannel channel, ByteBuffer src) throws IOException
channel
- the file channelsrc
- the byte bufferIOException
Copyright © 2012 JBoss by Red Hat. All Rights Reserved.