public class FileUtils
extends java.lang.Object
Facilities are provided in the following areas:
Note that a specific charset should be specified whenever possible. Relying on the platform default means that the code is Locale-dependent. Only use the default if the files are known to always use the platform default.
SecurityException are not documented in the Javadoc.
Origin of code: Excalibur, Alexandria, Commons-Utils
| Modifier and Type | Field and Description |
|---|---|
static java.io.File[] |
EMPTY_FILE_ARRAY
An empty array of type
File. |
static long |
ONE_EB
The number of bytes in an exabyte.
|
static java.math.BigInteger |
ONE_EB_BI
The number of bytes in an exabyte.
|
static long |
ONE_GB
The number of bytes in a gigabyte.
|
static java.math.BigInteger |
ONE_GB_BI
The number of bytes in a gigabyte.
|
static long |
ONE_KB
The number of bytes in a kilobyte.
|
static java.math.BigInteger |
ONE_KB_BI
The number of bytes in a kilobyte.
|
static long |
ONE_MB
The number of bytes in a megabyte.
|
static java.math.BigInteger |
ONE_MB_BI
The number of bytes in a megabyte.
|
static long |
ONE_PB
The number of bytes in a petabyte.
|
static java.math.BigInteger |
ONE_PB_BI
The number of bytes in a petabyte.
|
static long |
ONE_TB
The number of bytes in a terabyte.
|
static java.math.BigInteger |
ONE_TB_BI
The number of bytes in a terabyte.
|
static java.math.BigInteger |
ONE_YB
The number of bytes in a yottabyte.
|
static java.math.BigInteger |
ONE_ZB
The number of bytes in a zettabyte.
|
| Constructor and Description |
|---|
FileUtils()
Deprecated.
Will be private in 3.0.
|
| Modifier and Type | Method and Description |
|---|---|
static java.lang.String |
byteCountToDisplaySize(java.math.BigInteger size)
Returns a human-readable version of the file size, where the input represents a specific number of bytes.
|
static java.lang.String |
byteCountToDisplaySize(long size)
Returns a human-readable version of the file size, where the input represents a specific number of bytes.
|
static java.lang.String |
byteCountToDisplaySize(java.lang.Number size)
Returns a human-readable version of the file size, where the input represents a specific number of bytes.
|
static java.util.zip.Checksum |
checksum(java.io.File file,
java.util.zip.Checksum checksum)
Computes the checksum of a file using the specified checksum object.
|
static long |
checksumCRC32(java.io.File file)
Computes the checksum of a file using the CRC32 checksum routine.
|
static void |
cleanDirectory(java.io.File directory)
Cleans a directory without deleting it.
|
static boolean |
contentEquals(java.io.File file1,
java.io.File file2)
Tests whether the contents of two files are equal.
|
static boolean |
contentEqualsIgnoreEOL(java.io.File file1,
java.io.File file2,
java.lang.String charsetName)
Compares the contents of two files to determine if they are equal or not.
|
static java.io.File[] |
convertFileCollectionToFileArray(java.util.Collection<java.io.File> files)
Converts a Collection containing java.io.File instances into array
representation.
|
static void |
copyDirectory(java.io.File srcDir,
java.io.File destDir)
Copies a whole directory to a new location preserving the file dates.
|
static void |
copyDirectory(java.io.File srcDir,
java.io.File destDir,
boolean preserveFileDate)
Copies a whole directory to a new location.
|
static void |
copyDirectory(java.io.File srcDir,
java.io.File destDir,
java.io.FileFilter filter)
Copies a filtered directory to a new location preserving the file dates.
|
static void |
copyDirectory(java.io.File srcDir,
java.io.File destDir,
java.io.FileFilter filter,
boolean preserveFileDate)
Copies a filtered directory to a new location.
|
static void |
copyDirectory(java.io.File srcDir,
java.io.File destDir,
java.io.FileFilter fileFilter,
boolean preserveFileDate,
java.nio.file.CopyOption... copyOptions)
Copies a filtered directory to a new location.
|
static void |
copyDirectoryToDirectory(java.io.File sourceDir,
java.io.File destinationDir)
Copies a directory to within another directory preserving the file dates.
|
static void |
copyFile(java.io.File srcFile,
java.io.File destFile)
Copies a file to a new location preserving the file date.
|
static void |
copyFile(java.io.File srcFile,
java.io.File destFile,
boolean preserveFileDate)
Copies an existing file to a new file location.
|
static void |
copyFile(java.io.File srcFile,
java.io.File destFile,
boolean preserveFileDate,
java.nio.file.CopyOption... copyOptions)
Copies a file to a new location.
|
static void |
copyFile(java.io.File srcFile,
java.io.File destFile,
java.nio.file.CopyOption... copyOptions)
Copies a file to a new location.
|
static long |
copyFile(java.io.File input,
java.io.OutputStream output)
Copies bytes from a
File to an OutputStream. |
static void |
copyFileToDirectory(java.io.File srcFile,
java.io.File destDir)
Copies a file to a directory preserving the file date.
|
static void |
copyFileToDirectory(java.io.File sourceFile,
java.io.File destinationDir,
boolean preserveFileDate)
Copies a file to a directory optionally preserving the file date.
|
static void |
copyInputStreamToFile(java.io.InputStream source,
java.io.File destination)
Copies bytes from an
InputStream source to a file
destination. |
static void |
copyToDirectory(java.io.File sourceFile,
java.io.File destinationDir)
Copies a file or directory to within another directory preserving the file dates.
|
static void |
copyToDirectory(java.lang.Iterable<java.io.File> sourceIterable,
java.io.File destinationDir)
Copies a files to a directory preserving each file's date.
|
static void |
copyToFile(java.io.InputStream inputStream,
java.io.File file)
Copies bytes from an
InputStream source to a File destination. |
static void |
copyURLToFile(java.net.URL source,
java.io.File destination)
Copies bytes from the URL
source to a file
destination. |
static void |
copyURLToFile(java.net.URL source,
java.io.File destination,
int connectionTimeoutMillis,
int readTimeoutMillis)
Copies bytes from the URL
source to a file destination. |
static java.io.File |
createParentDirectories(java.io.File file)
Creates all parent directories for a File object, including any necessary but nonexistent parent directories.
|
static java.io.File |
current()
Gets the current directory.
|
static java.io.File |
delete(java.io.File file)
Deletes the given File but throws an IOException if it cannot, unlike
File.delete() which returns a
boolean. |
static void |
deleteDirectory(java.io.File directory)
Deletes a directory recursively.
|
static boolean |
deleteQuietly(java.io.File file)
Deletes a file, never throwing an exception.
|
static boolean |
directoryContains(java.io.File directory,
java.io.File child)
Determines whether the
parent directory contains the child element (a file or directory). |
static void |
forceDelete(java.io.File file)
Deletes a file or directory.
|
static void |
forceDeleteOnExit(java.io.File file)
Schedules a file to be deleted when JVM exits.
|
static void |
forceMkdir(java.io.File directory)
Creates all directories for a File object, including any necessary but nonexistent parent directories.
|
static void |
forceMkdirParent(java.io.File file)
Calls
File.mkdirs() and throws an IOException on failure. |
static java.io.File |
getFile(java.io.File directory,
java.lang.String... names)
Constructs a file from the set of name elements.
|
static java.io.File |
getFile(java.lang.String... names)
Constructs a file from the set of name elements.
|
static java.io.File |
getTempDirectory()
Returns a
File representing the system temporary directory. |
static java.lang.String |
getTempDirectoryPath()
Returns the path to the system temporary directory.
|
static java.io.File |
getUserDirectory()
Returns a
File representing the user's home directory. |
static java.lang.String |
getUserDirectoryPath()
Returns the path to the user's home directory.
|
static boolean |
isDirectory(java.io.File file,
java.nio.file.LinkOption... options)
Tests whether the specified
File is a directory or not. |
static boolean |
isEmptyDirectory(java.io.File directory)
Tests whether the directory is empty.
|
static boolean |
isFileNewer(java.io.File file,
java.time.chrono.ChronoLocalDate chronoLocalDate)
Tests if the specified
File is newer than the specified ChronoLocalDate
at the end of day. |
static boolean |
isFileNewer(java.io.File file,
java.time.chrono.ChronoLocalDate chronoLocalDate,
java.time.LocalTime localTime)
Tests if the specified
File is newer than the specified ChronoLocalDate
at the specified time. |
static boolean |
isFileNewer(java.io.File file,
java.time.chrono.ChronoLocalDate chronoLocalDate,
java.time.OffsetTime offsetTime)
Tests if the specified
File is newer than the specified ChronoLocalDate at the specified
OffsetTime. |
static boolean |
isFileNewer(java.io.File file,
java.time.chrono.ChronoLocalDateTime<?> chronoLocalDateTime)
Tests if the specified
File is newer than the specified ChronoLocalDateTime
at the system-default time zone. |
static boolean |
isFileNewer(java.io.File file,
java.time.chrono.ChronoLocalDateTime<?> chronoLocalDateTime,
java.time.ZoneId zoneId)
Tests if the specified
File is newer than the specified ChronoLocalDateTime
at the specified ZoneId. |
static boolean |
isFileNewer(java.io.File file,
java.time.chrono.ChronoZonedDateTime<?> chronoZonedDateTime)
Tests if the specified
File is newer than the specified ChronoZonedDateTime. |
static boolean |
isFileNewer(java.io.File file,
java.util.Date date)
Tests if the specified
File is newer than the specified Date. |
static boolean |
isFileNewer(java.io.File file,
java.io.File reference)
Tests if the specified
File is newer than the reference File. |
static boolean |
isFileNewer(java.io.File file,
java.nio.file.attribute.FileTime fileTime)
Tests if the specified
File is newer than the specified FileTime. |
static boolean |
isFileNewer(java.io.File file,
java.time.Instant instant)
Tests if the specified
File is newer than the specified Instant. |
static boolean |
isFileNewer(java.io.File file,
long timeMillis)
Tests if the specified
File is newer than the specified time reference. |
static boolean |
isFileNewer(java.io.File file,
java.time.OffsetDateTime offsetDateTime)
Tests if the specified
File is newer than the specified OffsetDateTime. |
static boolean |
isFileOlder(java.io.File file,
java.time.chrono.ChronoLocalDate chronoLocalDate)
Tests if the specified
File is older than the specified ChronoLocalDate
at the end of day. |
static boolean |
isFileOlder(java.io.File file,
java.time.chrono.ChronoLocalDate chronoLocalDate,
java.time.LocalTime localTime)
Tests if the specified
File is older than the specified ChronoLocalDate
at the specified LocalTime. |
static boolean |
isFileOlder(java.io.File file,
java.time.chrono.ChronoLocalDate chronoLocalDate,
java.time.OffsetTime offsetTime)
Tests if the specified
File is older than the specified ChronoLocalDate at the specified
OffsetTime. |
static boolean |
isFileOlder(java.io.File file,
java.time.chrono.ChronoLocalDateTime<?> chronoLocalDateTime)
Tests if the specified
File is older than the specified ChronoLocalDateTime
at the system-default time zone. |
static boolean |
isFileOlder(java.io.File file,
java.time.chrono.ChronoLocalDateTime<?> chronoLocalDateTime,
java.time.ZoneId zoneId)
Tests if the specified
File is older than the specified ChronoLocalDateTime
at the specified ZoneId. |
static boolean |
isFileOlder(java.io.File file,
java.time.chrono.ChronoZonedDateTime<?> chronoZonedDateTime)
Tests if the specified
File is older than the specified ChronoZonedDateTime. |
static boolean |
isFileOlder(java.io.File file,
java.util.Date date)
Tests if the specified
File is older than the specified Date. |
static boolean |
isFileOlder(java.io.File file,
java.io.File reference)
Tests if the specified
File is older than the reference File. |
static boolean |
isFileOlder(java.io.File file,
java.nio.file.attribute.FileTime fileTime)
Tests if the specified
File is older than the specified FileTime. |
static boolean |
isFileOlder(java.io.File file,
java.time.Instant instant)
Tests if the specified
File is older than the specified Instant. |
static boolean |
isFileOlder(java.io.File file,
long timeMillis)
Tests if the specified
File is older than the specified time reference. |
static boolean |
isFileOlder(java.io.File file,
java.time.OffsetDateTime offsetDateTime)
Tests if the specified
File is older than the specified OffsetDateTime. |
static boolean |
isRegularFile(java.io.File file,
java.nio.file.LinkOption... options)
Tests whether the specified
File is a regular file or not. |
static boolean |
isSymlink(java.io.File file)
Tests whether the specified file is a symbolic link rather than an actual file.
|
static java.util.Iterator<java.io.File> |
iterateFiles(java.io.File directory,
IOFileFilter fileFilter,
IOFileFilter dirFilter)
Iterates over the files in given directory (and optionally
its subdirectories).
|
static java.util.Iterator<java.io.File> |
iterateFiles(java.io.File directory,
java.lang.String[] extensions,
boolean recursive)
Iterates over the files in a given directory (and optionally
its subdirectories) which match an array of extensions.
|
static java.util.Iterator<java.io.File> |
iterateFilesAndDirs(java.io.File directory,
IOFileFilter fileFilter,
IOFileFilter dirFilter)
Iterates over the files in given directory (and optionally
its subdirectories).
|
static long |
lastModified(java.io.File file)
Returns the last modification time in milliseconds via
Files.getLastModifiedTime(Path, LinkOption...). |
static java.nio.file.attribute.FileTime |
lastModifiedFileTime(java.io.File file)
Returns the last modification
FileTime via
Files.getLastModifiedTime(Path, LinkOption...). |
static long |
lastModifiedUnchecked(java.io.File file)
Returns the last modification time in milliseconds via
Files.getLastModifiedTime(Path, LinkOption...). |
static LineIterator |
lineIterator(java.io.File file)
Returns an Iterator for the lines in a
File using the default encoding for the VM. |
static LineIterator |
lineIterator(java.io.File file,
java.lang.String charsetName)
Returns an Iterator for the lines in a
File. |
static java.util.Collection<java.io.File> |
listFiles(java.io.File directory,
IOFileFilter fileFilter,
IOFileFilter dirFilter)
Finds files within a given directory (and optionally its
subdirectories).
|
static java.util.Collection<java.io.File> |
listFiles(java.io.File directory,
java.lang.String[] extensions,
boolean recursive)
Finds files within a given directory (and optionally its subdirectories)
which match an array of extensions.
|
static java.util.Collection<java.io.File> |
listFilesAndDirs(java.io.File directory,
IOFileFilter fileFilter,
IOFileFilter dirFilter)
Finds files within a given directory (and optionally its
subdirectories).
|
static void |
moveDirectory(java.io.File srcDir,
java.io.File destDir)
Moves a directory.
|
static void |
moveDirectoryToDirectory(java.io.File source,
java.io.File destDir,
boolean createDestDir)
Moves a directory to another directory.
|
static void |
moveFile(java.io.File srcFile,
java.io.File destFile)
Moves a file preserving attributes.
|
static void |
moveFile(java.io.File srcFile,
java.io.File destFile,
java.nio.file.CopyOption... copyOptions)
Moves a file.
|
static void |
moveFileToDirectory(java.io.File srcFile,
java.io.File destDir,
boolean createDestDir)
Moves a file to a directory.
|
static void |
moveToDirectory(java.io.File src,
java.io.File destDir,
boolean createDestDir)
Moves a file or directory to a destination directory.
|
static java.io.OutputStream |
newOutputStream(java.io.File file,
boolean append)
Creates a new OutputStream by opening or creating a file, returning an output stream that may be used to write bytes
to the file.
|
static java.io.FileInputStream |
openInputStream(java.io.File file)
Opens a
FileInputStream for the specified file, providing better error messages than simply calling
new FileInputStream(file). |
static java.io.FileOutputStream |
openOutputStream(java.io.File file)
Opens a
FileOutputStream for the specified file, checking and
creating the parent directory if it does not exist. |
static java.io.FileOutputStream |
openOutputStream(java.io.File file,
boolean append)
Opens a
FileOutputStream for the specified file, checking and
creating the parent directory if it does not exist. |
static byte[] |
readFileToByteArray(java.io.File file)
Reads the contents of a file into a byte array.
|
static java.lang.String |
readFileToString(java.io.File file)
Deprecated.
2.5 use
readFileToString(File, Charset) instead (and specify the appropriate encoding) |
static java.lang.String |
readFileToString(java.io.File file,
java.nio.charset.Charset charsetName)
Reads the contents of a file into a String.
|
static java.lang.String |
readFileToString(java.io.File file,
java.lang.String charsetName)
Reads the contents of a file into a String.
|
static java.util.List<java.lang.String> |
readLines(java.io.File file)
Deprecated.
2.5 use
readLines(File, Charset) instead (and specify the appropriate encoding) |
static java.util.List<java.lang.String> |
readLines(java.io.File file,
java.nio.charset.Charset charset)
Reads the contents of a file line by line to a List of Strings.
|
static java.util.List<java.lang.String> |
readLines(java.io.File file,
java.lang.String charsetName)
Reads the contents of a file line by line to a List of Strings.
|
static long |
sizeOf(java.io.File file)
Returns the size of the specified file or directory.
|
static java.math.BigInteger |
sizeOfAsBigInteger(java.io.File file)
Returns the size of the specified file or directory.
|
static long |
sizeOfDirectory(java.io.File directory)
Counts the size of a directory recursively (sum of the length of all files).
|
static java.math.BigInteger |
sizeOfDirectoryAsBigInteger(java.io.File directory)
Counts the size of a directory recursively (sum of the length of all files).
|
static java.util.stream.Stream<java.io.File> |
streamFiles(java.io.File directory,
boolean recursive,
java.lang.String... extensions)
Streams over the files in a given directory (and optionally
its subdirectories) which match an array of extensions.
|
static java.io.File |
toFile(java.net.URL url)
Converts from a
URL to a File. |
static java.io.File[] |
toFiles(java.net.URL... urls)
Converts each of an array of
URL to a File. |
static void |
touch(java.io.File file)
Implements behavior similar to the Unix "touch" utility.
|
static java.net.URL[] |
toURLs(java.io.File... files)
Converts each of an array of
File to a URL. |
static boolean |
waitFor(java.io.File file,
int seconds)
Waits for the file system to propagate a file creation, with a timeout.
|
static void |
write(java.io.File file,
java.lang.CharSequence data)
Deprecated.
2.5 use
write(File, CharSequence, Charset) instead (and specify the appropriate encoding) |
static void |
write(java.io.File file,
java.lang.CharSequence data,
boolean append)
Deprecated.
2.5 use
write(File, CharSequence, Charset, boolean) instead (and specify the appropriate encoding) |
static void |
write(java.io.File file,
java.lang.CharSequence data,
java.nio.charset.Charset charset)
Writes a CharSequence to a file creating the file if it does not exist.
|
static void |
write(java.io.File file,
java.lang.CharSequence data,
java.nio.charset.Charset charset,
boolean append)
Writes a CharSequence to a file creating the file if it does not exist.
|
static void |
write(java.io.File file,
java.lang.CharSequence data,
java.lang.String charsetName)
Writes a CharSequence to a file creating the file if it does not exist.
|
static void |
write(java.io.File file,
java.lang.CharSequence data,
java.lang.String charsetName,
boolean append)
Writes a CharSequence to a file creating the file if it does not exist.
|
static void |
writeByteArrayToFile(java.io.File file,
byte[] data)
Writes a byte array to a file creating the file if it does not exist.
|
static void |
writeByteArrayToFile(java.io.File file,
byte[] data,
boolean append)
Writes a byte array to a file creating the file if it does not exist.
|
static void |
writeByteArrayToFile(java.io.File file,
byte[] data,
int off,
int len)
Writes
len bytes from the specified byte array starting
at offset off to a file, creating the file if it does
not exist. |
static void |
writeByteArrayToFile(java.io.File file,
byte[] data,
int off,
int len,
boolean append)
Writes
len bytes from the specified byte array starting
at offset off to a file, creating the file if it does
not exist. |
static void |
writeLines(java.io.File file,
java.util.Collection<?> lines)
Writes the
toString() value of each item in a collection to
the specified File line by line. |
static void |
writeLines(java.io.File file,
java.util.Collection<?> lines,
boolean append)
Writes the
toString() value of each item in a collection to
the specified File line by line. |
static void |
writeLines(java.io.File file,
java.util.Collection<?> lines,
java.lang.String lineEnding)
Writes the
toString() value of each item in a collection to
the specified File line by line. |
static void |
writeLines(java.io.File file,
java.util.Collection<?> lines,
java.lang.String lineEnding,
boolean append)
Writes the
toString() value of each item in a collection to
the specified File line by line. |
static void |
writeLines(java.io.File file,
java.lang.String charsetName,
java.util.Collection<?> lines)
Writes the
toString() value of each item in a collection to
the specified File line by line. |
static void |
writeLines(java.io.File file,
java.lang.String charsetName,
java.util.Collection<?> lines,
boolean append)
Writes the
toString() value of each item in a collection to
the specified File line by line, optionally appending. |
static void |
writeLines(java.io.File file,
java.lang.String charsetName,
java.util.Collection<?> lines,
java.lang.String lineEnding)
Writes the
toString() value of each item in a collection to
the specified File line by line. |
static void |
writeLines(java.io.File file,
java.lang.String charsetName,
java.util.Collection<?> lines,
java.lang.String lineEnding,
boolean append)
Writes the
toString() value of each item in a collection to
the specified File line by line. |
static void |
writeStringToFile(java.io.File file,
java.lang.String data)
Deprecated.
2.5 use
writeStringToFile(File, String, Charset) instead (and specify the appropriate encoding) |
static void |
writeStringToFile(java.io.File file,
java.lang.String data,
boolean append)
Deprecated.
2.5 use
writeStringToFile(File, String, Charset, boolean) instead (and specify the appropriate encoding) |
static void |
writeStringToFile(java.io.File file,
java.lang.String data,
java.nio.charset.Charset charset)
Writes a String to a file creating the file if it does not exist.
|
static void |
writeStringToFile(java.io.File file,
java.lang.String data,
java.nio.charset.Charset charset,
boolean append)
Writes a String to a file creating the file if it does not exist.
|
static void |
writeStringToFile(java.io.File file,
java.lang.String data,
java.lang.String charsetName)
Writes a String to a file creating the file if it does not exist.
|
static void |
writeStringToFile(java.io.File file,
java.lang.String data,
java.lang.String charsetName,
boolean append)
Writes a String to a file creating the file if it does not exist.
|
public static final long ONE_KB
public static final java.math.BigInteger ONE_KB_BI
public static final long ONE_MB
public static final java.math.BigInteger ONE_MB_BI
public static final long ONE_GB
public static final java.math.BigInteger ONE_GB_BI
public static final long ONE_TB
public static final java.math.BigInteger ONE_TB_BI
public static final long ONE_PB
public static final java.math.BigInteger ONE_PB_BI
public static final long ONE_EB
public static final java.math.BigInteger ONE_EB_BI
public static final java.math.BigInteger ONE_ZB
public static final java.math.BigInteger ONE_YB
public static final java.io.File[] EMPTY_FILE_ARRAY
File.@Deprecated public FileUtils()
public static java.lang.String byteCountToDisplaySize(java.math.BigInteger size)
If the size is over 1GB, the size is returned as the number of whole GB, i.e. the size is rounded down to the nearest GB boundary.
Similarly for the 1MB and 1KB boundaries.
size - the number of bytesjava.lang.NullPointerException - if the given BigInteger is null.public static java.lang.String byteCountToDisplaySize(long size)
If the size is over 1GB, the size is returned as the number of whole GB, i.e. the size is rounded down to the nearest GB boundary.
Similarly for the 1MB and 1KB boundaries.
size - the number of bytespublic static java.lang.String byteCountToDisplaySize(java.lang.Number size)
If the size is over 1GB, the size is returned as the number of whole GB, i.e. the size is rounded down to the nearest GB boundary.
Similarly for the 1MB and 1KB boundaries.
size - the number of bytespublic static java.util.zip.Checksum checksum(java.io.File file,
java.util.zip.Checksum checksum)
throws java.io.IOException
Checksum instance if desired simply by reusing the same checksum object. For example:
long checksum = FileUtils.checksum(file, new CRC32()).getValue();
file - the file to checksum, must not be nullchecksum - the checksum object to be used, must not be nulljava.lang.NullPointerException - if the given File is null.java.lang.NullPointerException - if the given Checksum is null.java.lang.IllegalArgumentException - if the given File does not exist or is not a file.java.io.IOException - if an IO error occurs reading the file.public static long checksumCRC32(java.io.File file)
throws java.io.IOException
file - the file to checksum, must not be nulljava.lang.NullPointerException - if the given File is null.java.lang.IllegalArgumentException - if the given File does not exist or is not a file.java.io.IOException - if an IO error occurs reading the file.public static void cleanDirectory(java.io.File directory)
throws java.io.IOException
directory - directory to cleanjava.lang.NullPointerException - if the given File is null.java.lang.IllegalArgumentException - if directory does not exist or is not a directory.java.io.IOException - if an I/O error occurs.forceDelete(File)public static boolean contentEquals(java.io.File file1,
java.io.File file2)
throws java.io.IOException
This method checks to see if the two files are different lengths or if they point to the same file, before resorting to byte-by-byte comparison of the contents.
Code origin: Avalon
file1 - the first filefile2 - the second filejava.lang.IllegalArgumentException - when an input is not a file.java.io.IOException - If an I/O error occurs.PathUtils.fileContentEquals(Path,Path,java.nio.file.LinkOption[],java.nio.file.OpenOption...)public static boolean contentEqualsIgnoreEOL(java.io.File file1,
java.io.File file2,
java.lang.String charsetName)
throws java.io.IOException
This method checks to see if the two files point to the same file, before resorting to line-by-line comparison of the contents.
file1 - the first filefile2 - the second filecharsetName - the name of the requested charset.
May be null, in which case the platform default is usedjava.lang.IllegalArgumentException - when an input is not a file.java.io.IOException - in case of an I/O error.java.nio.charset.UnsupportedCharsetException - If the named charset is unavailable (unchecked exception).IOUtils.contentEqualsIgnoreEOL(Reader, Reader)public static java.io.File[] convertFileCollectionToFileArray(java.util.Collection<java.io.File> files)
files - a Collection containing java.io.File instancespublic static void copyDirectory(java.io.File srcDir,
java.io.File destDir)
throws java.io.IOException
This method copies the specified directory and all its child directories and files to the specified destination. The destination is the new location and name of the directory.
The destination directory is created if it does not exist. If the destination directory did exist, then this method merges the source with the destination, with the source taking precedence.
Note: Setting preserveFileDate to true tries to preserve the file's last
modified date/times using BasicFileAttributeView.setTimes(FileTime, FileTime, FileTime), however it is
not guaranteed that the operation will succeed. If the modification operation fails it will fallback to
File.setLastModified(long) and if that fails, the methods throws IOException.
srcDir - an existing directory to copy, must not be null.destDir - the new directory, must not be null.java.lang.NullPointerException - if any of the given Files are null.java.lang.IllegalArgumentException - if the source or destination is invalid.java.io.FileNotFoundException - if the source does not exist.java.io.IOException - if an error occurs or setting the last-modified time didn't succeed.public static void copyDirectory(java.io.File srcDir,
java.io.File destDir,
boolean preserveFileDate)
throws java.io.IOException
This method copies the contents of the specified source directory to within the specified destination directory.
The destination directory is created if it does not exist. If the destination directory did exist, then this method merges the source with the destination, with the source taking precedence.
Note: Setting preserveFileDate to true tries to preserve the files' last
modified date/times using File.setLastModified(long), however it is not guaranteed that those operations
will succeed. If the modification operation fails, the methods throws IOException.
srcDir - an existing directory to copy, must not be null.destDir - the new directory, must not be null.preserveFileDate - true if the file date of the copy should be the same as the original.java.lang.NullPointerException - if any of the given Files are null.java.lang.IllegalArgumentException - if the source or destination is invalid.java.io.FileNotFoundException - if the source does not exist.java.io.IOException - if an error occurs or setting the last-modified time didn't succeed.public static void copyDirectory(java.io.File srcDir,
java.io.File destDir,
java.io.FileFilter filter)
throws java.io.IOException
This method copies the contents of the specified source directory to within the specified destination directory.
The destination directory is created if it does not exist. If the destination directory did exist, then this method merges the source with the destination, with the source taking precedence.
Note: This method tries to preserve the files' last modified date/times using
File.setLastModified(long), however it is not guaranteed that those operations will succeed. If the
modification operation fails, the methods throws IOException.
// only copy the directory structure FileUtils.copyDirectory(srcDir, destDir, DirectoryFileFilter.DIRECTORY);Example: Copy directories and txt files
// Create a filter for ".txt" files
IOFileFilter txtSuffixFilter = FileFilterUtils.suffixFileFilter(".txt");
IOFileFilter txtFiles = FileFilterUtils.andFileFilter(FileFileFilter.FILE, txtSuffixFilter);
// Create a filter for either directories or ".txt" files
FileFilter filter = FileFilterUtils.orFileFilter(DirectoryFileFilter.DIRECTORY, txtFiles);
// Copy using the filter
FileUtils.copyDirectory(srcDir, destDir, filter);
srcDir - an existing directory to copy, must not be null.destDir - the new directory, must not be null.filter - the filter to apply, null means copy all directories and files should be the same as the original.java.lang.NullPointerException - if any of the given Files are null.java.lang.IllegalArgumentException - if the source or destination is invalid.java.io.FileNotFoundException - if the source does not exist.java.io.IOException - if an error occurs or setting the last-modified time didn't succeed.public static void copyDirectory(java.io.File srcDir,
java.io.File destDir,
java.io.FileFilter filter,
boolean preserveFileDate)
throws java.io.IOException
This method copies the contents of the specified source directory to within the specified destination directory.
The destination directory is created if it does not exist. If the destination directory did exist, then this method merges the source with the destination, with the source taking precedence.
Note: Setting preserveFileDate to true tries to preserve the file's last
modified date/times using BasicFileAttributeView.setTimes(FileTime, FileTime, FileTime), however it is
not guaranteed that the operation will succeed. If the modification operation fails it will fallback to
File.setLastModified(long) and if that fails, the methods throws IOException.
// only copy the directory structure FileUtils.copyDirectory(srcDir, destDir, DirectoryFileFilter.DIRECTORY, false);Example: Copy directories and txt files
// Create a filter for ".txt" files
IOFileFilter txtSuffixFilter = FileFilterUtils.suffixFileFilter(".txt");
IOFileFilter txtFiles = FileFilterUtils.andFileFilter(FileFileFilter.FILE, txtSuffixFilter);
// Create a filter for either directories or ".txt" files
FileFilter filter = FileFilterUtils.orFileFilter(DirectoryFileFilter.DIRECTORY, txtFiles);
// Copy using the filter
FileUtils.copyDirectory(srcDir, destDir, filter, false);
srcDir - an existing directory to copy, must not be null.destDir - the new directory, must not be null.filter - the filter to apply, null means copy all directories and files.preserveFileDate - true if the file date of the copy should be the same as the original.java.lang.NullPointerException - if any of the given Files are null.java.lang.IllegalArgumentException - if the source or destination is invalid.java.io.FileNotFoundException - if the source does not exist.java.io.IOException - if an error occurs or setting the last-modified time didn't succeed.public static void copyDirectory(java.io.File srcDir,
java.io.File destDir,
java.io.FileFilter fileFilter,
boolean preserveFileDate,
java.nio.file.CopyOption... copyOptions)
throws java.io.IOException
This method copies the contents of the specified source directory to within the specified destination directory.
The destination directory is created if it does not exist. If the destination directory did exist, then this method merges the source with the destination, with the source taking precedence.
Note: Setting preserveFileDate to true tries to preserve the file's last
modified date/times using BasicFileAttributeView.setTimes(FileTime, FileTime, FileTime), however it is
not guaranteed that the operation will succeed. If the modification operation fails it will fallback to
File.setLastModified(long) and if that fails, the methods throws IOException.
// only copy the directory structure FileUtils.copyDirectory(srcDir, destDir, DirectoryFileFilter.DIRECTORY, false);Example: Copy directories and txt files
// Create a filter for ".txt" files
IOFileFilter txtSuffixFilter = FileFilterUtils.suffixFileFilter(".txt");
IOFileFilter txtFiles = FileFilterUtils.andFileFilter(FileFileFilter.FILE, txtSuffixFilter);
// Create a filter for either directories or ".txt" files
FileFilter filter = FileFilterUtils.orFileFilter(DirectoryFileFilter.DIRECTORY, txtFiles);
// Copy using the filter
FileUtils.copyDirectory(srcDir, destDir, filter, false);
srcDir - an existing directory to copy, must not be nulldestDir - the new directory, must not be nullfileFilter - the filter to apply, null means copy all directories and filespreserveFileDate - true if the file date of the copy should be the same as the originalcopyOptions - options specifying how the copy should be done, for example StandardCopyOption.java.lang.NullPointerException - if any of the given Files are null.java.lang.IllegalArgumentException - if the source or destination is invalid.java.io.FileNotFoundException - if the source does not exist.java.io.IOException - if an error occurs or setting the last-modified time didn't succeed.public static void copyDirectoryToDirectory(java.io.File sourceDir,
java.io.File destinationDir)
throws java.io.IOException
This method copies the source directory and all its contents to a directory of the same name in the specified destination directory.
The destination directory is created if it does not exist. If the destination directory did exist, then this method merges the source with the destination, with the source taking precedence.
Note: Setting preserveFileDate to true tries to preserve the file's last
modified date/times using BasicFileAttributeView.setTimes(FileTime, FileTime, FileTime), however it is
not guaranteed that the operation will succeed. If the modification operation fails it will fallback to
File.setLastModified(long) and if that fails, the methods throws IOException.
sourceDir - an existing directory to copy, must not be null.destinationDir - the directory to place the copy in, must not be null.java.lang.NullPointerException - if any of the given Files are null.java.lang.IllegalArgumentException - if the source or destination is invalid.java.io.FileNotFoundException - if the source does not exist.java.io.IOException - if an error occurs or setting the last-modified time didn't succeed.public static void copyFile(java.io.File srcFile,
java.io.File destFile)
throws java.io.IOException
This method copies the contents of the specified source file to the specified destination file. The directory holding the destination file is created if it does not exist. If the destination file exists, then this method will overwrite it.
Note: This method tries to preserve the file's last modified date/times using
BasicFileAttributeView.setTimes(FileTime, FileTime, FileTime), however it is not guaranteed that the
operation will succeed. If the modification operation fails it will fallback to
File.setLastModified(long) and if that fails, the methods throws IOException.
srcFile - an existing file to copy, must not be null.destFile - the new file, must not be null.java.lang.NullPointerException - if any of the given Files are null.java.io.IOException - if source or destination is invalid.java.io.IOException - if an error occurs or setting the last-modified time didn't succeed.java.io.IOException - if the output file length is not the same as the input file length after the copy completes.copyFileToDirectory(File, File),
copyFile(File, File, boolean)public static void copyFile(java.io.File srcFile,
java.io.File destFile,
boolean preserveFileDate)
throws java.io.IOException
This method copies the contents of the specified source file to the specified destination file. The directory holding the destination file is created if it does not exist. If the destination file exists, then this method will overwrite it.
Note: Setting preserveFileDate to true tries to preserve the file's last
modified date/times using BasicFileAttributeView.setTimes(FileTime, FileTime, FileTime), however it is
not guaranteed that the operation will succeed. If the modification operation fails it will fallback to
File.setLastModified(long) and if that fails, the methods throws IOException.
srcFile - an existing file to copy, must not be null.destFile - the new file, must not be null.preserveFileDate - true if the file date of the copy should be the same as the original.java.lang.NullPointerException - if any of the given Files are null.java.io.IOException - if source or destination is invalid.java.io.IOException - if an error occurs or setting the last-modified time didn't succeed.java.io.IOException - if the output file length is not the same as the input file length after the copy completescopyFile(File, File, boolean, CopyOption...)public static void copyFile(java.io.File srcFile,
java.io.File destFile,
boolean preserveFileDate,
java.nio.file.CopyOption... copyOptions)
throws java.io.IOException
This method copies the contents of the specified source file to the specified destination file. The directory
holding the destination file is created if it does not exist. If the destination file exists, you can overwrite
it with StandardCopyOption.REPLACE_EXISTING.
Note: Setting preserveFileDate to true tries to preserve the file's last
modified date/times using BasicFileAttributeView.setTimes(FileTime, FileTime, FileTime), however it is
not guaranteed that the operation will succeed. If the modification operation fails it will fallback to
File.setLastModified(long) and if that fails, the methods throws IOException.
srcFile - an existing file to copy, must not be null.destFile - the new file, must not be null.preserveFileDate - true if the file date of the copy should be the same as the original.copyOptions - options specifying how the copy should be done, for example StandardCopyOption..java.lang.NullPointerException - if any of the given Files are null.java.io.FileNotFoundException - if the source does not exist.java.lang.IllegalArgumentException - if source is not a file.java.io.IOException - if the output file length is not the same as the input file length after the copy completes.java.io.IOException - if an I/O error occurs, or setting the last-modified time didn't succeed.copyFileToDirectory(File, File, boolean)public static void copyFile(java.io.File srcFile,
java.io.File destFile,
java.nio.file.CopyOption... copyOptions)
throws java.io.IOException
This method copies the contents of the specified source file to the specified destination file. The directory
holding the destination file is created if it does not exist. If the destination file exists, you can overwrite
it if you use StandardCopyOption.REPLACE_EXISTING.
srcFile - an existing file to copy, must not be null.destFile - the new file, must not be null.copyOptions - options specifying how the copy should be done, for example StandardCopyOption..java.lang.NullPointerException - if any of the given Files are null.java.io.FileNotFoundException - if the source does not exist.java.lang.IllegalArgumentException - if source is not a file.java.io.IOException - if an I/O error occurs.StandardCopyOptionpublic static long copyFile(java.io.File input,
java.io.OutputStream output)
throws java.io.IOException
File to an OutputStream.
This method buffers the input internally, so there is no need to use a BufferedInputStream.
input - the File to read.output - the OutputStream to write.java.lang.NullPointerException - if the File is null.java.lang.NullPointerException - if the OutputStream is null.java.io.IOException - if an I/O error occurs.public static void copyFileToDirectory(java.io.File srcFile,
java.io.File destDir)
throws java.io.IOException
This method copies the contents of the specified source file to a file of the same name in the specified destination directory. The destination directory is created if it does not exist. If the destination file exists, then this method will overwrite it.
Note: This method tries to preserve the file's last modified date/times using
BasicFileAttributeView.setTimes(FileTime, FileTime, FileTime), however it is not guaranteed that the
operation will succeed. If the modification operation fails it will fallback to
File.setLastModified(long) and if that fails, the methods throws IOException.
srcFile - an existing file to copy, must not be null.destDir - the directory to place the copy in, must not be null.java.lang.NullPointerException - if any of the given Files are null.java.lang.IllegalArgumentException - if source or destination is invalid.java.io.IOException - if an error occurs or setting the last-modified time didn't succeed.copyFile(File, File, boolean)public static void copyFileToDirectory(java.io.File sourceFile,
java.io.File destinationDir,
boolean preserveFileDate)
throws java.io.IOException
This method copies the contents of the specified source file to a file of the same name in the specified destination directory. The destination directory is created if it does not exist. If the destination file exists, then this method will overwrite it.
Note: Setting preserveFileDate to true tries to preserve the file's last
modified date/times using BasicFileAttributeView.setTimes(FileTime, FileTime, FileTime), however it is
not guaranteed that the operation will succeed. If the modification operation fails it will fallback to
File.setLastModified(long) and if that fails, the methods throws IOException.
sourceFile - an existing file to copy, must not be null.destinationDir - the directory to place the copy in, must not be null.preserveFileDate - true if the file date of the copy should be the same as the original.java.lang.NullPointerException - if any of the given Files are null.java.io.IOException - if an error occurs or setting the last-modified time didn't succeed.java.io.IOException - if the output file length is not the same as the input file length after the copy completes.copyFile(File, File, CopyOption...)public static void copyInputStreamToFile(java.io.InputStream source,
java.io.File destination)
throws java.io.IOException
InputStream source to a file
destination. The directories up to destination
will be created if they don't already exist. destination
will be overwritten if it already exists.
The source stream is closed.
See copyToFile(InputStream, File) for a method that does not close the input stream.
source - the InputStream to copy bytes from, must not be null, will be closeddestination - the non-directory File to write bytes to
(possibly overwriting), must not be nulljava.io.IOException - if destination is a directoryjava.io.IOException - if destination cannot be writtenjava.io.IOException - if destination needs creating but can't bejava.io.IOException - if an IO error occurs during copyingpublic static void copyToDirectory(java.io.File sourceFile,
java.io.File destinationDir)
throws java.io.IOException
This method copies the source file or directory, along all its contents, to a directory of the same name in the specified destination directory.
The destination directory is created if it does not exist. If the destination directory did exist, then this method merges the source with the destination, with the source taking precedence.
Note: Setting preserveFileDate to true tries to preserve the file's last
modified date/times using BasicFileAttributeView.setTimes(FileTime, FileTime, FileTime), however it is
not guaranteed that the operation will succeed. If the modification operation fails it will fallback to
File.setLastModified(long) and if that fails, the methods throws IOException.
sourceFile - an existing file or directory to copy, must not be null.destinationDir - the directory to place the copy in, must not be null.java.lang.NullPointerException - if any of the given Files are null.java.lang.IllegalArgumentException - if the source or destination is invalid.java.io.FileNotFoundException - if the source does not exist.java.io.IOException - if an error occurs or setting the last-modified time didn't succeed.copyDirectoryToDirectory(File, File),
copyFileToDirectory(File, File)public static void copyToDirectory(java.lang.Iterable<java.io.File> sourceIterable,
java.io.File destinationDir)
throws java.io.IOException
This method copies the contents of the specified source files to a file of the same name in the specified destination directory. The destination directory is created if it does not exist. If the destination file exists, then this method will overwrite it.
Note: Setting preserveFileDate to true tries to preserve the file's last
modified date/times using BasicFileAttributeView.setTimes(FileTime, FileTime, FileTime), however it is
not guaranteed that the operation will succeed. If the modification operation fails it will fallback to
File.setLastModified(long) and if that fails, the methods throws IOException.
sourceIterable - existing files to copy, must not be null.destinationDir - the directory to place the copies in, must not be null.java.lang.NullPointerException - if any of the given Files are null.java.io.IOException - if source or destination is invalid.java.io.IOException - if an error occurs or setting the last-modified time didn't succeed.copyFileToDirectory(File, File)public static void copyToFile(java.io.InputStream inputStream,
java.io.File file)
throws java.io.IOException
InputStream source to a File destination. The directories
up to destination will be created if they don't already exist. destination will be
overwritten if it already exists. The source stream is left open, e.g. for use with
ZipInputStream. See copyInputStreamToFile(InputStream, File) for a
method that closes the input stream.inputStream - the InputStream to copy bytes from, must not be nullfile - the non-directory File to write bytes to (possibly overwriting), must not be
nulljava.lang.NullPointerException - if the InputStream is null.java.lang.NullPointerException - if the File is null.java.lang.IllegalArgumentException - if the file object is a directory.java.lang.IllegalArgumentException - if the file is not writable.java.io.IOException - if the directories could not be created.java.io.IOException - if an IO error occurs during copying.public static void copyURLToFile(java.net.URL source,
java.io.File destination)
throws java.io.IOException
source to a file
destination. The directories up to destination
will be created if they don't already exist. destination
will be overwritten if it already exists.
Warning: this method does not set a connection or read timeout and thus
might block forever. Use copyURLToFile(URL, File, int, int)
with reasonable timeouts to prevent this.
source - the URL to copy bytes from, must not be nulldestination - the non-directory File to write bytes to
(possibly overwriting), must not be nulljava.io.IOException - if source URL cannot be openedjava.io.IOException - if destination is a directoryjava.io.IOException - if destination cannot be writtenjava.io.IOException - if destination needs creating but can't bejava.io.IOException - if an IO error occurs during copyingpublic static void copyURLToFile(java.net.URL source,
java.io.File destination,
int connectionTimeoutMillis,
int readTimeoutMillis)
throws java.io.IOException
source to a file destination. The directories up to
destination will be created if they don't already exist. destination will be
overwritten if it already exists.source - the URL to copy bytes from, must not be nulldestination - the non-directory File to write bytes to (possibly overwriting), must not be
nullconnectionTimeoutMillis - the number of milliseconds until this method will time out if no connection could
be established to the sourcereadTimeoutMillis - the number of milliseconds until this method will time out if no data could be read from
the sourcejava.io.IOException - if source URL cannot be openedjava.io.IOException - if destination is a directoryjava.io.IOException - if destination cannot be writtenjava.io.IOException - if destination needs creating but can't bejava.io.IOException - if an IO error occurs during copyingpublic static java.io.File createParentDirectories(java.io.File file)
throws java.io.IOException
file - the File that may need parents, may be null.null if the given File does have a parent.java.io.IOException - if the directory was not created along with all its parent directories.java.lang.SecurityException - See File.mkdirs().public static java.io.File current()
public static java.io.File delete(java.io.File file)
throws java.io.IOException
File.delete() which returns a
boolean.file - The file to delete.java.lang.NullPointerException - if the parameter is nulljava.io.IOException - if the file cannot be deleted.File.delete()public static void deleteDirectory(java.io.File directory)
throws java.io.IOException
directory - directory to deletejava.io.IOException - in case deletion is unsuccessfuljava.lang.NullPointerException - if the parameter is nulljava.lang.IllegalArgumentException - if directory is not a directorypublic static boolean deleteQuietly(java.io.File file)
The difference between File.delete() and this method are:
file - file or directory to delete, can be nulltrue if the file or directory was deleted, otherwise
falsepublic static boolean directoryContains(java.io.File directory,
java.io.File child)
throws java.io.IOException
parent directory contains the child element (a file or directory).
Files are normalized before comparison.
Edge cases:directory must not be null: if null, throw IllegalArgumentExceptiondirectory must be a directory: if not a directory, throw IllegalArgumentExceptiondirectory - the file to consider as the parent.child - the file to consider as the child.java.io.IOException - if an IO error occurs while checking the files.java.lang.NullPointerException - if the given File is null.java.lang.IllegalArgumentException - if the given File does not exist or is not a directory.FilenameUtils.directoryContains(String, String)public static void forceDelete(java.io.File file)
throws java.io.IOException
The difference between File.delete() and this method are:
file - file or directory to delete, must not be null.java.lang.NullPointerException - if the file is null.java.io.FileNotFoundException - if the file was not found.java.io.IOException - in case deletion is unsuccessful.public static void forceDeleteOnExit(java.io.File file)
throws java.io.IOException
file - file or directory to delete, must not be null.java.lang.NullPointerException - if the file is null.java.io.IOException - in case deletion is unsuccessful.public static void forceMkdir(java.io.File directory)
throws java.io.IOException
directory already exists or is
null, nothing happens.
Calls File.mkdirs() and throws an IOException on failure.
directory - the receiver for mkdirs(). If the directory already exists or is null, nothing happens.java.io.IOException - if the directory was not created along with all its parent directories.java.io.IOException - if the given file object is not a directory.java.lang.SecurityException - See File.mkdirs().File.mkdirs()public static void forceMkdirParent(java.io.File file)
throws java.io.IOException
File.mkdirs() and throws an IOException on failure.
Creates all directories for a File object, including any necessary but nonexistent parent directories. If the directory already exists or is
null, nothing happens.
file - file with parents to create, must not be null.java.lang.NullPointerException - if the file is null.java.io.IOException - if the directory was not created along with all its parent directories.java.io.IOException - if the given file object is not a directory.java.lang.SecurityException - See File.mkdirs().File.mkdirs()public static java.io.File getFile(java.io.File directory,
java.lang.String... names)
directory - the parent directory.names - the name elements.public static java.io.File getFile(java.lang.String... names)
names - the name elements.public static java.io.File getTempDirectory()
File representing the system temporary directory.public static java.lang.String getTempDirectoryPath()
public static java.io.File getUserDirectory()
File representing the user's home directory.public static java.lang.String getUserDirectoryPath()
public static boolean isDirectory(java.io.File file,
java.nio.file.LinkOption... options)
File is a directory or not. Implemented as a
null-safe delegate to Files.isDirectory(Path path, LinkOption... options).file - the path to the file.options - options indicating how symbolic links are handledtrue if the file is a directory; false if
the path is null, the file does not exist, is not a directory, or it cannot
be determined if the file is a directory or not.java.lang.SecurityException - In the case of the default provider, and a security manager is installed, the
checkRead method is invoked to check read
access to the directory.public static boolean isEmptyDirectory(java.io.File directory)
throws java.io.IOException
directory - the directory to query.java.io.IOException - if an I/O error occurs.java.nio.file.NotDirectoryException - if the file could not otherwise be opened because it is not a directory
(optional specific exception).public static boolean isFileNewer(java.io.File file,
java.time.chrono.ChronoLocalDate chronoLocalDate)
File is newer than the specified ChronoLocalDate
at the end of day.
Note: The input date is assumed to be in the system default time-zone with the time
part set to the current time. To use a non-default time-zone use the method
isFileNewer(file, chronoLocalDate.atTime(LocalTime.now(zoneId)), zoneId) where
zoneId is a valid ZoneId.
file - the File of which the modification date must be compared.chronoLocalDate - the date reference.File exists and has been modified after the given
ChronoLocalDate at the current time.java.lang.NullPointerException - if the file or local date is null.public static boolean isFileNewer(java.io.File file,
java.time.chrono.ChronoLocalDate chronoLocalDate,
java.time.LocalTime localTime)
File is newer than the specified ChronoLocalDate
at the specified time.
Note: The input date and time are assumed to be in the system default time-zone. To use a
non-default time-zone use the method isFileNewer(file, chronoLocalDate.atTime(localTime), zoneId) where zoneId is a valid
ZoneId.
file - the File of which the modification date must be compared.chronoLocalDate - the date reference.localTime - the time reference.File exists and has been modified after the given
ChronoLocalDate at the given time.java.lang.NullPointerException - if the file, local date or zone ID is null.public static boolean isFileNewer(java.io.File file,
java.time.chrono.ChronoLocalDate chronoLocalDate,
java.time.OffsetTime offsetTime)
File is newer than the specified ChronoLocalDate at the specified
OffsetTime.file - the File of which the modification date must be comparedchronoLocalDate - the date referenceoffsetTime - the time referenceFile exists and has been modified after the given ChronoLocalDate at the given
OffsetTime.java.lang.NullPointerException - if the file, local date or zone ID is nullpublic static boolean isFileNewer(java.io.File file,
java.time.chrono.ChronoLocalDateTime<?> chronoLocalDateTime)
File is newer than the specified ChronoLocalDateTime
at the system-default time zone.
Note: The input date and time is assumed to be in the system default time-zone. To use a
non-default time-zone use the method isFileNewer(file, chronoLocalDateTime, zoneId) where zoneId is a valid
ZoneId.
file - the File of which the modification date must be compared.chronoLocalDateTime - the date reference.File exists and has been modified after the given
ChronoLocalDateTime at the system-default time zone.java.lang.NullPointerException - if the file or local date time is null.public static boolean isFileNewer(java.io.File file,
java.time.chrono.ChronoLocalDateTime<?> chronoLocalDateTime,
java.time.ZoneId zoneId)
File is newer than the specified ChronoLocalDateTime
at the specified ZoneId.file - the File of which the modification date must be compared.chronoLocalDateTime - the date reference.zoneId - the time zone.File exists and has been modified after the given
ChronoLocalDateTime at the given ZoneId.java.lang.NullPointerException - if the file, local date time or zone ID is null.public static boolean isFileNewer(java.io.File file,
java.time.chrono.ChronoZonedDateTime<?> chronoZonedDateTime)
File is newer than the specified ChronoZonedDateTime.file - the File of which the modification date must be compared.chronoZonedDateTime - the date reference.File exists and has been modified after the given
ChronoZonedDateTime.java.lang.NullPointerException - if the file or zoned date time is null.public static boolean isFileNewer(java.io.File file,
java.util.Date date)
File is newer than the specified Date.file - the File of which the modification date must be compared.date - the date reference.File exists and has been modified
after the given Date.java.lang.NullPointerException - if the file or date is null.public static boolean isFileNewer(java.io.File file,
java.io.File reference)
File is newer than the reference File.file - the File of which the modification date must be compared.reference - the File of which the modification date is used.File exists and has been modified more
recently than the reference File.java.lang.NullPointerException - if the file or reference file is null.java.lang.IllegalArgumentException - if the reference file doesn't exist.public static boolean isFileNewer(java.io.File file,
java.nio.file.attribute.FileTime fileTime)
throws java.io.IOException
File is newer than the specified FileTime.file - the File of which the modification date must be compared.fileTime - the file time reference.File exists and has been modified after the given FileTime.java.io.IOException - if an I/O error occurs.java.lang.NullPointerException - if the file or local date is null.public static boolean isFileNewer(java.io.File file,
java.time.Instant instant)
File is newer than the specified Instant.file - the File of which the modification date must be compared.instant - the date reference.File exists and has been modified after the given Instant.java.lang.NullPointerException - if the file or instant is null.public static boolean isFileNewer(java.io.File file,
long timeMillis)
File is newer than the specified time reference.file - the File of which the modification date must be compared.timeMillis - the time reference measured in milliseconds since the
epoch (00:00:00 GMT, January 1, 1970).File exists and has been modified after the given time reference.java.lang.NullPointerException - if the file is null.public static boolean isFileNewer(java.io.File file,
java.time.OffsetDateTime offsetDateTime)
File is newer than the specified OffsetDateTime.file - the File of which the modification date must be comparedoffsetDateTime - the date referenceFile exists and has been modified before the given OffsetDateTime.java.lang.NullPointerException - if the file or zoned date time is nullpublic static boolean isFileOlder(java.io.File file,
java.time.chrono.ChronoLocalDate chronoLocalDate)
File is older than the specified ChronoLocalDate
at the end of day.
Note: The input date is assumed to be in the system default time-zone with the time
part set to the current time. To use a non-default time-zone use the method
isFileOlder(file, chronoLocalDate.atTime(LocalTime.now(zoneId)), zoneId) where
zoneId is a valid ZoneId.
file - the File of which the modification date must be compared.chronoLocalDate - the date reference.File exists and has been modified before the given
ChronoLocalDate at the current time.java.lang.NullPointerException - if the file or local date is null.ZoneId.systemDefault(),
LocalTime.now()public static boolean isFileOlder(java.io.File file,
java.time.chrono.ChronoLocalDate chronoLocalDate,
java.time.LocalTime localTime)
File is older than the specified ChronoLocalDate
at the specified LocalTime.
Note: The input date and time are assumed to be in the system default time-zone. To use a
non-default time-zone use the method isFileOlder(file, chronoLocalDate.atTime(localTime), zoneId) where zoneId is a valid
ZoneId.
file - the File of which the modification date must be compared.chronoLocalDate - the date reference.localTime - the time reference.File exists and has been modified before the
given ChronoLocalDate at the specified time.java.lang.NullPointerException - if the file, local date or local time is null.ZoneId.systemDefault()public static boolean isFileOlder(java.io.File file,
java.time.chrono.ChronoLocalDate chronoLocalDate,
java.time.OffsetTime offsetTime)
File is older than the specified ChronoLocalDate at the specified
OffsetTime.file - the File of which the modification date must be comparedchronoLocalDate - the date referenceoffsetTime - the time referenceFile exists and has been modified after the given ChronoLocalDate at the given
OffsetTime.java.lang.NullPointerException - if the file, local date or zone ID is nullpublic static boolean isFileOlder(java.io.File file,
java.time.chrono.ChronoLocalDateTime<?> chronoLocalDateTime)
File is older than the specified ChronoLocalDateTime
at the system-default time zone.
Note: The input date and time is assumed to be in the system default time-zone. To use a
non-default time-zone use the method isFileOlder(file, chronoLocalDateTime, zoneId) where zoneId is a valid
ZoneId.
file - the File of which the modification date must be compared.chronoLocalDateTime - the date reference.File exists and has been modified before the given
ChronoLocalDateTime at the system-default time zone.java.lang.NullPointerException - if the file or local date time is null.ZoneId.systemDefault()public static boolean isFileOlder(java.io.File file,
java.time.chrono.ChronoLocalDateTime<?> chronoLocalDateTime,
java.time.ZoneId zoneId)
File is older than the specified ChronoLocalDateTime
at the specified ZoneId.file - the File of which the modification date must be compared.chronoLocalDateTime - the date reference.zoneId - the time zone.File exists and has been modified before the given
ChronoLocalDateTime at the given ZoneId.java.lang.NullPointerException - if the file, local date time or zone ID is null.public static boolean isFileOlder(java.io.File file,
java.time.chrono.ChronoZonedDateTime<?> chronoZonedDateTime)
File is older than the specified ChronoZonedDateTime.file - the File of which the modification date must be compared.chronoZonedDateTime - the date reference.File exists and has been modified before the given
ChronoZonedDateTime.java.lang.NullPointerException - if the file or zoned date time is null.public static boolean isFileOlder(java.io.File file,
java.util.Date date)
File is older than the specified Date.file - the File of which the modification date must be compared.date - the date reference.File exists and has been modified before the given Date.java.lang.NullPointerException - if the file or date is null.public static boolean isFileOlder(java.io.File file,
java.io.File reference)
File is older than the reference File.file - the File of which the modification date must be compared.reference - the File of which the modification date is used.File exists and has been modified before the reference File.java.lang.NullPointerException - if the file or reference file is null.java.lang.IllegalArgumentException - if the reference file doesn't exist.public static boolean isFileOlder(java.io.File file,
java.nio.file.attribute.FileTime fileTime)
throws java.io.IOException
File is older than the specified FileTime.file - the File of which the modification date must be compared.fileTime - the file time reference.File exists and has been modified before the given FileTime.java.io.IOException - if an I/O error occurs.java.lang.NullPointerException - if the file or local date is null.public static boolean isFileOlder(java.io.File file,
java.time.Instant instant)
File is older than the specified Instant.file - the File of which the modification date must be compared.instant - the date reference.File exists and has been modified before the given Instant.java.lang.NullPointerException - if the file or instant is null.public static boolean isFileOlder(java.io.File file,
long timeMillis)
File is older than the specified time reference.file - the File of which the modification date must be compared.timeMillis - the time reference measured in milliseconds since the
epoch (00:00:00 GMT, January 1, 1970).File exists and has been modified before the given time reference.java.lang.NullPointerException - if the file is null.public static boolean isFileOlder(java.io.File file,
java.time.OffsetDateTime offsetDateTime)
File is older than the specified OffsetDateTime.file - the File of which the modification date must be comparedoffsetDateTime - the date referenceFile exists and has been modified before the given OffsetDateTime.java.lang.NullPointerException - if the file or zoned date time is nullpublic static boolean isRegularFile(java.io.File file,
java.nio.file.LinkOption... options)
File is a regular file or not. Implemented as a
null-safe delegate to Files.isRegularFile(Path path, LinkOption... options).file - the path to the file.options - options indicating how symbolic links are handledtrue if the file is a regular file; false if
the path is null, the file does not exist, is not a regular file, or it cannot
be determined if the file is a regular file or not.java.lang.SecurityException - In the case of the default provider, and a security manager is installed, the
checkRead method is invoked to check read
access to the directory.public static boolean isSymlink(java.io.File file)
This method delegates to Files.isSymbolicLink(Path path)
file - the file to test.Files.isSymbolicLink(Path path).Files.isSymbolicLink(Path)public static java.util.Iterator<java.io.File> iterateFiles(java.io.File directory,
IOFileFilter fileFilter,
IOFileFilter dirFilter)
The resulting iterator MUST be consumed in its entirety in order to close its underlying stream.
All files found are filtered by an IOFileFilter.
directory - the directory to search infileFilter - filter to apply when finding files.dirFilter - optional filter to apply when finding subdirectories.
If this parameter is null, subdirectories will not be included in the
search. Use TrueFileFilter.INSTANCE to match all directories.FileFilterUtils,
NameFileFilterpublic static java.util.Iterator<java.io.File> iterateFiles(java.io.File directory,
java.lang.String[] extensions,
boolean recursive)
The resulting iterator MUST be consumed in its entirety in order to close its underlying stream.
directory - the directory to search inextensions - an array of extensions, ex. {"java","xml"}. If this
parameter is null, all files are returned.recursive - if true all subdirectories are searched as wellpublic static java.util.Iterator<java.io.File> iterateFilesAndDirs(java.io.File directory,
IOFileFilter fileFilter,
IOFileFilter dirFilter)
The resulting iterator MUST be consumed in its entirety in order to close its underlying stream.
All files found are filtered by an IOFileFilter.
The resulting iterator includes the subdirectories themselves.
directory - the directory to search infileFilter - filter to apply when finding files.dirFilter - optional filter to apply when finding subdirectories.
If this parameter is null, subdirectories will not be included in the
search. Use TrueFileFilter.INSTANCE to match all directories.FileFilterUtils,
NameFileFilterpublic static long lastModified(java.io.File file)
throws java.io.IOException
Files.getLastModifiedTime(Path, LinkOption...).
For the best precision, use lastModifiedFileTime(File).
Use this method to avoid issues with File.lastModified() like
JDK-8177809 where File.lastModified() is
losing milliseconds (always ends in 000). This bug exists in OpenJDK 8 and 9, and is fixed in 10.
file - The File to query.FileTime.toMillis().java.io.IOException - if an I/O error occurs.public static java.nio.file.attribute.FileTime lastModifiedFileTime(java.io.File file)
throws java.io.IOException
FileTime via
Files.getLastModifiedTime(Path, LinkOption...).
Use this method to avoid issues with File.lastModified() like
JDK-8177809 where File.lastModified() is
losing milliseconds (always ends in 000). This bug exists in OpenJDK 8 and 9, and is fixed in 10.
file - The File to query.Files.getLastModifiedTime(Path, LinkOption...).java.io.IOException - if an I/O error occurs.public static long lastModifiedUnchecked(java.io.File file)
Files.getLastModifiedTime(Path, LinkOption...).
For the best precision, use lastModifiedFileTime(File).
Use this method to avoid issues with File.lastModified() like
JDK-8177809 where File.lastModified() is
losing milliseconds (always ends in 000). This bug exists in OpenJDK 8 and 9, and is fixed in 10.
file - The File to query.FileTime.toMillis().java.io.UncheckedIOException - if an I/O error occurs.public static LineIterator lineIterator(java.io.File file) throws java.io.IOException
File using the default encoding for the VM.file - the file to open for input, must not be nullnulljava.lang.NullPointerException - if file is null.java.io.FileNotFoundException - if the file does not exist, is a directory rather than a regular file, or for some
other reason cannot be opened for reading.java.io.IOException - if an I/O error occurs.lineIterator(File, String)public static LineIterator lineIterator(java.io.File file, java.lang.String charsetName) throws java.io.IOException
File.
This method opens an InputStream for the file.
When you have finished with the iterator you should close the stream
to free internal resources. This can be done by using a try-with-resources block or calling the
LineIterator.close() method.
The recommended usage pattern is:
LineIterator it = FileUtils.lineIterator(file, StandardCharsets.UTF_8.name());
try {
while (it.hasNext()) {
String line = it.nextLine();
/// do something with line
}
} finally {
LineIterator.closeQuietly(iterator);
}
If an exception occurs during the creation of the iterator, the underlying stream is closed.
file - the file to open for input, must not be nullcharsetName - the name of the requested charset, null means platform defaultnull; MUST be closed by the caller.java.lang.NullPointerException - if file is null.java.io.FileNotFoundException - if the file does not exist, is a directory rather than a regular file, or for some
other reason cannot be opened for reading.java.io.IOException - if an I/O error occurs.public static java.util.Collection<java.io.File> listFiles(java.io.File directory,
IOFileFilter fileFilter,
IOFileFilter dirFilter)
If your search should recurse into subdirectories you can pass in an IOFileFilter for directories. You don't need to bind a DirectoryFileFilter (via logical AND) to this filter. This method does that for you.
An example: If you want to search through all directories called
"temp" you pass in FileFilterUtils.NameFileFilter("temp")
Another common usage of this method is find files in a directory
tree but ignoring the directories generated CVS. You can simply pass
in FileFilterUtils.makeCVSAware(null).
directory - the directory to search infileFilter - filter to apply when finding files. Must not be null,
use TrueFileFilter.INSTANCE to match all files in selected directories.dirFilter - optional filter to apply when finding subdirectories.
If this parameter is null, subdirectories will not be included in the
search. Use TrueFileFilter.INSTANCE to match all directories.FileFilterUtils,
NameFileFilterpublic static java.util.Collection<java.io.File> listFiles(java.io.File directory,
java.lang.String[] extensions,
boolean recursive)
directory - the directory to search inextensions - an array of extensions, ex. {"java","xml"}. If this
parameter is null, all files are returned.recursive - if true all subdirectories are searched as wellpublic static java.util.Collection<java.io.File> listFilesAndDirs(java.io.File directory,
IOFileFilter fileFilter,
IOFileFilter dirFilter)
The resulting collection includes the starting directory and any subdirectories that match the directory filter.
directory - the directory to search infileFilter - filter to apply when finding files.dirFilter - optional filter to apply when finding subdirectories.
If this parameter is null, subdirectories will not be included in the
search. Use TrueFileFilter.INSTANCE to match all directories.listFiles(java.io.File, java.io.FileFilter),
FileFilterUtils,
NameFileFilterpublic static void moveDirectory(java.io.File srcDir,
java.io.File destDir)
throws java.io.IOException
When the destination directory is on another file system, do a "copy and delete".
srcDir - the directory to be moved.destDir - the destination directory.java.lang.NullPointerException - if any of the given Files are null.java.lang.IllegalArgumentException - if the source or destination is invalid.java.io.FileNotFoundException - if the source does not exist.java.io.IOException - if an error occurs or setting the last-modified time didn't succeed.public static void moveDirectoryToDirectory(java.io.File source,
java.io.File destDir,
boolean createDestDir)
throws java.io.IOException
If createDestDir is true, creates all destination parent directories, including any necessary but nonexistent parent directories.
source - the file to be moved.destDir - the destination file.createDestDir - If true create the destination directory, otherwise if false throw an
IOException.java.lang.NullPointerException - if any of the given Files are null.java.lang.IllegalArgumentException - if the source or destination is invalid.java.io.FileNotFoundException - if the source does not exist.java.io.IOException - if the directory was not created along with all its parent directories, if enabled.java.io.IOException - if an error occurs or setting the last-modified time didn't succeed.java.lang.SecurityException - See File.mkdirs().public static void moveFile(java.io.File srcFile,
java.io.File destFile)
throws java.io.IOException
Shorthand for moveFile(srcFile, destFile, StandardCopyOption.COPY_ATTRIBUTES).
When the destination file is on another file system, do a "copy and delete".
srcFile - the file to be moved.destFile - the destination file.java.lang.NullPointerException - if any of the given Files are null.FileExistsException - if the destination file exists.java.io.FileNotFoundException - if the source file does not exist.java.io.IOException - if source or destination is invalid.java.io.IOException - if an error occurs.public static void moveFile(java.io.File srcFile,
java.io.File destFile,
java.nio.file.CopyOption... copyOptions)
throws java.io.IOException
When the destination file is on another file system, do a "copy and delete".
srcFile - the file to be moved.destFile - the destination file.copyOptions - Copy options.java.lang.NullPointerException - if any of the given Files are null.FileExistsException - if the destination file exists.java.io.FileNotFoundException - if the source file does not exist.java.io.IOException - if source or destination is invalid.java.io.IOException - if an error occurs or setting the last-modified time didn't succeed.public static void moveFileToDirectory(java.io.File srcFile,
java.io.File destDir,
boolean createDestDir)
throws java.io.IOException
If createDestDir is true, creates all destination parent directories, including any necessary but nonexistent parent directories.
srcFile - the file to be moved.destDir - the destination file.createDestDir - If true create the destination directory, otherwise if false throw an
IOException.java.lang.NullPointerException - if any of the given Files are null.FileExistsException - if the destination file exists.java.io.FileNotFoundException - if the source file does not exist.java.io.IOException - if source or destination is invalid.java.io.IOException - if the directory was not created along with all its parent directories, if enabled.java.io.IOException - if an error occurs or setting the last-modified time didn't succeed.java.lang.SecurityException - See File.mkdirs().public static void moveToDirectory(java.io.File src,
java.io.File destDir,
boolean createDestDir)
throws java.io.IOException
If createDestDir is true, creates all destination parent directories, including any necessary but nonexistent parent directories.
When the destination is on another file system, do a "copy and delete".
src - the file or directory to be moved.destDir - the destination directory.createDestDir - If true create the destination directory, otherwise if false throw an IOException.java.lang.NullPointerException - if any of the given Files are null.FileExistsException - if the directory or file exists in the destination directory.java.io.FileNotFoundException - if the source file does not exist.java.io.IOException - if source or destination is invalid.java.io.IOException - if an error occurs or setting the last-modified time didn't succeed.public static java.io.OutputStream newOutputStream(java.io.File file,
boolean append)
throws java.io.IOException
append - Whether or not to append.file - the File.java.io.IOException - if an I/O error occurs.PathUtils.newOutputStream(Path, boolean)public static java.io.FileInputStream openInputStream(java.io.File file)
throws java.io.IOException
FileInputStream for the specified file, providing better error messages than simply calling
new FileInputStream(file).
At the end of the method either the stream will be successfully opened, or an exception will have been thrown.
An exception is thrown if the file does not exist. An exception is thrown if the file object exists but is a directory. An exception is thrown if the file exists but cannot be read.
file - the file to open for input, must not be nullFileInputStream for the specified filejava.lang.NullPointerException - if file is null.java.io.FileNotFoundException - if the file does not exist, is a directory rather than a regular file, or for some
other reason cannot be opened for reading.java.io.IOException - See FileNotFoundException above, FileNotFoundException is a subclass of IOException.public static java.io.FileOutputStream openOutputStream(java.io.File file)
throws java.io.IOException
FileOutputStream for the specified file, checking and
creating the parent directory if it does not exist.
At the end of the method either the stream will be successfully opened, or an exception will have been thrown.
The parent directory will be created if it does not exist. The file will be created if it does not exist. An exception is thrown if the file object exists but is a directory. An exception is thrown if the file exists but cannot be written to. An exception is thrown if the parent directory cannot be created.
file - the file to open for output, must not be nullFileOutputStream for the specified filejava.lang.NullPointerException - if the file object is null.java.lang.IllegalArgumentException - if the file object is a directoryjava.lang.IllegalArgumentException - if the file is not writable.java.io.IOException - if the directories could not be created.public static java.io.FileOutputStream openOutputStream(java.io.File file,
boolean append)
throws java.io.IOException
FileOutputStream for the specified file, checking and
creating the parent directory if it does not exist.
At the end of the method either the stream will be successfully opened, or an exception will have been thrown.
The parent directory will be created if it does not exist. The file will be created if it does not exist. An exception is thrown if the file object exists but is a directory. An exception is thrown if the file exists but cannot be written to. An exception is thrown if the parent directory cannot be created.
file - the file to open for output, must not be nullappend - if true, then bytes will be added to the
end of the file rather than overwritingFileOutputStream for the specified filejava.lang.NullPointerException - if the file object is null.java.lang.IllegalArgumentException - if the file object is a directoryjava.lang.IllegalArgumentException - if the file is not writable.java.io.IOException - if the directories could not be created.public static byte[] readFileToByteArray(java.io.File file)
throws java.io.IOException
file - the file to read, must not be nullnulljava.lang.NullPointerException - if file is null.java.io.FileNotFoundException - if the file does not exist, is a directory rather than a regular file, or for some
other reason cannot be opened for reading.java.io.IOException - if an I/O error occurs.@Deprecated
public static java.lang.String readFileToString(java.io.File file)
throws java.io.IOException
readFileToString(File, Charset) instead (and specify the appropriate encoding)file - the file to read, must not be nullnulljava.lang.NullPointerException - if file is null.java.io.FileNotFoundException - if the file does not exist, is a directory rather than a regular file, or for some
other reason cannot be opened for reading.java.io.IOException - if an I/O error occurs.public static java.lang.String readFileToString(java.io.File file,
java.nio.charset.Charset charsetName)
throws java.io.IOException
file - the file to read, must not be nullcharsetName - the name of the requested charset, null means platform defaultnulljava.lang.NullPointerException - if file is null.java.io.FileNotFoundException - if the file does not exist, is a directory rather than a regular file, or for some
other reason cannot be opened for reading.java.io.IOException - if an I/O error occurs.public static java.lang.String readFileToString(java.io.File file,
java.lang.String charsetName)
throws java.io.IOException
file - the file to read, must not be nullcharsetName - the name of the requested charset, null means platform defaultnulljava.lang.NullPointerException - if file is null.java.io.FileNotFoundException - if the file does not exist, is a directory rather than a regular file, or for some
other reason cannot be opened for reading.java.io.IOException - if an I/O error occurs.java.nio.charset.UnsupportedCharsetException - thrown instead of .UnsupportedEncodingException in version 2.2 if the named charset is unavailable.@Deprecated
public static java.util.List<java.lang.String> readLines(java.io.File file)
throws java.io.IOException
readLines(File, Charset) instead (and specify the appropriate encoding)file - the file to read, must not be nullnulljava.lang.NullPointerException - if file is null.java.io.FileNotFoundException - if the file does not exist, is a directory rather than a regular file, or for some
other reason cannot be opened for reading.java.io.IOException - if an I/O error occurs.public static java.util.List<java.lang.String> readLines(java.io.File file,
java.nio.charset.Charset charset)
throws java.io.IOException
file - the file to read, must not be nullcharset - the charset to use, null means platform defaultnulljava.lang.NullPointerException - if file is null.java.io.FileNotFoundException - if the file does not exist, is a directory rather than a regular file, or for some
other reason cannot be opened for reading.java.io.IOException - if an I/O error occurs.public static java.util.List<java.lang.String> readLines(java.io.File file,
java.lang.String charsetName)
throws java.io.IOException
file - the file to read, must not be nullcharsetName - the name of the requested charset, null means platform defaultnulljava.lang.NullPointerException - if file is null.java.io.FileNotFoundException - if the file does not exist, is a directory rather than a regular file, or for some
other reason cannot be opened for reading.java.io.IOException - if an I/O error occurs.java.nio.charset.UnsupportedCharsetException - thrown instead of .UnsupportedEncodingException in version 2.2 if the named charset is unavailable.public static long sizeOf(java.io.File file)
File is a regular file, then the file's length is returned.
If the argument is a directory, then the size of the directory is
calculated recursively. If a directory or subdirectory is security
restricted, its size will not be included.
Note that overflow is not detected, and the return value may be negative if
overflow occurs. See sizeOfAsBigInteger(File) for an alternative
method that does not overflow.
file - the regular file or directory to return the size
of (must not be null).java.lang.NullPointerException - if the file is null.java.lang.IllegalArgumentException - if the file does not exist.java.io.UncheckedIOException - if an IO error occurs.public static java.math.BigInteger sizeOfAsBigInteger(java.io.File file)
File is a regular file, then the file's length is returned.
If the argument is a directory, then the size of the directory is
calculated recursively. If a directory or subdirectory is security
restricted, its size will not be included.file - the regular file or directory to return the size
of (must not be null).java.lang.NullPointerException - if the file is null.java.lang.IllegalArgumentException - if the file does not exist.java.io.UncheckedIOException - if an IO error occurs.public static long sizeOfDirectory(java.io.File directory)
Note that overflow is not detected, and the return value may be negative if
overflow occurs. See sizeOfDirectoryAsBigInteger(File) for an alternative
method that does not overflow.
directory - directory to inspect, must not be null.Long.MAX_VALUE.java.lang.NullPointerException - if the directory is null.java.io.UncheckedIOException - if an IO error occurs.public static java.math.BigInteger sizeOfDirectoryAsBigInteger(java.io.File directory)
directory - directory to inspect, must not be null.java.lang.NullPointerException - if the directory is null.java.io.UncheckedIOException - if an IO error occurs.public static java.util.stream.Stream<java.io.File> streamFiles(java.io.File directory,
boolean recursive,
java.lang.String... extensions)
throws java.io.IOException
directory - the directory to search inrecursive - if true all subdirectories are searched as wellextensions - an array of extensions, ex. {"java","xml"}. If this
parameter is null, all files are returned.java.io.IOException - if an I/O error is thrown when accessing the starting file.public static java.io.File toFile(java.net.URL url)
URL to a File.
From version 1.1 this method will decode the URL.
Syntax such as file:///my%20docs/file.txt will be
correctly decoded to /my docs/file.txt. Starting with version
1.5, this method uses UTF-8 to decode percent-encoded octets to characters.
Additionally, malformed percent-encoded octets are handled leniently by
passing them through literally.
url - the file URL to convert, null returns nullFile object, or null
if the URL's protocol is not filepublic static java.io.File[] toFiles(java.net.URL... urls)
URL to a File.
Returns an array of the same size as the input.
If the input is null, an empty array is returned.
If the input contains null, the output array contains null at the same
index.
This method will decode the URL.
Syntax such as file:///my%20docs/file.txt will be
correctly decoded to /my docs/file.txt.
urls - the file URLs to convert, null returns empty arraynull array of Files matching the input, with a null item
if there was a null at that index in the input arrayjava.lang.IllegalArgumentException - if any file is not a URL filejava.lang.IllegalArgumentException - if any file is incorrectly encodedpublic static void touch(java.io.File file)
throws java.io.IOException
NOTE: As from v1.3, this method throws an IOException if the last modified date of the file cannot be set. Also, as from v1.3 this method creates parent directories if they do not exist.
file - the File to touch.java.lang.NullPointerException - if the parameter is null.java.io.IOException - if setting the last-modified time failed or an I/O problem occurs.public static java.net.URL[] toURLs(java.io.File... files)
throws java.io.IOException
File to a URL.
Returns an array of the same size as the input.
files - the files to convert, must not be nulljava.io.IOException - if a file cannot be convertedjava.lang.NullPointerException - if the parameter is nullpublic static boolean waitFor(java.io.File file,
int seconds)
This method repeatedly tests Files.exists(Path, LinkOption...) until it returns
true up to the maximum time specified in seconds.
file - the file to check, must not be nullseconds - the maximum time in seconds to waitjava.lang.NullPointerException - if the file is null@Deprecated
public static void write(java.io.File file,
java.lang.CharSequence data)
throws java.io.IOException
write(File, CharSequence, Charset) instead (and specify the appropriate encoding)file - the file to writedata - the content to write to the filejava.io.IOException - in case of an I/O error@Deprecated
public static void write(java.io.File file,
java.lang.CharSequence data,
boolean append)
throws java.io.IOException
write(File, CharSequence, Charset, boolean) instead (and specify the appropriate encoding)file - the file to writedata - the content to write to the fileappend - if true, then the data will be added to the
end of the file rather than overwritingjava.io.IOException - in case of an I/O errorpublic static void write(java.io.File file,
java.lang.CharSequence data,
java.nio.charset.Charset charset)
throws java.io.IOException
file - the file to writedata - the content to write to the filecharset - the name of the requested charset, null means platform defaultjava.io.IOException - in case of an I/O errorpublic static void write(java.io.File file,
java.lang.CharSequence data,
java.nio.charset.Charset charset,
boolean append)
throws java.io.IOException
file - the file to writedata - the content to write to the filecharset - the charset to use, null means platform defaultappend - if true, then the data will be added to the
end of the file rather than overwritingjava.io.IOException - in case of an I/O errorpublic static void write(java.io.File file,
java.lang.CharSequence data,
java.lang.String charsetName)
throws java.io.IOException
file - the file to writedata - the content to write to the filecharsetName - the name of the requested charset, null means platform defaultjava.io.IOException - in case of an I/O errorjava.io.UnsupportedEncodingException - if the encoding is not supported by the VMpublic static void write(java.io.File file,
java.lang.CharSequence data,
java.lang.String charsetName,
boolean append)
throws java.io.IOException
file - the file to writedata - the content to write to the filecharsetName - the name of the requested charset, null means platform defaultappend - if true, then the data will be added to the
end of the file rather than overwritingjava.io.IOException - in case of an I/O errorjava.nio.charset.UnsupportedCharsetException - thrown instead of .UnsupportedEncodingException in version 2.2 if the encoding is not supported by the VMpublic static void writeByteArrayToFile(java.io.File file,
byte[] data)
throws java.io.IOException
NOTE: As from v1.3, the parent directories of the file will be created if they do not exist.
file - the file to write todata - the content to write to the filejava.io.IOException - in case of an I/O errorpublic static void writeByteArrayToFile(java.io.File file,
byte[] data,
boolean append)
throws java.io.IOException
file - the file to write todata - the content to write to the fileappend - if true, then bytes will be added to the
end of the file rather than overwritingjava.io.IOException - in case of an I/O errorpublic static void writeByteArrayToFile(java.io.File file,
byte[] data,
int off,
int len)
throws java.io.IOException
len bytes from the specified byte array starting
at offset off to a file, creating the file if it does
not exist.file - the file to write todata - the content to write to the fileoff - the start offset in the datalen - the number of bytes to writejava.io.IOException - in case of an I/O errorpublic static void writeByteArrayToFile(java.io.File file,
byte[] data,
int off,
int len,
boolean append)
throws java.io.IOException
len bytes from the specified byte array starting
at offset off to a file, creating the file if it does
not exist.file - the file to write todata - the content to write to the fileoff - the start offset in the datalen - the number of bytes to writeappend - if true, then bytes will be added to the
end of the file rather than overwritingjava.io.IOException - in case of an I/O errorpublic static void writeLines(java.io.File file,
java.util.Collection<?> lines)
throws java.io.IOException
toString() value of each item in a collection to
the specified File line by line.
The default VM encoding and the default line ending will be used.file - the file to write tolines - the lines to write, null entries produce blank linesjava.io.IOException - in case of an I/O errorpublic static void writeLines(java.io.File file,
java.util.Collection<?> lines,
boolean append)
throws java.io.IOException
toString() value of each item in a collection to
the specified File line by line.
The default VM encoding and the default line ending will be used.file - the file to write tolines - the lines to write, null entries produce blank linesappend - if true, then the lines will be added to the
end of the file rather than overwritingjava.io.IOException - in case of an I/O errorpublic static void writeLines(java.io.File file,
java.util.Collection<?> lines,
java.lang.String lineEnding)
throws java.io.IOException
toString() value of each item in a collection to
the specified File line by line.
The default VM encoding and the specified line ending will be used.file - the file to write tolines - the lines to write, null entries produce blank lineslineEnding - the line separator to use, null is system defaultjava.io.IOException - in case of an I/O errorpublic static void writeLines(java.io.File file,
java.util.Collection<?> lines,
java.lang.String lineEnding,
boolean append)
throws java.io.IOException
toString() value of each item in a collection to
the specified File line by line.
The default VM encoding and the specified line ending will be used.file - the file to write tolines - the lines to write, null entries produce blank lineslineEnding - the line separator to use, null is system defaultappend - if true, then the lines will be added to the
end of the file rather than overwritingjava.io.IOException - in case of an I/O errorpublic static void writeLines(java.io.File file,
java.lang.String charsetName,
java.util.Collection<?> lines)
throws java.io.IOException
toString() value of each item in a collection to
the specified File line by line.
The specified character encoding and the default line ending will be used.
NOTE: As from v1.3, the parent directories of the file will be created if they do not exist.
file - the file to write tocharsetName - the name of the requested charset, null means platform defaultlines - the lines to write, null entries produce blank linesjava.io.IOException - in case of an I/O errorjava.io.UnsupportedEncodingException - if the encoding is not supported by the VMpublic static void writeLines(java.io.File file,
java.lang.String charsetName,
java.util.Collection<?> lines,
boolean append)
throws java.io.IOException
toString() value of each item in a collection to
the specified File line by line, optionally appending.
The specified character encoding and the default line ending will be used.file - the file to write tocharsetName - the name of the requested charset, null means platform defaultlines - the lines to write, null entries produce blank linesappend - if true, then the lines will be added to the
end of the file rather than overwritingjava.io.IOException - in case of an I/O errorjava.io.UnsupportedEncodingException - if the encoding is not supported by the VMpublic static void writeLines(java.io.File file,
java.lang.String charsetName,
java.util.Collection<?> lines,
java.lang.String lineEnding)
throws java.io.IOException
toString() value of each item in a collection to
the specified File line by line.
The specified character encoding and the line ending will be used.
NOTE: As from v1.3, the parent directories of the file will be created if they do not exist.
file - the file to write tocharsetName - the name of the requested charset, null means platform defaultlines - the lines to write, null entries produce blank lineslineEnding - the line separator to use, null is system defaultjava.io.IOException - in case of an I/O errorjava.io.UnsupportedEncodingException - if the encoding is not supported by the VMpublic static void writeLines(java.io.File file,
java.lang.String charsetName,
java.util.Collection<?> lines,
java.lang.String lineEnding,
boolean append)
throws java.io.IOException
toString() value of each item in a collection to
the specified File line by line.
The specified character encoding and the line ending will be used.file - the file to write tocharsetName - the name of the requested charset, null means platform defaultlines - the lines to write, null entries produce blank lineslineEnding - the line separator to use, null is system defaultappend - if true, then the lines will be added to the
end of the file rather than overwritingjava.io.IOException - in case of an I/O errorjava.io.UnsupportedEncodingException - if the encoding is not supported by the VM@Deprecated
public static void writeStringToFile(java.io.File file,
java.lang.String data)
throws java.io.IOException
writeStringToFile(File, String, Charset) instead (and specify the appropriate encoding)file - the file to writedata - the content to write to the filejava.io.IOException - in case of an I/O error@Deprecated
public static void writeStringToFile(java.io.File file,
java.lang.String data,
boolean append)
throws java.io.IOException
writeStringToFile(File, String, Charset, boolean) instead (and specify the appropriate encoding)file - the file to writedata - the content to write to the fileappend - if true, then the String will be added to the
end of the file rather than overwritingjava.io.IOException - in case of an I/O errorpublic static void writeStringToFile(java.io.File file,
java.lang.String data,
java.nio.charset.Charset charset)
throws java.io.IOException
NOTE: As from v1.3, the parent directories of the file will be created if they do not exist.
file - the file to writedata - the content to write to the filecharset - the charset to use, null means platform defaultjava.io.IOException - in case of an I/O errorjava.io.UnsupportedEncodingException - if the encoding is not supported by the VMpublic static void writeStringToFile(java.io.File file,
java.lang.String data,
java.nio.charset.Charset charset,
boolean append)
throws java.io.IOException
file - the file to writedata - the content to write to the filecharset - the charset to use, null means platform defaultappend - if true, then the String will be added to the
end of the file rather than overwritingjava.io.IOException - in case of an I/O errorpublic static void writeStringToFile(java.io.File file,
java.lang.String data,
java.lang.String charsetName)
throws java.io.IOException
NOTE: As from v1.3, the parent directories of the file will be created if they do not exist.
file - the file to writedata - the content to write to the filecharsetName - the name of the requested charset, null means platform defaultjava.io.IOException - in case of an I/O errorjava.io.UnsupportedEncodingException - if the encoding is not supported by the VMpublic static void writeStringToFile(java.io.File file,
java.lang.String data,
java.lang.String charsetName,
boolean append)
throws java.io.IOException
file - the file to writedata - the content to write to the filecharsetName - the name of the requested charset, null means platform defaultappend - if true, then the String will be added to the
end of the file rather than overwritingjava.io.IOException - in case of an I/O errorjava.nio.charset.UnsupportedCharsetException - thrown instead of .UnsupportedEncodingException in version 2.2 if the encoding is not supported by the VMCopyright © 2010 - 2023 Adobe. All Rights Reserved