FileUtils

This utility class contains utility functions that use the file system abstraction.

Methods
static boolean canWrite(String fileName)
Check if the file is writable.
static boolean canWrite(String fileName)
Check if the file is writable. This method is similar to Java 7 java.nio.file.Path.checkAccess(AccessMode.WRITE)
Parameters:
fileName - the file name
Returns:
if the file is writable
static void createDirectories(String dir)
Create the directory and all required parent directories.
static void createDirectories(String dir)
Create the directory and all required parent directories.
Parameters:
dir - the directory name
static void createDirectory(String directoryName)
Create a directory (all required parent directories must already exist).
static void createDirectory(String directoryName)
Create a directory (all required parent directories must already exist). This method is similar to Java 7 java.nio.file.Path.createDirectory.
Parameters:
directoryName - the directory name
static boolean createFile(String fileName)
Create a new file.
static boolean createFile(String fileName)
Create a new file. This method is similar to Java 7 java.nio.file.Path.createFile, but returns false instead of throwing a exception if the file already existed.
Parameters:
fileName - the file name
Returns:
true if creating was successful
static String createTempFile(String prefix, String suffix, boolean deleteOnExit, boolean inTempDir)
Create a new temporary file.
static String createTempFile(String prefix, String suffix, boolean deleteOnExit, boolean inTempDir) throws IOException
Create a new temporary file.
Parameters:
prefix - the prefix of the file name (including directory name if required)
suffix - the suffix
deleteOnExit - if the file should be deleted when the virtual machine exists
inTempDir - if the file should be stored in the temporary directory
Returns:
the name of the created file
static void delete(String path)
Delete a file or directory if it exists.
static void delete(String path)
Delete a file or directory if it exists. Directories may only be deleted if they are empty. This method is similar to Java 7 java.nio.file.Path.deleteIfExists.
Parameters:
path - the file or directory name
static void deleteRecursive(String path, boolean tryOnly)
Delete a directory or file and all subdirectories and files.
static void deleteRecursive(String path, boolean tryOnly)
Delete a directory or file and all subdirectories and files.
Parameters:
path - the path
tryOnly - whether errors should be ignored
static boolean exists(String fileName)
Checks if a file exists.
static boolean exists(String fileName)
Checks if a file exists. This method is similar to Java 7 java.nio.file.Path.exists.
Parameters:
fileName - the file name
Returns:
true if it exists
static String getName(String path)
Get the file or directory name (the last element of the path).
static String getName(String path)
Get the file or directory name (the last element of the path). This method is similar to Java 7 java.nio.file.Path.getName.
Parameters:
path - the directory and file name
Returns:
just the file name
static String getParent(String fileName)
Get the parent directory of a file or directory.
static String getParent(String fileName)
Get the parent directory of a file or directory. This method returns null if there is no parent. This method is similar to Java 7 java.nio.file.Path.getParent.
Parameters:
fileName - the file or directory name
Returns:
the parent directory name
static boolean isAbsolute(String fileName)
Check if the file name includes a path.
static boolean isAbsolute(String fileName)
Check if the file name includes a path. This method is similar to Java 7 java.nio.file.Path.isAbsolute.
Parameters:
fileName - the file name
Returns:
if the file name is absolute
static boolean isDirectory(String fileName)
Check if it is a file or a directory.
static boolean isDirectory(String fileName)
Check if it is a file or a directory. java.nio.file.attribute.Attributes. readBasicFileAttributes(file).isDirectory()
Parameters:
fileName - the file or directory name
Returns:
true if it is a directory
static long lastModified(String fileName)
Get the last modified date of a file.
static long lastModified(String fileName)
Get the last modified date of a file. This method is similar to Java 7 java.nio.file.attribute.Attributes. readBasicFileAttributes(file).lastModified().toMillis()
Parameters:
fileName - the file name
Returns:
the last modified date
static void move(String source, String target)
Rename a file if this is allowed.
static void move(String source, String target)
Rename a file if this is allowed. This method is similar to Java 7 java.nio.file.Files.move.
Parameters:
source - the old fully qualified file name
target - the new fully qualified file name
static void moveAtomicReplace(String source, String target)
Rename a file if this is allowed, and try to atomically replace an existing file.
static void moveAtomicReplace(String source, String target)
Rename a file if this is allowed, and try to atomically replace an existing file. This method is similar to Java 7 java.nio.file.Files.move.
Parameters:
source - the old fully qualified file name
target - the new fully qualified file name
static List newDirectoryStream(String path)
List the files and directories in the given directory.
static List newDirectoryStream(String path)
List the files and directories in the given directory. This method is similar to Java 7 java.nio.file.Path.newDirectoryStream.
Parameters:
path - the directory
Returns:
the list of fully qualified file names
static InputStream newInputStream(String fileName)
Create an input stream to read from the file.
static InputStream newInputStream(String fileName) throws IOException
Create an input stream to read from the file. This method is similar to Java 7 java.nio.file.Path.newInputStream.
Parameters:
fileName - the file name
Returns:
the input stream
static OutputStream newOutputStream(String fileName, boolean append)
Create an output stream to write into the file.
static OutputStream newOutputStream(String fileName, boolean append) throws IOException
Create an output stream to write into the file. This method is similar to Java 7 java.nio.file.Path.newOutputStream.
Parameters:
fileName - the file name
append - if true, the file will grow, if false, the file will be truncated first
Returns:
the output stream
static FileChannel open(String fileName, String mode)
Open a random access file object.
static FileChannel open(String fileName, String mode) throws IOException
Open a random access file object. This method is similar to Java 7 java.nio.channels.FileChannel.open.
Parameters:
fileName - the file name
mode - the access mode. Supported are r, rw, rws, rwd
Returns:
the file object
static void readFully(FileChannel channel, ByteBuffer dst)
Fully read from the file.
static void readFully(FileChannel channel, ByteBuffer dst) throws IOException
Fully read from the file. This will read all remaining bytes, or throw an EOFException if not successful.
Parameters:
channel - the file channel
dst - the byte buffer
static boolean setReadOnly(String fileName)
Disable the ability to write.
static boolean setReadOnly(String fileName)
Disable the ability to write. The file can still be deleted afterwards.
Parameters:
fileName - the file name
Returns:
true if the call was successful
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 long size(String fileName)
Get the size of a file in bytes This method is similar to Java 7 java.nio.file.attribute.Attributes. readBasicFileAttributes(file).size()
Parameters:
fileName - the file name
Returns:
the size in bytes
static String toRealPath(String fileName)
Get the canonical file or directory name.
static String toRealPath(String fileName)
Get the canonical file or directory name. This method is similar to Java 7 java.nio.file.Path.toRealPath.
Parameters:
fileName - the file name
Returns:
the normalized file name
static boolean tryDelete(String path)
Try to delete a file or directory (ignoring errors).
static boolean tryDelete(String path)
Try to delete a file or directory (ignoring errors).
Parameters:
path - the file or directory name
Returns:
true if it worked
static String unwrap(String fileName)
Get the unwrapped file name (without wrapper prefixes if wrapping / delegating file systems are used).
static String unwrap(String fileName)
Get the unwrapped file name (without wrapper prefixes if wrapping / delegating file systems are used).
Parameters:
fileName - the file name
Returns:
the unwrapped
static void writeFully(FileChannel channel, ByteBuffer src)
Fully write to the file.
static void writeFully(FileChannel channel, ByteBuffer src) throws IOException
Fully write to the file. This will write all remaining bytes.
Parameters:
channel - the file channel
src - the byte buffer