You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2019/10/10 15:44:15 UTC
[commons-io] 01/02: Sort members.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git
commit 633cd397779599a83cbda0edaed02475302c9597
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Thu Oct 10 11:20:32 2019 -0400
Sort members.
---
src/main/java/org/apache/commons/io/FileUtils.java | 4204 ++++++++++----------
1 file changed, 2102 insertions(+), 2102 deletions(-)
diff --git a/src/main/java/org/apache/commons/io/FileUtils.java b/src/main/java/org/apache/commons/io/FileUtils.java
index 346d0e3..9792a24 100644
--- a/src/main/java/org/apache/commons/io/FileUtils.java
+++ b/src/main/java/org/apache/commons/io/FileUtils.java
@@ -82,13 +82,6 @@ import org.apache.commons.io.output.NullOutputStream;
public class FileUtils {
/**
- * Instances should NOT be constructed in standard programming.
- */
- public FileUtils() {
- super();
- }
-
- /**
* The number of bytes in a kilobyte.
*/
public static final long ONE_KB = 1024;
@@ -177,200 +170,6 @@ public class FileUtils {
//-----------------------------------------------------------------------
/**
- * Construct a file from the set of name elements.
- *
- * @param directory the parent directory
- * @param names the name elements
- * @return the file
- * @since 2.1
- */
- public static File getFile(final File directory, final String... names) {
- if (directory == null) {
- throw new NullPointerException("directory must not be null");
- }
- if (names == null) {
- throw new NullPointerException("names must not be null");
- }
- File file = directory;
- for (final String name : names) {
- file = new File(file, name);
- }
- return file;
- }
-
- /**
- * Construct a file from the set of name elements.
- *
- * @param names the name elements
- * @return the file
- * @since 2.1
- */
- public static File getFile(final String... names) {
- if (names == null) {
- throw new NullPointerException("names must not be null");
- }
- File file = null;
- for (final String name : names) {
- if (file == null) {
- file = new File(name);
- } else {
- file = new File(file, name);
- }
- }
- return file;
- }
-
- /**
- * Returns the path to the system temporary directory.
- *
- * @return the path to the system temporary directory.
- *
- * @since 2.0
- */
- public static String getTempDirectoryPath() {
- return System.getProperty("java.io.tmpdir");
- }
-
- /**
- * Returns a {@link File} representing the system temporary directory.
- *
- * @return the system temporary directory.
- *
- * @since 2.0
- */
- public static File getTempDirectory() {
- return new File(getTempDirectoryPath());
- }
-
- /**
- * Returns the path to the user's home directory.
- *
- * @return the path to the user's home directory.
- *
- * @since 2.0
- */
- public static String getUserDirectoryPath() {
- return System.getProperty("user.home");
- }
-
- /**
- * Returns a {@link File} representing the user's home directory.
- *
- * @return the user's home directory.
- *
- * @since 2.0
- */
- public static File getUserDirectory() {
- return new File(getUserDirectoryPath());
- }
-
- //-----------------------------------------------------------------------
- /**
- * Opens a {@link FileInputStream} for the specified file, providing better
- * error messages than simply calling <code>new FileInputStream(file)</code>.
- * <p>
- * At the end of the method either the stream will be successfully opened,
- * or an exception will have been thrown.
- * </p>
- * <p>
- * 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.
- * </p>
- *
- * @param file the file to open for input, must not be {@code null}
- * @return a new {@link FileInputStream} for the specified file
- * @throws FileNotFoundException if the file does not exist
- * @throws IOException if the file object is a directory
- * @throws IOException if the file cannot be read
- * @since 1.3
- */
- public static FileInputStream openInputStream(final File file) throws IOException {
- if (file.exists()) {
- if (file.isDirectory()) {
- throw new IOException("File '" + file + "' exists but is a directory");
- }
- if (file.canRead() == false) {
- throw new IOException("File '" + file + "' cannot be read");
- }
- } else {
- throw new FileNotFoundException("File '" + file + "' does not exist");
- }
- return new FileInputStream(file);
- }
-
- //-----------------------------------------------------------------------
- /**
- * Opens a {@link FileOutputStream} for the specified file, checking and
- * creating the parent directory if it does not exist.
- * <p>
- * At the end of the method either the stream will be successfully opened,
- * or an exception will have been thrown.
- * </p>
- * <p>
- * 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.
- * </p>
- *
- * @param file the file to open for output, must not be {@code null}
- * @return a new {@link FileOutputStream} for the specified file
- * @throws IOException if the file object is a directory
- * @throws IOException if the file cannot be written to
- * @throws IOException if a parent directory needs creating but that fails
- * @since 1.3
- */
- public static FileOutputStream openOutputStream(final File file) throws IOException {
- return openOutputStream(file, false);
- }
-
- /**
- * Opens a {@link FileOutputStream} for the specified file, checking and
- * creating the parent directory if it does not exist.
- * <p>
- * At the end of the method either the stream will be successfully opened,
- * or an exception will have been thrown.
- * </p>
- * <p>
- * 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.
- * </p>
- *
- * @param file the file to open for output, must not be {@code null}
- * @param append if {@code true}, then bytes will be added to the
- * end of the file rather than overwriting
- * @return a new {@link FileOutputStream} for the specified file
- * @throws IOException if the file object is a directory
- * @throws IOException if the file cannot be written to
- * @throws IOException if a parent directory needs creating but that fails
- * @since 2.1
- */
- public static FileOutputStream openOutputStream(final File file, final boolean append) throws IOException {
- if (file.exists()) {
- if (file.isDirectory()) {
- throw new IOException("File '" + file + "' exists but is a directory");
- }
- if (file.canWrite() == false) {
- throw new IOException("File '" + file + "' cannot be written to");
- }
- } else {
- final File parent = file.getParentFile();
- if (parent != null) {
- if (!parent.mkdirs() && !parent.isDirectory()) {
- throw new IOException("Directory '" + parent + "' could not be created");
- }
- }
- }
- return new FileOutputStream(file, append);
- }
-
- //-----------------------------------------------------------------------
- /**
* Returns a human-readable version of the file size, where the input represents a specific number of bytes.
* <p>
* 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
@@ -426,367 +225,206 @@ public class FileUtils {
return byteCountToDisplaySize(BigInteger.valueOf(size));
}
- //-----------------------------------------------------------------------
/**
- * Implements the same behaviour as the "touch" utility on Unix. It creates
- * a new file with size 0 or, if the file exists already, it is opened and
- * closed without modifying it, but updating the file date and time.
- * <p>
- * 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.
- * </p>
+ * Checks that the given {@code File} exists and is a directory.
*
- * @param file the File to touch
- * @throws IOException If an I/O problem occurs
+ * @param directory The {@code File} to check.
+ * @throws IllegalArgumentException if the given {@code File} does not exist or is not a directory.
*/
- public static void touch(final File file) throws IOException {
- if (!file.exists()) {
- openOutputStream(file).close();
+ private static void checkDirectory(final File directory) {
+ if (!directory.exists()) {
+ throw new IllegalArgumentException(directory + " does not exist");
}
- final boolean success = file.setLastModified(System.currentTimeMillis());
- if (!success) {
- throw new IOException("Unable to set the last modification time for " + file);
+ if (!directory.isDirectory()) {
+ throw new IllegalArgumentException(directory + " is not a directory");
}
}
- //-----------------------------------------------------------------------
/**
- * Converts a Collection containing java.io.File instanced into array
- * representation. This is to account for the difference between
- * File.listFiles() and FileUtils.listFiles().
+ * Checks that two file lengths are equal.
*
- * @param files a Collection containing java.io.File instances
- * @return an array of java.io.File
+ * @param srcFile Source file.
+ * @param destFile Destination file.
+ * @param srcLen Source file length.
+ * @param dstLen Destination file length
+ * @throws IOException Thrown when the given sizes are not equal.
*/
- public static File[] convertFileCollectionToFileArray(final Collection<File> files) {
- return files.toArray(new File[files.size()]);
+ private static void checkEqualSizes(final File srcFile, final File destFile, final long srcLen, final long dstLen)
+ throws IOException {
+ if (srcLen != dstLen) {
+ throw new IOException("Failed to copy full contents from '" + srcFile + "' to '" + destFile
+ + "' Expected length: " + srcLen + " Actual: " + dstLen);
+ }
}
- //-----------------------------------------------------------------------
/**
- * Finds files within a given directory (and optionally its
- * subdirectories). All files found are filtered by an IOFileFilter.
+ * Checks requirements for file copy.
*
- * @param files the collection of files found.
- * @param directory the directory to search in.
- * @param filter the filter to apply to files and directories.
- * @param includeSubDirectories indicates if will include the subdirectories themselves
- */
- private static void innerListFiles(final Collection<File> files, final File directory,
- final IOFileFilter filter, final boolean includeSubDirectories) {
- final File[] found = directory.listFiles((FileFilter) filter);
-
- if (found != null) {
- for (final File file : found) {
- if (file.isDirectory()) {
- if (includeSubDirectories) {
- files.add(file);
- }
- innerListFiles(files, file, filter, includeSubDirectories);
- } else {
- files.add(file);
- }
- }
+ * @param src the source file
+ * @param dest the destination
+ * @throws FileNotFoundException if the destination does not exist
+ */
+ private static void checkFileRequirements(final File src, final File dest) throws FileNotFoundException {
+ if (src == null) {
+ throw new NullPointerException("Source must not be null");
+ }
+ if (dest == null) {
+ throw new NullPointerException("Destination must not be null");
+ }
+ if (!src.exists()) {
+ throw new FileNotFoundException("Source '" + src + "' does not exist");
}
}
/**
- * Finds files within a given directory (and optionally its
- * subdirectories). All files found are filtered by an IOFileFilter.
- * <p>
- * 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.
- * </p>
- * <p>
- * An example: If you want to search through all directories called
- * "temp" you pass in <code>FileFilterUtils.NameFileFilter("temp")</code>
- * </p>
- * <p>
- * Another common usage of this method is find files in a directory
- * tree but ignoring the directories generated CVS. You can simply pass
- * in <code>FileFilterUtils.makeCVSAware(null)</code>.
- * </p>
+ * Computes the checksum of a file using the specified checksum object.
+ * Multiple files may be checked using one <code>Checksum</code> instance
+ * if desired simply by reusing the same checksum object.
+ * For example:
+ * <pre>
+ * long csum = FileUtils.checksum(file, new CRC32()).getValue();
+ * </pre>
*
- * @param directory the directory to search in
- * @param fileFilter filter to apply when finding files. Must not be {@code null},
- * use {@link TrueFileFilter#INSTANCE} to match all files in selected directories.
- * @param dirFilter optional filter to apply when finding subdirectories.
- * If this parameter is {@code null}, subdirectories will not be included in the
- * search. Use {@link TrueFileFilter#INSTANCE} to match all directories.
- * @return a collection of java.io.File with the matching files
- * @see org.apache.commons.io.filefilter.FileFilterUtils
- * @see org.apache.commons.io.filefilter.NameFileFilter
+ * @param file the file to checksum, must not be {@code null}
+ * @param checksum the checksum object to be used, must not be {@code null}
+ * @return the checksum specified, updated with the content of the file
+ * @throws NullPointerException if the file or checksum is {@code null}
+ * @throws IllegalArgumentException if the file is a directory
+ * @throws IOException if an IO error occurs reading the file
+ * @since 1.3
*/
- public static Collection<File> listFiles(
- final File directory, final IOFileFilter fileFilter, final IOFileFilter dirFilter) {
- return innerListFilesOrDirectories(directory, fileFilter, dirFilter, false);
+ public static Checksum checksum(final File file, final Checksum checksum) throws IOException {
+ if (file.isDirectory()) {
+ throw new IllegalArgumentException("Checksums can't be computed on directories");
+ }
+ try (InputStream in = new CheckedInputStream(new FileInputStream(file), checksum)) {
+ IOUtils.copy(in, new NullOutputStream());
+ }
+ return checksum;
}
+ //-----------------------------------------------------------------------
/**
- * Validates the given arguments.
- * <ul>
- * <li>Throws {@link IllegalArgumentException} if {@code directory} is not a directory</li>
- * <li>Throws {@link NullPointerException} if {@code fileFilter} is null</li>
- * </ul>
+ * Computes the checksum of a file using the CRC32 checksum routine.
+ * The value of the checksum is returned.
*
- * @param directory The File to test
- * @param fileFilter The IOFileFilter to test
+ * @param file the file to checksum, must not be {@code null}
+ * @return the checksum value
+ * @throws NullPointerException if the file or checksum is {@code null}
+ * @throws IllegalArgumentException if the file is a directory
+ * @throws IOException if an IO error occurs reading the file
+ * @since 1.3
*/
- private static void validateListFilesParameters(final File directory, final IOFileFilter fileFilter) {
- if (!directory.isDirectory()) {
- throw new IllegalArgumentException("Parameter 'directory' is not a directory: " + directory);
- }
- if (fileFilter == null) {
- throw new NullPointerException("Parameter 'fileFilter' is null");
- }
+ public static long checksumCRC32(final File file) throws IOException {
+ final CRC32 crc = new CRC32();
+ checksum(file, crc);
+ return crc.getValue();
}
/**
- * Returns a filter that accepts files in addition to the {@link File} objects accepted by the given filter.
+ * Cleans a directory without deleting it.
*
- * @param fileFilter a base filter to add to
- * @return a filter that accepts files
+ * @param directory directory to clean
+ * @throws IOException in case cleaning is unsuccessful
+ * @throws IllegalArgumentException if {@code directory} does not exist or is not a directory
*/
- private static IOFileFilter setUpEffectiveFileFilter(final IOFileFilter fileFilter) {
- return FileFilterUtils.and(fileFilter, FileFilterUtils.notFileFilter(DirectoryFileFilter.INSTANCE));
+ public static void cleanDirectory(final File directory) throws IOException {
+ final File[] files = verifiedListFiles(directory);
+
+ IOException exception = null;
+ for (final File file : files) {
+ try {
+ forceDelete(file);
+ } catch (final IOException ioe) {
+ exception = ioe;
+ }
+ }
+
+ if (null != exception) {
+ throw exception;
+ }
}
/**
- * Returns a filter that accepts directories in addition to the {@link File} objects accepted by the given filter.
+ * Cleans a directory without deleting it.
*
- * @param dirFilter a base filter to add to
- * @return a filter that accepts directories
+ * @param directory directory to clean, must not be {@code null}
+ * @throws NullPointerException if the directory is {@code null}
+ * @throws IOException in case cleaning is unsuccessful
*/
- private static IOFileFilter setUpEffectiveDirFilter(final IOFileFilter dirFilter) {
- return dirFilter == null ? FalseFileFilter.INSTANCE : FileFilterUtils.and(dirFilter,
- DirectoryFileFilter.INSTANCE);
+ private static void cleanDirectoryOnExit(final File directory) throws IOException {
+ final File[] files = verifiedListFiles(directory);
+
+ IOException exception = null;
+ for (final File file : files) {
+ try {
+ forceDeleteOnExit(file);
+ } catch (final IOException ioe) {
+ exception = ioe;
+ }
+ }
+
+ if (null != exception) {
+ throw exception;
+ }
}
+ //-----------------------------------------------------------------------
/**
- * Finds files within a given directory (and optionally its
- * subdirectories). All files found are filtered by an IOFileFilter.
+ * Compares the contents of two files to determine if they are equal or not.
* <p>
- * The resulting collection includes the starting directory and
- * any subdirectories that match the directory filter.
+ * 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.
+ * </p>
+ * <p>
+ * Code origin: Avalon
* </p>
*
- * @param directory the directory to search in
- * @param fileFilter filter to apply when finding files.
- * @param dirFilter optional filter to apply when finding subdirectories.
- * If this parameter is {@code null}, subdirectories will not be included in the
- * search. Use TrueFileFilter.INSTANCE to match all directories.
- * @return a collection of java.io.File with the matching files
- * @see org.apache.commons.io.FileUtils#listFiles
- * @see org.apache.commons.io.filefilter.FileFilterUtils
- * @see org.apache.commons.io.filefilter.NameFileFilter
- * @since 2.2
+ * @param file1 the first file
+ * @param file2 the second file
+ * @return true if the content of the files are equal or they both don't
+ * exist, false otherwise
+ * @throws IOException in case of an I/O error
*/
- public static Collection<File> listFilesAndDirs(
- final File directory, final IOFileFilter fileFilter, final IOFileFilter dirFilter) {
- return innerListFilesOrDirectories(directory, fileFilter, dirFilter, true);
- }
+ public static boolean contentEquals(final File file1, final File file2) throws IOException {
+ final boolean file1Exists = file1.exists();
+ if (file1Exists != file2.exists()) {
+ return false;
+ }
- /**
- * Finds files within a given directory (and optionally its
- * subdirectories). All files found are filtered by an IOFileFilter.
- *
- * @param directory the directory to search in
- * @param fileFilter filter to apply when finding files.
- * @param dirFilter optional filter to apply when finding subdirectories.
- * If this parameter is {@code null}, subdirectories will not be included in the
- * search. Use TrueFileFilter.INSTANCE to match all directories.
- * @param includeSubDirectories indicates if will include the subdirectories themselves
- * @return a collection of java.io.File with the matching files
- * @see org.apache.commons.io.FileUtils#listFiles
- * @see org.apache.commons.io.filefilter.FileFilterUtils
- * @see org.apache.commons.io.filefilter.NameFileFilter
- */
- private static Collection<File> innerListFilesOrDirectories(
- final File directory, final IOFileFilter fileFilter, final IOFileFilter dirFilter,
- final boolean includeSubDirectories) {
- validateListFilesParameters(directory, fileFilter);
+ if (!file1Exists) {
+ // two not existing files are equal
+ return true;
+ }
- final IOFileFilter effFileFilter = setUpEffectiveFileFilter(fileFilter);
- final IOFileFilter effDirFilter = setUpEffectiveDirFilter(dirFilter);
+ if (file1.isDirectory() || file2.isDirectory()) {
+ // don't want to compare directory contents
+ throw new IOException("Can't compare directories, only files");
+ }
- //Find files
- final Collection<File> files = new java.util.LinkedList<>();
- if (includeSubDirectories) {
- files.add(directory);
+ if (file1.length() != file2.length()) {
+ // lengths differ, cannot be equal
+ return false;
+ }
+
+ if (file1.getCanonicalFile().equals(file2.getCanonicalFile())) {
+ // same file
+ return true;
+ }
+
+ try (InputStream input1 = new FileInputStream(file1);
+ InputStream input2 = new FileInputStream(file2)) {
+ return IOUtils.contentEquals(input1, input2);
}
- innerListFiles(files, directory,
- FileFilterUtils.or(effFileFilter, effDirFilter), includeSubDirectories);
- return files;
}
+ //-----------------------------------------------------------------------
/**
- * Allows iteration over the files in given directory (and optionally
- * its subdirectories).
+ * Compares the contents of two files to determine if they are equal or not.
* <p>
- * All files found are filtered by an IOFileFilter. This method is
- * based on {@link #listFiles(File, IOFileFilter, IOFileFilter)},
- * which supports Iterable ('foreach' loop).
- * </p>
- *
- * @param directory the directory to search in
- * @param fileFilter filter to apply when finding files.
- * @param dirFilter optional filter to apply when finding subdirectories.
- * If this parameter is {@code null}, subdirectories will not be included in the
- * search. Use TrueFileFilter.INSTANCE to match all directories.
- * @return an iterator of java.io.File for the matching files
- * @see org.apache.commons.io.filefilter.FileFilterUtils
- * @see org.apache.commons.io.filefilter.NameFileFilter
- * @since 1.2
- */
- public static Iterator<File> iterateFiles(
- final File directory, final IOFileFilter fileFilter, final IOFileFilter dirFilter) {
- return listFiles(directory, fileFilter, dirFilter).iterator();
- }
-
- /**
- * Allows iteration over the files in given directory (and optionally
- * its subdirectories).
- * <p>
- * All files found are filtered by an IOFileFilter. This method is
- * based on {@link #listFilesAndDirs(File, IOFileFilter, IOFileFilter)},
- * which supports Iterable ('foreach' loop).
- * </p>
- * <p>
- * The resulting iterator includes the subdirectories themselves.
- * </p>
- *
- * @param directory the directory to search in
- * @param fileFilter filter to apply when finding files.
- * @param dirFilter optional filter to apply when finding subdirectories.
- * If this parameter is {@code null}, subdirectories will not be included in the
- * search. Use TrueFileFilter.INSTANCE to match all directories.
- * @return an iterator of java.io.File for the matching files
- * @see org.apache.commons.io.filefilter.FileFilterUtils
- * @see org.apache.commons.io.filefilter.NameFileFilter
- * @since 2.2
- */
- public static Iterator<File> iterateFilesAndDirs(final File directory, final IOFileFilter fileFilter,
- final IOFileFilter dirFilter) {
- return listFilesAndDirs(directory, fileFilter, dirFilter).iterator();
- }
-
- //-----------------------------------------------------------------------
- /**
- * Converts an array of file extensions to suffixes for use
- * with IOFileFilters.
- *
- * @param extensions an array of extensions. Format: {"java", "xml"}
- * @return an array of suffixes. Format: {".java", ".xml"}
- */
- private static String[] toSuffixes(final String... extensions) {
- final String[] suffixes = new String[extensions.length];
- for (int i = 0; i < extensions.length; i++) {
- suffixes[i] = "." + extensions[i];
- }
- return suffixes;
- }
-
-
- /**
- * Finds files within a given directory (and optionally its subdirectories)
- * which match an array of extensions.
- *
- * @param directory the directory to search in
- * @param extensions an array of extensions, ex. {"java","xml"}. If this
- * parameter is {@code null}, all files are returned.
- * @param recursive if true all subdirectories are searched as well
- * @return a collection of java.io.File with the matching files
- */
- public static Collection<File> listFiles(
- final File directory, final String[] extensions, final boolean recursive) {
- IOFileFilter filter;
- if (extensions == null) {
- filter = TrueFileFilter.INSTANCE;
- } else {
- final String[] suffixes = toSuffixes(extensions);
- filter = new SuffixFileFilter(suffixes);
- }
- return listFiles(directory, filter,
- recursive ? TrueFileFilter.INSTANCE : FalseFileFilter.INSTANCE);
- }
-
- /**
- * Allows iteration over the files in a given directory (and optionally
- * its subdirectories) which match an array of extensions. This method
- * is based on {@link #listFiles(File, String[], boolean)},
- * which supports Iterable ('foreach' loop).
- *
- * @param directory the directory to search in
- * @param extensions an array of extensions, ex. {"java","xml"}. If this
- * parameter is {@code null}, all files are returned.
- * @param recursive if true all subdirectories are searched as well
- * @return an iterator of java.io.File with the matching files
- * @since 1.2
- */
- public static Iterator<File> iterateFiles(
- final File directory, final String[] extensions, final boolean recursive) {
- return listFiles(directory, extensions, recursive).iterator();
- }
-
- //-----------------------------------------------------------------------
- /**
- * Compares the contents of two files to determine if they are equal or not.
- * <p>
- * 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.
- * </p>
- * <p>
- * Code origin: Avalon
- * </p>
- *
- * @param file1 the first file
- * @param file2 the second file
- * @return true if the content of the files are equal or they both don't
- * exist, false otherwise
- * @throws IOException in case of an I/O error
- */
- public static boolean contentEquals(final File file1, final File file2) throws IOException {
- final boolean file1Exists = file1.exists();
- if (file1Exists != file2.exists()) {
- return false;
- }
-
- if (!file1Exists) {
- // two not existing files are equal
- return true;
- }
-
- if (file1.isDirectory() || file2.isDirectory()) {
- // don't want to compare directory contents
- throw new IOException("Can't compare directories, only files");
- }
-
- if (file1.length() != file2.length()) {
- // lengths differ, cannot be equal
- return false;
- }
-
- if (file1.getCanonicalFile().equals(file2.getCanonicalFile())) {
- // same file
- return true;
- }
-
- try (InputStream input1 = new FileInputStream(file1);
- InputStream input2 = new FileInputStream(file2)) {
- return IOUtils.contentEquals(input1, input2);
- }
- }
-
- //-----------------------------------------------------------------------
- /**
- * Compares the contents of two files to determine if they are equal or not.
- * <p>
- * This method checks to see if the two files point to the same file,
- * before resorting to line-by-line comparison of the contents.
+ * This method checks to see if the two files point to the same file,
+ * before resorting to line-by-line comparison of the contents.
* </p>
*
* @param file1 the first file
@@ -833,203 +471,248 @@ public class FileUtils {
//-----------------------------------------------------------------------
/**
- * Convert from a <code>URL</code> to a <code>File</code>.
- * <p>
- * From version 1.1 this method will decode the URL.
- * Syntax such as <code>file:///my%20docs/file.txt</code> will be
- * correctly decoded to <code>/my docs/file.txt</code>. 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.
- * </p>
+ * Converts a Collection containing java.io.File instanced into array
+ * representation. This is to account for the difference between
+ * File.listFiles() and FileUtils.listFiles().
*
- * @param url the file URL to convert, {@code null} returns {@code null}
- * @return the equivalent <code>File</code> object, or {@code null}
- * if the URL's protocol is not <code>file</code>
+ * @param files a Collection containing java.io.File instances
+ * @return an array of java.io.File
*/
- public static File toFile(final URL url) {
- if (url == null || !"file".equalsIgnoreCase(url.getProtocol())) {
- return null;
- }
- String filename = url.getFile().replace('/', File.separatorChar);
- filename = decodeUrl(filename);
- return new File(filename);
+ public static File[] convertFileCollectionToFileArray(final Collection<File> files) {
+ return files.toArray(new File[files.size()]);
}
/**
- * Decodes the specified URL as per RFC 3986, i.e. transforms
- * percent-encoded octets to characters by decoding with the UTF-8 character
- * set. This function is primarily intended for usage with
- * {@link java.net.URL} which unfortunately does not enforce proper URLs. As
- * such, this method will leniently accept invalid characters or malformed
- * percent-encoded octets and simply pass them literally through to the
- * result string. Except for rare edge cases, this will make unencoded URLs
- * pass through unaltered.
+ * Copies a whole directory to a new location preserving the file dates.
+ * <p>
+ * 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.
+ * </p>
+ * <p>
+ * 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.
+ * </p>
+ * <p>
+ * <strong>Note:</strong> This method tries to preserve the files' last
+ * modified date/times using {@link File#setLastModified(long)}, however
+ * it is not guaranteed that those operations will succeed.
+ * If the modification operation fails, no indication is provided.
+ * </p>
*
- * @param url The URL to decode, may be {@code null}.
- * @return The decoded URL or {@code null} if the input was
- * {@code null}.
+ * @param srcDir an existing directory to copy, must not be {@code null}
+ * @param destDir the new directory, must not be {@code null}
+ *
+ * @throws NullPointerException if source or destination is {@code null}
+ * @throws IOException if source or destination is invalid
+ * @throws IOException if an IO error occurs during copying
+ * @since 1.1
*/
- static String decodeUrl(final String url) {
- String decoded = url;
- if (url != null && url.indexOf('%') >= 0) {
- final int n = url.length();
- final StringBuilder buffer = new StringBuilder();
- final ByteBuffer bytes = ByteBuffer.allocate(n);
- for (int i = 0; i < n; ) {
- if (url.charAt(i) == '%') {
- try {
- do {
- final byte octet = (byte) Integer.parseInt(url.substring(i + 1, i + 3), 16);
- bytes.put(octet);
- i += 3;
- } while (i < n && url.charAt(i) == '%');
- continue;
- } catch (final RuntimeException e) {
- // malformed percent-encoded octet, fall through and
- // append characters literally
- } finally {
- if (bytes.position() > 0) {
- bytes.flip();
- buffer.append(StandardCharsets.UTF_8.decode(bytes).toString());
- bytes.clear();
- }
- }
- }
- buffer.append(url.charAt(i++));
- }
- decoded = buffer.toString();
- }
- return decoded;
+ public static void copyDirectory(final File srcDir, final File destDir) throws IOException {
+ copyDirectory(srcDir, destDir, true);
}
/**
- * Converts each of an array of <code>URL</code> to a <code>File</code>.
+ * Copies a whole directory to a new location.
* <p>
- * Returns an array of the same size as the input.
- * If the input is {@code null}, an empty array is returned.
- * If the input contains {@code null}, the output array contains {@code null} at the same
- * index.
+ * This method copies the contents of the specified source directory
+ * to within the specified destination directory.
* </p>
* <p>
- * This method will decode the URL.
- * Syntax such as <code>file:///my%20docs/file.txt</code> will be
- * correctly decoded to <code>/my docs/file.txt</code>.
- * </p>
+ * 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.
+ * </p>
+ * <p>
+ * <strong>Note:</strong> Setting <code>preserveFileDate</code> to
+ * {@code true} tries to preserve the files' last modified
+ * date/times using {@link File#setLastModified(long)}, however it is
+ * not guaranteed that those operations will succeed.
+ * If the modification operation fails, no indication is provided.
+ * </p>
*
- * @param urls the file URLs to convert, {@code null} returns empty array
- * @return a non-{@code null} array of Files matching the input, with a {@code null} item
- * if there was a {@code null} at that index in the input array
- * @throws IllegalArgumentException if any file is not a URL file
- * @throws IllegalArgumentException if any file is incorrectly encoded
+ * @param srcDir an existing directory to copy, must not be {@code null}
+ * @param destDir the new directory, must not be {@code null}
+ * @param preserveFileDate true if the file date of the copy
+ * should be the same as the original
+ *
+ * @throws NullPointerException if source or destination is {@code null}
+ * @throws IOException if source or destination is invalid
+ * @throws IOException if an IO error occurs during copying
* @since 1.1
*/
- public static File[] toFiles(final URL... urls) {
- if (urls == null || urls.length == 0) {
- return EMPTY_FILE_ARRAY;
- }
- final File[] files = new File[urls.length];
- for (int i = 0; i < urls.length; i++) {
- final URL url = urls[i];
- if (url != null) {
- if (url.getProtocol().equals("file") == false) {
- throw new IllegalArgumentException(
- "URL could not be converted to a File: " + url);
- }
- files[i] = toFile(url);
- }
- }
- return files;
+ public static void copyDirectory(final File srcDir, final File destDir,
+ final boolean preserveFileDate) throws IOException {
+ copyDirectory(srcDir, destDir, null, preserveFileDate);
}
/**
- * Converts each of an array of <code>File</code> to a <code>URL</code>.
+ * Copies a filtered directory to a new location preserving the file dates.
* <p>
- * Returns an array of the same size as the input.
+ * This method copies the contents of the specified source directory
+ * to within the specified destination directory.
* </p>
- *
- * @param files the files to convert, must not be {@code null}
- * @return an array of URLs matching the input
- * @throws IOException if a file cannot be converted
- * @throws NullPointerException if the parameter is null
- */
- public static URL[] toURLs(final File... files) throws IOException {
- final URL[] urls = new URL[files.length];
-
- for (int i = 0; i < urls.length; i++) {
- urls[i] = files[i].toURI().toURL();
- }
-
- return urls;
- }
-
- //-----------------------------------------------------------------------
- /**
- * Copies a file to a directory preserving the file date.
* <p>
- * 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.
+ * If the destination directory did exist, then this method merges
+ * the source with the destination, with the source taking precedence.
* </p>
* <p>
- * <strong>Note:</strong> This method tries to preserve the file's last
+ * <strong>Note:</strong> This method tries to preserve the files' last
* modified date/times using {@link File#setLastModified(long)}, however
- * it is not guaranteed that the operation will succeed.
+ * it is not guaranteed that those operations will succeed.
* If the modification operation fails, no indication is provided.
* </p>
+ * <b>Example: Copy directories only</b>
+ * <pre>
+ * // only copy the directory structure
+ * FileUtils.copyDirectory(srcDir, destDir, DirectoryFileFilter.DIRECTORY);
+ * </pre>
*
- * @param srcFile an existing file to copy, must not be {@code null}
- * @param destDir the directory to place the copy in, must not be {@code null}
+ * <b>Example: Copy directories and txt files</b>
+ * <pre>
+ * // Create a filter for ".txt" files
+ * IOFileFilter txtSuffixFilter = FileFilterUtils.suffixFileFilter(".txt");
+ * IOFileFilter txtFiles = FileFilterUtils.andFileFilter(FileFileFilter.FILE, txtSuffixFilter);
*
- * @throws NullPointerException if source or destination is null
+ * // 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);
+ * </pre>
+ *
+ * @param srcDir an existing directory to copy, must not be {@code null}
+ * @param destDir the new directory, must not be {@code null}
+ * @param filter the filter to apply, null means copy all directories and files
+ * should be the same as the original
+ *
+ * @throws NullPointerException if source or destination is {@code null}
* @throws IOException if source or destination is invalid
* @throws IOException if an IO error occurs during copying
- * @see #copyFile(File, File, boolean)
+ * @since 1.4
*/
- public static void copyFileToDirectory(final File srcFile, final File destDir) throws IOException {
- copyFileToDirectory(srcFile, destDir, true);
+ public static void copyDirectory(final File srcDir, final File destDir,
+ final FileFilter filter) throws IOException {
+ copyDirectory(srcDir, destDir, filter, true);
}
/**
- * Copies a file to a directory optionally preserving the file date.
+ * Copies a filtered directory to a new location.
+ * <p>
+ * This method copies the contents of the specified source directory
+ * to within the specified destination directory.
+ * </p>
* <p>
- * 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.
+ * If the destination directory did exist, then this method merges
+ * the source with the destination, with the source taking precedence.
* </p>
* <p>
* <strong>Note:</strong> Setting <code>preserveFileDate</code> to
- * {@code true} tries to preserve the file's last modified
+ * {@code true} tries to preserve the files' last modified
* date/times using {@link File#setLastModified(long)}, however it is
- * not guaranteed that the operation will succeed.
+ * not guaranteed that those operations will succeed.
* If the modification operation fails, no indication is provided.
* </p>
+ * <b>Example: Copy directories only</b>
+ * <pre>
+ * // only copy the directory structure
+ * FileUtils.copyDirectory(srcDir, destDir, DirectoryFileFilter.DIRECTORY, false);
+ * </pre>
*
- * @param srcFile an existing file to copy, must not be {@code null}
- * @param destDir the directory to place the copy in, must not be {@code null}
+ * <b>Example: Copy directories and txt files</b>
+ * <pre>
+ * // 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);
+ * </pre>
+ *
+ * @param srcDir an existing directory to copy, must not be {@code null}
+ * @param destDir the new directory, must not be {@code null}
+ * @param filter the filter to apply, null means copy all directories and files
* @param preserveFileDate true if the file date of the copy
* should be the same as the original
*
* @throws NullPointerException if source or destination is {@code null}
* @throws IOException if source or destination is invalid
* @throws IOException if an IO error occurs during copying
- * @throws IOException if the output file length is not the same as the input file length after the copy
- * completes
- * @see #copyFile(File, File, boolean)
- * @since 1.3
+ * @since 1.4
*/
- public static void copyFileToDirectory(final File srcFile, final File destDir, final boolean preserveFileDate)
- throws IOException {
+ public static void copyDirectory(final File srcDir, final File destDir,
+ final FileFilter filter, final boolean preserveFileDate) throws IOException {
+ checkFileRequirements(srcDir, destDir);
+ if (!srcDir.isDirectory()) {
+ throw new IOException("Source '" + srcDir + "' exists but is not a directory");
+ }
+ if (srcDir.getCanonicalPath().equals(destDir.getCanonicalPath())) {
+ throw new IOException("Source '" + srcDir + "' and destination '" + destDir + "' are the same");
+ }
+
+ // Cater for destination being directory within the source directory (see IO-141)
+ List<String> exclusionList = null;
+ if (destDir.getCanonicalPath().startsWith(srcDir.getCanonicalPath())) {
+ final File[] srcFiles = filter == null ? srcDir.listFiles() : srcDir.listFiles(filter);
+ if (srcFiles != null && srcFiles.length > 0) {
+ exclusionList = new ArrayList<>(srcFiles.length);
+ for (final File srcFile : srcFiles) {
+ final File copiedFile = new File(destDir, srcFile.getName());
+ exclusionList.add(copiedFile.getCanonicalPath());
+ }
+ }
+ }
+ doCopyDirectory(srcDir, destDir, filter, preserveFileDate, exclusionList);
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Copies a directory to within another directory preserving the file dates.
+ * <p>
+ * This method copies the source directory and all its contents to a
+ * directory of the same name in the specified destination directory.
+ * </p>
+ * <p>
+ * 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.
+ * </p>
+ * <p>
+ * <strong>Note:</strong> This method tries to preserve the files' last
+ * modified date/times using {@link File#setLastModified(long)}, however
+ * it is not guaranteed that those operations will succeed.
+ * If the modification operation fails, no indication is provided.
+ * </p>
+ *
+ * @param srcDir an existing directory to copy, must not be {@code null}
+ * @param destDir the directory to place the copy in, must not be {@code null}
+ *
+ * @throws NullPointerException if source or destination is {@code null}
+ * @throws IllegalArgumentException if {@code srcDir} or {@code destDir} is not a directory
+ * @throws IOException if source or destination is invalid
+ * @throws IOException if an IO error occurs during copying
+ * @since 1.2
+ */
+ public static void copyDirectoryToDirectory(final File srcDir, final File destDir) throws IOException {
+ if (srcDir == null) {
+ throw new NullPointerException("Source must not be null");
+ }
+ if (srcDir.exists() && srcDir.isDirectory() == false) {
+ throw new IllegalArgumentException("Source '" + srcDir + "' is not a directory");
+ }
if (destDir == null) {
throw new NullPointerException("Destination must not be null");
}
if (destDir.exists() && destDir.isDirectory() == false) {
throw new IllegalArgumentException("Destination '" + destDir + "' is not a directory");
}
- final File destFile = new File(destDir, srcFile.getName());
- copyFile(srcFile, destFile, preserveFileDate);
+ copyDirectory(srcDir, new File(destDir, srcDir.getName()), true);
}
/**
@@ -1131,109 +814,103 @@ public class FileUtils {
}
}
+ //-----------------------------------------------------------------------
/**
- * Internal copy file method.
- * This uses the original file length, and throws an IOException
- * if the output file length is different from the current input file length.
- * So it may fail if the file changes size.
- * It may also fail with "IllegalArgumentException: Negative size" if the input file is truncated part way
- * through copying the data and the new file size is less than the current position.
+ * Copies a file to a directory preserving the file date.
+ * <p>
+ * 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.
+ * </p>
+ * <p>
+ * <strong>Note:</strong> This method tries to preserve the file's last
+ * modified date/times using {@link File#setLastModified(long)}, however
+ * it is not guaranteed that the operation will succeed.
+ * If the modification operation fails, no indication is provided.
+ * </p>
*
- * @param srcFile the validated source file, must not be {@code null}
- * @param destFile the validated destination file, must not be {@code null}
- * @param preserveFileDate whether to preserve the file date
- * @throws IOException if an error occurs
- * @throws IOException if the output file length is not the same as the input file length after the
- * copy completes
- * @throws IllegalArgumentException "Negative size" if the file is truncated so that the size is less than the
- * position
+ * @param srcFile an existing file to copy, must not be {@code null}
+ * @param destDir the directory to place the copy in, must not be {@code null}
+ *
+ * @throws NullPointerException if source or destination is null
+ * @throws IOException if source or destination is invalid
+ * @throws IOException if an IO error occurs during copying
+ * @see #copyFile(File, File, boolean)
*/
- private static void doCopyFile(final File srcFile, final File destFile, final boolean preserveFileDate)
- throws IOException {
- if (destFile.exists() && destFile.isDirectory()) {
- throw new IOException("Destination '" + destFile + "' exists but is a directory");
- }
-
- final Path srcPath = srcFile.toPath();
- final Path destPath = destFile.toPath();
- final long newLastModifed = preserveFileDate ? srcFile.lastModified() : destFile.lastModified();
- Files.copy(srcPath, destPath, StandardCopyOption.REPLACE_EXISTING);
-
- // TODO IO-386: Do we still need this check?
- checkEqualSizes(srcFile, destFile, Files.size(srcPath), Files.size(destPath));
- // TODO IO-386: Do we still need this check?
- checkEqualSizes(srcFile, destFile, srcFile.length(), destFile.length());
-
- destFile.setLastModified(newLastModifed);
- }
-
- /**
- * Checks that two file lengths are equal.
- *
- * @param srcFile Source file.
- * @param destFile Destination file.
- * @param srcLen Source file length.
- * @param dstLen Destination file length
- * @throws IOException Thrown when the given sizes are not equal.
- */
- private static void checkEqualSizes(final File srcFile, final File destFile, final long srcLen, final long dstLen)
- throws IOException {
- if (srcLen != dstLen) {
- throw new IOException("Failed to copy full contents from '" + srcFile + "' to '" + destFile
- + "' Expected length: " + srcLen + " Actual: " + dstLen);
- }
+ public static void copyFileToDirectory(final File srcFile, final File destDir) throws IOException {
+ copyFileToDirectory(srcFile, destDir, true);
}
- //-----------------------------------------------------------------------
/**
- * Copies a directory to within another directory preserving the file dates.
- * <p>
- * This method copies the source directory and all its contents to a
- * directory of the same name in the specified destination directory.
- * </p>
+ * Copies a file to a directory optionally preserving the file date.
* <p>
+ * 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 directory did exist, then this method merges
- * the source with the destination, with the source taking precedence.
+ * If the destination file exists, then this method will overwrite it.
* </p>
* <p>
- * <strong>Note:</strong> This method tries to preserve the files' last
- * modified date/times using {@link File#setLastModified(long)}, however
- * it is not guaranteed that those operations will succeed.
+ * <strong>Note:</strong> Setting <code>preserveFileDate</code> to
+ * {@code true} tries to preserve the file's last modified
+ * date/times using {@link File#setLastModified(long)}, however it is
+ * not guaranteed that the operation will succeed.
* If the modification operation fails, no indication is provided.
* </p>
*
- * @param srcDir an existing directory to copy, must not be {@code null}
- * @param destDir the directory to place the copy in, must not be {@code null}
+ * @param srcFile an existing file to copy, must not be {@code null}
+ * @param destDir the directory to place the copy in, must not be {@code null}
+ * @param preserveFileDate true if the file date of the copy
+ * should be the same as the original
*
* @throws NullPointerException if source or destination is {@code null}
- * @throws IllegalArgumentException if {@code srcDir} or {@code destDir} is not a directory
* @throws IOException if source or destination is invalid
* @throws IOException if an IO error occurs during copying
- * @since 1.2
+ * @throws IOException if the output file length is not the same as the input file length after the copy
+ * completes
+ * @see #copyFile(File, File, boolean)
+ * @since 1.3
*/
- public static void copyDirectoryToDirectory(final File srcDir, final File destDir) throws IOException {
- if (srcDir == null) {
- throw new NullPointerException("Source must not be null");
- }
- if (srcDir.exists() && srcDir.isDirectory() == false) {
- throw new IllegalArgumentException("Source '" + srcDir + "' is not a directory");
- }
+ public static void copyFileToDirectory(final File srcFile, final File destDir, final boolean preserveFileDate)
+ throws IOException {
if (destDir == null) {
throw new NullPointerException("Destination must not be null");
}
if (destDir.exists() && destDir.isDirectory() == false) {
throw new IllegalArgumentException("Destination '" + destDir + "' is not a directory");
}
- copyDirectory(srcDir, new File(destDir, srcDir.getName()), true);
+ final File destFile = new File(destDir, srcFile.getName());
+ copyFile(srcFile, destFile, preserveFileDate);
}
/**
- * Copies a whole directory to a new location preserving the file dates.
+ * Copies bytes from an {@link InputStream} <code>source</code> to a file
+ * <code>destination</code>. The directories up to <code>destination</code>
+ * will be created if they don't already exist. <code>destination</code>
+ * will be overwritten if it already exists.
+ * The {@code source} stream is closed.
+ * See {@link #copyToFile(InputStream, File)} for a method that does not close the input stream.
+ *
+ * @param source the <code>InputStream</code> to copy bytes from, must not be {@code null}, will be closed
+ * @param destination the non-directory <code>File</code> to write bytes to
+ * (possibly overwriting), must not be {@code null}
+ * @throws IOException if <code>destination</code> is a directory
+ * @throws IOException if <code>destination</code> cannot be written
+ * @throws IOException if <code>destination</code> needs creating but can't be
+ * @throws IOException if an IO error occurs during copying
+ * @since 2.0
+ */
+ public static void copyInputStreamToFile(final InputStream source, final File destination) throws IOException {
+ try (InputStream in = source) {
+ copyToFile(in, destination);
+ }
+ }
+
+ /**
+ * Copies a file or directory to within another directory preserving the file dates.
* <p>
- * 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.
+ * This method copies the source file or directory, along all its contents, to a
+ * directory of the same name in the specified destination directory.
* </p>
* <p>
* The destination directory is created if it does not exist.
@@ -1247,193 +924,309 @@ public class FileUtils {
* If the modification operation fails, no indication is provided.
* </p>
*
- * @param srcDir an existing directory to copy, must not be {@code null}
- * @param destDir the new directory, must not be {@code null}
+ * @param src an existing file or directory to copy, must not be {@code null}
+ * @param destDir the directory to place the copy in, must not be {@code null}
*
* @throws NullPointerException if source or destination is {@code null}
- * @throws IOException if source or destination is invalid
- * @throws IOException if an IO error occurs during copying
- * @since 1.1
+ * @throws IOException if source or destination is invalid
+ * @throws IOException if an IO error occurs during copying
+ * @see #copyDirectoryToDirectory(File, File)
+ * @see #copyFileToDirectory(File, File)
+ * @since 2.6
*/
- public static void copyDirectory(final File srcDir, final File destDir) throws IOException {
- copyDirectory(srcDir, destDir, true);
+ public static void copyToDirectory(final File src, final File destDir) throws IOException {
+ if (src == null) {
+ throw new NullPointerException("Source must not be null");
+ }
+ if (src.isFile()) {
+ copyFileToDirectory(src, destDir);
+ } else if (src.isDirectory()) {
+ copyDirectoryToDirectory(src, destDir);
+ } else {
+ throw new IOException("The source " + src + " does not exist");
+ }
}
+
/**
- * Copies a whole directory to a new location.
- * <p>
- * This method copies the contents of the specified source directory
- * to within the specified destination directory.
- * </p>
+ * Copies a files to a directory preserving each file's date.
* <p>
+ * 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 directory did exist, then this method merges
- * the source with the destination, with the source taking precedence.
+ * If the destination file exists, then this method will overwrite it.
* </p>
* <p>
- * <strong>Note:</strong> Setting <code>preserveFileDate</code> to
- * {@code true} tries to preserve the files' last modified
- * date/times using {@link File#setLastModified(long)}, however it is
- * not guaranteed that those operations will succeed.
+ * <strong>Note:</strong> This method tries to preserve the file's last
+ * modified date/times using {@link File#setLastModified(long)}, however
+ * it is not guaranteed that the operation will succeed.
* If the modification operation fails, no indication is provided.
* </p>
*
- * @param srcDir an existing directory to copy, must not be {@code null}
- * @param destDir the new directory, must not be {@code null}
- * @param preserveFileDate true if the file date of the copy
- * should be the same as the original
+ * @param srcs a existing files to copy, must not be {@code null}
+ * @param destDir the directory to place the copy in, must not be {@code null}
*
- * @throws NullPointerException if source or destination is {@code null}
- * @throws IOException if source or destination is invalid
- * @throws IOException if an IO error occurs during copying
- * @since 1.1
+ * @throws NullPointerException if source or destination is null
+ * @throws IOException if source or destination is invalid
+ * @throws IOException if an IO error occurs during copying
+ * @see #copyFileToDirectory(File, File)
+ * @since 2.6
*/
- public static void copyDirectory(final File srcDir, final File destDir,
- final boolean preserveFileDate) throws IOException {
- copyDirectory(srcDir, destDir, null, preserveFileDate);
+ public static void copyToDirectory(final Iterable<File> srcs, final File destDir) throws IOException {
+ if (srcs == null) {
+ throw new NullPointerException("Sources must not be null");
+ }
+ for (final File src : srcs) {
+ copyFileToDirectory(src, destDir);
+ }
}
/**
- * Copies a filtered directory to a new location preserving the file dates.
- * <p>
- * This method copies the contents of the specified source directory
- * to within the specified destination directory.
- * </p>
- * <p>
- * 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.
- * </p>
- * <p>
- * <strong>Note:</strong> This method tries to preserve the files' last
- * modified date/times using {@link File#setLastModified(long)}, however
- * it is not guaranteed that those operations will succeed.
- * If the modification operation fails, no indication is provided.
- * </p>
- * <b>Example: Copy directories only</b>
- * <pre>
- * // only copy the directory structure
- * FileUtils.copyDirectory(srcDir, destDir, DirectoryFileFilter.DIRECTORY);
- * </pre>
- *
- * <b>Example: Copy directories and txt files</b>
- * <pre>
- * // 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);
- * </pre>
- *
- * @param srcDir an existing directory to copy, must not be {@code null}
- * @param destDir the new directory, must not be {@code null}
- * @param filter the filter to apply, null means copy all directories and files
- * should be the same as the original
+ * Copies bytes from an {@link InputStream} <code>source</code> to a file
+ * <code>destination</code>. The directories up to <code>destination</code>
+ * will be created if they don't already exist. <code>destination</code>
+ * will be overwritten if it already exists.
+ * The {@code source} stream is left open, e.g. for use with {@link java.util.zip.ZipInputStream ZipInputStream}.
+ * See {@link #copyInputStreamToFile(InputStream, File)} for a method that closes the input stream.
*
- * @throws NullPointerException if source or destination is {@code null}
- * @throws IOException if source or destination is invalid
- * @throws IOException if an IO error occurs during copying
- * @since 1.4
+ * @param source the <code>InputStream</code> to copy bytes from, must not be {@code null}
+ * @param destination the non-directory <code>File</code> to write bytes to
+ * (possibly overwriting), must not be {@code null}
+ * @throws IOException if <code>destination</code> is a directory
+ * @throws IOException if <code>destination</code> cannot be written
+ * @throws IOException if <code>destination</code> needs creating but can't be
+ * @throws IOException if an IO error occurs during copying
+ * @since 2.5
*/
- public static void copyDirectory(final File srcDir, final File destDir,
- final FileFilter filter) throws IOException {
- copyDirectory(srcDir, destDir, filter, true);
+ public static void copyToFile(final InputStream source, final File destination) throws IOException {
+ try (OutputStream out = openOutputStream(destination)) {
+ IOUtils.copy(source, out);
+ }
}
+ //-----------------------------------------------------------------------
/**
- * Copies a filtered directory to a new location.
- * <p>
- * This method copies the contents of the specified source directory
- * to within the specified destination directory.
- * </p>
+ * Copies bytes from the URL <code>source</code> to a file
+ * <code>destination</code>. The directories up to <code>destination</code>
+ * will be created if they don't already exist. <code>destination</code>
+ * will be overwritten if it already exists.
* <p>
- * 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.
+ * Warning: this method does not set a connection or read timeout and thus
+ * might block forever. Use {@link #copyURLToFile(URL, File, int, int)}
+ * with reasonable timeouts to prevent this.
* </p>
- * <p>
- * <strong>Note:</strong> Setting <code>preserveFileDate</code> to
- * {@code true} tries to preserve the files' last modified
- * date/times using {@link File#setLastModified(long)}, however it is
- * not guaranteed that those operations will succeed.
- * If the modification operation fails, no indication is provided.
- * </p>
- * <b>Example: Copy directories only</b>
- * <pre>
- * // only copy the directory structure
- * FileUtils.copyDirectory(srcDir, destDir, DirectoryFileFilter.DIRECTORY, false);
- * </pre>
*
- * <b>Example: Copy directories and txt files</b>
- * <pre>
- * // Create a filter for ".txt" files
- * IOFileFilter txtSuffixFilter = FileFilterUtils.suffixFileFilter(".txt");
- * IOFileFilter txtFiles = FileFilterUtils.andFileFilter(FileFileFilter.FILE, txtSuffixFilter);
+ * @param source the <code>URL</code> to copy bytes from, must not be {@code null}
+ * @param destination the non-directory <code>File</code> to write bytes to
+ * (possibly overwriting), must not be {@code null}
+ * @throws IOException if <code>source</code> URL cannot be opened
+ * @throws IOException if <code>destination</code> is a directory
+ * @throws IOException if <code>destination</code> cannot be written
+ * @throws IOException if <code>destination</code> needs creating but can't be
+ * @throws IOException if an IO error occurs during copying
+ */
+ public static void copyURLToFile(final URL source, final File destination) throws IOException {
+ copyInputStreamToFile(source.openStream(), destination);
+ }
+
+ /**
+ * Copies bytes from the URL <code>source</code> to a file
+ * <code>destination</code>. The directories up to <code>destination</code>
+ * will be created if they don't already exist. <code>destination</code>
+ * will be overwritten if it already exists.
*
- * // Create a filter for either directories or ".txt" files
- * FileFilter filter = FileFilterUtils.orFileFilter(DirectoryFileFilter.DIRECTORY, txtFiles);
+ * @param source the <code>URL</code> to copy bytes from, must not be {@code null}
+ * @param destination the non-directory <code>File</code> to write bytes to
+ * (possibly overwriting), must not be {@code null}
+ * @param connectionTimeout the number of milliseconds until this method
+ * will timeout if no connection could be established to the <code>source</code>
+ * @param readTimeout the number of milliseconds until this method will
+ * timeout if no data could be read from the <code>source</code>
+ * @throws IOException if <code>source</code> URL cannot be opened
+ * @throws IOException if <code>destination</code> is a directory
+ * @throws IOException if <code>destination</code> cannot be written
+ * @throws IOException if <code>destination</code> needs creating but can't be
+ * @throws IOException if an IO error occurs during copying
+ * @since 2.0
+ */
+ public static void copyURLToFile(final URL source, final File destination,
+ final int connectionTimeout, final int readTimeout) throws IOException {
+ final URLConnection connection = source.openConnection();
+ connection.setConnectTimeout(connectionTimeout);
+ connection.setReadTimeout(readTimeout);
+ copyInputStreamToFile(connection.getInputStream(), destination);
+ }
+
+ /**
+ * Decodes the specified URL as per RFC 3986, i.e. transforms
+ * percent-encoded octets to characters by decoding with the UTF-8 character
+ * set. This function is primarily intended for usage with
+ * {@link java.net.URL} which unfortunately does not enforce proper URLs. As
+ * such, this method will leniently accept invalid characters or malformed
+ * percent-encoded octets and simply pass them literally through to the
+ * result string. Except for rare edge cases, this will make unencoded URLs
+ * pass through unaltered.
*
- * // Copy using the filter
- * FileUtils.copyDirectory(srcDir, destDir, filter, false);
- * </pre>
+ * @param url The URL to decode, may be {@code null}.
+ * @return The decoded URL or {@code null} if the input was
+ * {@code null}.
+ */
+ static String decodeUrl(final String url) {
+ String decoded = url;
+ if (url != null && url.indexOf('%') >= 0) {
+ final int n = url.length();
+ final StringBuilder buffer = new StringBuilder();
+ final ByteBuffer bytes = ByteBuffer.allocate(n);
+ for (int i = 0; i < n; ) {
+ if (url.charAt(i) == '%') {
+ try {
+ do {
+ final byte octet = (byte) Integer.parseInt(url.substring(i + 1, i + 3), 16);
+ bytes.put(octet);
+ i += 3;
+ } while (i < n && url.charAt(i) == '%');
+ continue;
+ } catch (final RuntimeException e) {
+ // malformed percent-encoded octet, fall through and
+ // append characters literally
+ } finally {
+ if (bytes.position() > 0) {
+ bytes.flip();
+ buffer.append(StandardCharsets.UTF_8.decode(bytes).toString());
+ bytes.clear();
+ }
+ }
+ }
+ buffer.append(url.charAt(i++));
+ }
+ decoded = buffer.toString();
+ }
+ return decoded;
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Deletes a directory recursively.
*
- * @param srcDir an existing directory to copy, must not be {@code null}
- * @param destDir the new directory, must not be {@code null}
- * @param filter the filter to apply, null means copy all directories and files
- * @param preserveFileDate true if the file date of the copy
- * should be the same as the original
+ * @param directory directory to delete
+ * @throws IOException in case deletion is unsuccessful
+ * @throws IllegalArgumentException if {@code directory} does not exist or is not a directory
+ */
+ public static void deleteDirectory(final File directory) throws IOException {
+ if (!directory.exists()) {
+ return;
+ }
+
+ if (!isSymlink(directory)) {
+ cleanDirectory(directory);
+ }
+
+ if (!directory.delete()) {
+ final String message =
+ "Unable to delete directory " + directory + ".";
+ throw new IOException(message);
+ }
+ }
+
+ /**
+ * Schedules a directory recursively for deletion on JVM exit.
*
- * @throws NullPointerException if source or destination is {@code null}
- * @throws IOException if source or destination is invalid
- * @throws IOException if an IO error occurs during copying
- * @since 1.4
+ * @param directory directory to delete, must not be {@code null}
+ * @throws NullPointerException if the directory is {@code null}
+ * @throws IOException in case deletion is unsuccessful
*/
- public static void copyDirectory(final File srcDir, final File destDir,
- final FileFilter filter, final boolean preserveFileDate) throws IOException {
- checkFileRequirements(srcDir, destDir);
- if (!srcDir.isDirectory()) {
- throw new IOException("Source '" + srcDir + "' exists but is not a directory");
+ private static void deleteDirectoryOnExit(final File directory) throws IOException {
+ if (!directory.exists()) {
+ return;
}
- if (srcDir.getCanonicalPath().equals(destDir.getCanonicalPath())) {
- throw new IOException("Source '" + srcDir + "' and destination '" + destDir + "' are the same");
+
+ directory.deleteOnExit();
+ if (!isSymlink(directory)) {
+ cleanDirectoryOnExit(directory);
}
+ }
- // Cater for destination being directory within the source directory (see IO-141)
- List<String> exclusionList = null;
- if (destDir.getCanonicalPath().startsWith(srcDir.getCanonicalPath())) {
- final File[] srcFiles = filter == null ? srcDir.listFiles() : srcDir.listFiles(filter);
- if (srcFiles != null && srcFiles.length > 0) {
- exclusionList = new ArrayList<>(srcFiles.length);
- for (final File srcFile : srcFiles) {
- final File copiedFile = new File(destDir, srcFile.getName());
- exclusionList.add(copiedFile.getCanonicalPath());
- }
+ /**
+ * Deletes a file, never throwing an exception. If file is a directory, delete it and all sub-directories.
+ * <p>
+ * The difference between File.delete() and this method are:
+ * </p>
+ * <ul>
+ * <li>A directory to be deleted does not have to be empty.</li>
+ * <li>No exceptions are thrown when a file or directory cannot be deleted.</li>
+ * </ul>
+ *
+ * @param file file or directory to delete, can be {@code null}
+ * @return {@code true} if the file or directory was deleted, otherwise
+ * {@code false}
+ *
+ * @since 1.4
+ */
+ public static boolean deleteQuietly(final File file) {
+ if (file == null) {
+ return false;
+ }
+ try {
+ if (file.isDirectory()) {
+ cleanDirectory(file);
}
+ } catch (final Exception ignored) {
+ // ignore
+ }
+
+ try {
+ return file.delete();
+ } catch (final Exception ignored) {
+ return false;
}
- doCopyDirectory(srcDir, destDir, filter, preserveFileDate, exclusionList);
}
/**
- * Checks requirements for file copy.
+ * Determines whether the {@code parent} directory contains the {@code child} element (a file or directory).
+ * <p>
+ * Files are normalized before comparison.
+ * </p>
*
- * @param src the source file
- * @param dest the destination
- * @throws FileNotFoundException if the destination does not exist
+ * Edge cases:
+ * <ul>
+ * <li>A {@code directory} must not be null: if null, throw IllegalArgumentException</li>
+ * <li>A {@code directory} must be a directory: if not a directory, throw IllegalArgumentException</li>
+ * <li>A directory does not contain itself: return false</li>
+ * <li>A null child file is not contained in any parent: return false</li>
+ * </ul>
+ *
+ * @param directory the file to consider as the parent.
+ * @param child the file to consider as the child.
+ * @return true is the candidate leaf is under by the specified composite. False otherwise.
+ * @throws IOException if an IO error occurs while checking the files.
+ * @throws IllegalArgumentException if {@code directory} is null or not a directory.
+ * @see FilenameUtils#directoryContains(String, String)
+ * @since 2.2
*/
- private static void checkFileRequirements(final File src, final File dest) throws FileNotFoundException {
- if (src == null) {
- throw new NullPointerException("Source must not be null");
+ public static boolean directoryContains(final File directory, final File child) throws IOException {
+
+ // Fail fast against NullPointerException
+ if (directory == null) {
+ throw new IllegalArgumentException("Directory must not be null");
}
- if (dest == null) {
- throw new NullPointerException("Destination must not be null");
+
+ if (!directory.isDirectory()) {
+ throw new IllegalArgumentException("Not a directory: " + directory);
}
- if (!src.exists()) {
- throw new FileNotFoundException("Source '" + src + "' does not exist");
+
+ if (child == null) {
+ return false;
+ }
+
+ if (!directory.exists() || !child.exists()) {
+ return false;
}
+
+ // Canonicalize paths (normalizes relative paths)
+ final String canonicalParent = directory.getCanonicalPath();
+ final String canonicalChild = child.getCanonicalPath();
+
+ return FilenameUtils.directoryContains(canonicalParent, canonicalChild);
}
/**
@@ -1484,1682 +1277,1889 @@ public class FileUtils {
}
}
- //-----------------------------------------------------------------------
/**
- * Copies bytes from the URL <code>source</code> to a file
- * <code>destination</code>. The directories up to <code>destination</code>
- * will be created if they don't already exist. <code>destination</code>
- * will be overwritten if it already exists.
+ * Internal copy file method.
+ * This uses the original file length, and throws an IOException
+ * if the output file length is different from the current input file length.
+ * So it may fail if the file changes size.
+ * It may also fail with "IllegalArgumentException: Negative size" if the input file is truncated part way
+ * through copying the data and the new file size is less than the current position.
+ *
+ * @param srcFile the validated source file, must not be {@code null}
+ * @param destFile the validated destination file, must not be {@code null}
+ * @param preserveFileDate whether to preserve the file date
+ * @throws IOException if an error occurs
+ * @throws IOException if the output file length is not the same as the input file length after the
+ * copy completes
+ * @throws IllegalArgumentException "Negative size" if the file is truncated so that the size is less than the
+ * position
+ */
+ private static void doCopyFile(final File srcFile, final File destFile, final boolean preserveFileDate)
+ throws IOException {
+ if (destFile.exists() && destFile.isDirectory()) {
+ throw new IOException("Destination '" + destFile + "' exists but is a directory");
+ }
+
+ final Path srcPath = srcFile.toPath();
+ final Path destPath = destFile.toPath();
+ final long newLastModifed = preserveFileDate ? srcFile.lastModified() : destFile.lastModified();
+ Files.copy(srcPath, destPath, StandardCopyOption.REPLACE_EXISTING);
+
+ // TODO IO-386: Do we still need this check?
+ checkEqualSizes(srcFile, destFile, Files.size(srcPath), Files.size(destPath));
+ // TODO IO-386: Do we still need this check?
+ checkEqualSizes(srcFile, destFile, srcFile.length(), destFile.length());
+
+ destFile.setLastModified(newLastModifed);
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Deletes a file. If file is a directory, delete it and all sub-directories.
* <p>
- * Warning: this method does not set a connection or read timeout and thus
- * might block forever. Use {@link #copyURLToFile(URL, File, int, int)}
- * with reasonable timeouts to prevent this.
+ * The difference between File.delete() and this method are:
* </p>
+ * <ul>
+ * <li>A directory to be deleted does not have to be empty.</li>
+ * <li>You get exceptions when a file or directory cannot be deleted.
+ * (java.io.File methods returns a boolean)</li>
+ * </ul>
*
- * @param source the <code>URL</code> to copy bytes from, must not be {@code null}
- * @param destination the non-directory <code>File</code> to write bytes to
- * (possibly overwriting), must not be {@code null}
- * @throws IOException if <code>source</code> URL cannot be opened
- * @throws IOException if <code>destination</code> is a directory
- * @throws IOException if <code>destination</code> cannot be written
- * @throws IOException if <code>destination</code> needs creating but can't be
- * @throws IOException if an IO error occurs during copying
+ * @param file file or directory to delete, must not be {@code null}
+ * @throws NullPointerException if the directory is {@code null}
+ * @throws FileNotFoundException if the file was not found
+ * @throws IOException in case deletion is unsuccessful
*/
- public static void copyURLToFile(final URL source, final File destination) throws IOException {
- copyInputStreamToFile(source.openStream(), destination);
+ public static void forceDelete(final File file) throws IOException {
+ if (file.isDirectory()) {
+ deleteDirectory(file);
+ } else {
+ final boolean filePresent = file.exists();
+ if (!file.delete()) {
+ if (!filePresent) {
+ throw new FileNotFoundException("File does not exist: " + file);
+ }
+ final String message =
+ "Unable to delete file: " + file;
+ throw new IOException(message);
+ }
+ }
}
/**
- * Copies bytes from the URL <code>source</code> to a file
- * <code>destination</code>. The directories up to <code>destination</code>
- * will be created if they don't already exist. <code>destination</code>
- * will be overwritten if it already exists.
+ * Schedules a file to be deleted when JVM exits.
+ * If file is directory delete it and all sub-directories.
+ *
+ * @param file file or directory to delete, must not be {@code null}
+ * @throws NullPointerException if the file is {@code null}
+ * @throws IOException in case deletion is unsuccessful
+ */
+ public static void forceDeleteOnExit(final File file) throws IOException {
+ if (file.isDirectory()) {
+ deleteDirectoryOnExit(file);
+ } else {
+ file.deleteOnExit();
+ }
+ }
+
+ /**
+ * Makes a directory, including any necessary but nonexistent parent
+ * directories. If a file already exists with specified name but it is
+ * not a directory then an IOException is thrown.
+ * If the directory cannot be created (or the file already exists but is not a directory)
+ * then an IOException is thrown.
+ *
+ * @param directory directory to create, must not be {@code null}
+ * @throws NullPointerException if the directory is {@code null}
+ * @throws IOException if the directory cannot be created or the file already exists but is not a directory
+ */
+ public static void forceMkdir(final File directory) throws IOException {
+ if (directory.exists()) {
+ if (!directory.isDirectory()) {
+ final String message =
+ "File "
+ + directory
+ + " exists and is "
+ + "not a directory. Unable to create directory.";
+ throw new IOException(message);
+ }
+ } else {
+ if (!directory.mkdirs()) {
+ // Double-check that some other thread or process hasn't made
+ // the directory in the background
+ if (!directory.isDirectory()) {
+ final String message =
+ "Unable to create directory " + directory;
+ throw new IOException(message);
+ }
+ }
+ }
+ }
+
+ /**
+ * Makes any necessary but nonexistent parent directories for a given File. If the parent directory cannot be
+ * created then an IOException is thrown.
+ *
+ * @param file file with parent to create, must not be {@code null}
+ * @throws NullPointerException if the file is {@code null}
+ * @throws IOException if the parent directory cannot be created
+ * @since 2.5
+ */
+ public static void forceMkdirParent(final File file) throws IOException {
+ final File parent = file.getParentFile();
+ if (parent == null) {
+ return;
+ }
+ forceMkdir(parent);
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Construct a file from the set of name elements.
+ *
+ * @param directory the parent directory
+ * @param names the name elements
+ * @return the file
+ * @since 2.1
+ */
+ public static File getFile(final File directory, final String... names) {
+ if (directory == null) {
+ throw new NullPointerException("directory must not be null");
+ }
+ if (names == null) {
+ throw new NullPointerException("names must not be null");
+ }
+ File file = directory;
+ for (final String name : names) {
+ file = new File(file, name);
+ }
+ return file;
+ }
+
+ /**
+ * Construct a file from the set of name elements.
+ *
+ * @param names the name elements
+ * @return the file
+ * @since 2.1
+ */
+ public static File getFile(final String... names) {
+ if (names == null) {
+ throw new NullPointerException("names must not be null");
+ }
+ File file = null;
+ for (final String name : names) {
+ if (file == null) {
+ file = new File(name);
+ } else {
+ file = new File(file, name);
+ }
+ }
+ return file;
+ }
+
+ /**
+ * Returns a {@link File} representing the system temporary directory.
+ *
+ * @return the system temporary directory.
*
- * @param source the <code>URL</code> to copy bytes from, must not be {@code null}
- * @param destination the non-directory <code>File</code> to write bytes to
- * (possibly overwriting), must not be {@code null}
- * @param connectionTimeout the number of milliseconds until this method
- * will timeout if no connection could be established to the <code>source</code>
- * @param readTimeout the number of milliseconds until this method will
- * timeout if no data could be read from the <code>source</code>
- * @throws IOException if <code>source</code> URL cannot be opened
- * @throws IOException if <code>destination</code> is a directory
- * @throws IOException if <code>destination</code> cannot be written
- * @throws IOException if <code>destination</code> needs creating but can't be
- * @throws IOException if an IO error occurs during copying
* @since 2.0
*/
- public static void copyURLToFile(final URL source, final File destination,
- final int connectionTimeout, final int readTimeout) throws IOException {
- final URLConnection connection = source.openConnection();
- connection.setConnectTimeout(connectionTimeout);
- connection.setReadTimeout(readTimeout);
- copyInputStreamToFile(connection.getInputStream(), destination);
+ public static File getTempDirectory() {
+ return new File(getTempDirectoryPath());
}
/**
- * Copies bytes from an {@link InputStream} <code>source</code> to a file
- * <code>destination</code>. The directories up to <code>destination</code>
- * will be created if they don't already exist. <code>destination</code>
- * will be overwritten if it already exists.
- * The {@code source} stream is closed.
- * See {@link #copyToFile(InputStream, File)} for a method that does not close the input stream.
+ * Returns the path to the system temporary directory.
+ *
+ * @return the path to the system temporary directory.
*
- * @param source the <code>InputStream</code> to copy bytes from, must not be {@code null}, will be closed
- * @param destination the non-directory <code>File</code> to write bytes to
- * (possibly overwriting), must not be {@code null}
- * @throws IOException if <code>destination</code> is a directory
- * @throws IOException if <code>destination</code> cannot be written
- * @throws IOException if <code>destination</code> needs creating but can't be
- * @throws IOException if an IO error occurs during copying
* @since 2.0
*/
- public static void copyInputStreamToFile(final InputStream source, final File destination) throws IOException {
- try (InputStream in = source) {
- copyToFile(in, destination);
+ public static String getTempDirectoryPath() {
+ return System.getProperty("java.io.tmpdir");
+ }
+
+ /**
+ * Returns a {@link File} representing the user's home directory.
+ *
+ * @return the user's home directory.
+ *
+ * @since 2.0
+ */
+ public static File getUserDirectory() {
+ return new File(getUserDirectoryPath());
+ }
+
+ /**
+ * Returns the path to the user's home directory.
+ *
+ * @return the path to the user's home directory.
+ *
+ * @since 2.0
+ */
+ public static String getUserDirectoryPath() {
+ return System.getProperty("user.home");
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Finds files within a given directory (and optionally its
+ * subdirectories). All files found are filtered by an IOFileFilter.
+ *
+ * @param files the collection of files found.
+ * @param directory the directory to search in.
+ * @param filter the filter to apply to files and directories.
+ * @param includeSubDirectories indicates if will include the subdirectories themselves
+ */
+ private static void innerListFiles(final Collection<File> files, final File directory,
+ final IOFileFilter filter, final boolean includeSubDirectories) {
+ final File[] found = directory.listFiles((FileFilter) filter);
+
+ if (found != null) {
+ for (final File file : found) {
+ if (file.isDirectory()) {
+ if (includeSubDirectories) {
+ files.add(file);
+ }
+ innerListFiles(files, file, filter, includeSubDirectories);
+ } else {
+ files.add(file);
+ }
+ }
}
}
/**
- * Copies bytes from an {@link InputStream} <code>source</code> to a file
- * <code>destination</code>. The directories up to <code>destination</code>
- * will be created if they don't already exist. <code>destination</code>
- * will be overwritten if it already exists.
- * The {@code source} stream is left open, e.g. for use with {@link java.util.zip.ZipInputStream ZipInputStream}.
- * See {@link #copyInputStreamToFile(InputStream, File)} for a method that closes the input stream.
+ * Finds files within a given directory (and optionally its
+ * subdirectories). All files found are filtered by an IOFileFilter.
*
- * @param source the <code>InputStream</code> to copy bytes from, must not be {@code null}
- * @param destination the non-directory <code>File</code> to write bytes to
- * (possibly overwriting), must not be {@code null}
- * @throws IOException if <code>destination</code> is a directory
- * @throws IOException if <code>destination</code> cannot be written
- * @throws IOException if <code>destination</code> needs creating but can't be
- * @throws IOException if an IO error occurs during copying
- * @since 2.5
+ * @param directory the directory to search in
+ * @param fileFilter filter to apply when finding files.
+ * @param dirFilter optional filter to apply when finding subdirectories.
+ * If this parameter is {@code null}, subdirectories will not be included in the
+ * search. Use TrueFileFilter.INSTANCE to match all directories.
+ * @param includeSubDirectories indicates if will include the subdirectories themselves
+ * @return a collection of java.io.File with the matching files
+ * @see org.apache.commons.io.FileUtils#listFiles
+ * @see org.apache.commons.io.filefilter.FileFilterUtils
+ * @see org.apache.commons.io.filefilter.NameFileFilter
*/
- public static void copyToFile(final InputStream source, final File destination) throws IOException {
- try (OutputStream out = openOutputStream(destination)) {
- IOUtils.copy(source, out);
+ private static Collection<File> innerListFilesOrDirectories(
+ final File directory, final IOFileFilter fileFilter, final IOFileFilter dirFilter,
+ final boolean includeSubDirectories) {
+ validateListFilesParameters(directory, fileFilter);
+
+ final IOFileFilter effFileFilter = setUpEffectiveFileFilter(fileFilter);
+ final IOFileFilter effDirFilter = setUpEffectiveDirFilter(dirFilter);
+
+ //Find files
+ final Collection<File> files = new java.util.LinkedList<>();
+ if (includeSubDirectories) {
+ files.add(directory);
+ }
+ innerListFiles(files, directory,
+ FileFilterUtils.or(effFileFilter, effDirFilter), includeSubDirectories);
+ return files;
+ }
+
+ /**
+ * Tests if the specified <code>File</code> is newer than the specified
+ * <code>Date</code>.
+ *
+ * @param file the <code>File</code> of which the modification date
+ * must be compared, must not be {@code null}
+ * @param date the date reference, must not be {@code null}
+ * @return true if the <code>File</code> exists and has been modified
+ * after the given <code>Date</code>.
+ * @throws IllegalArgumentException if the file is {@code null}
+ * @throws IllegalArgumentException if the date is {@code null}
+ */
+ public static boolean isFileNewer(final File file, final Date date) {
+ if (date == null) {
+ throw new IllegalArgumentException("No specified date");
+ }
+ return isFileNewer(file, date.getTime());
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Tests if the specified <code>File</code> is newer than the reference
+ * <code>File</code>.
+ *
+ * @param file the <code>File</code> of which the modification date must
+ * be compared, must not be {@code null}
+ * @param reference the <code>File</code> of which the modification date
+ * is used, must not be {@code null}
+ * @return true if the <code>File</code> exists and has been modified more
+ * recently than the reference <code>File</code>
+ * @throws IllegalArgumentException if the file is {@code null}
+ * @throws IllegalArgumentException if the reference file is {@code null} or doesn't exist
+ */
+ public static boolean isFileNewer(final File file, final File reference) {
+ if (reference == null) {
+ throw new IllegalArgumentException("No specified reference file");
+ }
+ if (!reference.exists()) {
+ throw new IllegalArgumentException("The reference file '"
+ + reference + "' doesn't exist");
}
+ return isFileNewer(file, reference.lastModified());
}
/**
- * Copies a file or directory to within another directory preserving the file dates.
- * <p>
- * This method copies the source file or directory, along all its contents, to a
- * directory of the same name in the specified destination directory.
- * </p>
- * <p>
- * 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.
- * </p>
- * <p>
- * <strong>Note:</strong> This method tries to preserve the files' last
- * modified date/times using {@link File#setLastModified(long)}, however
- * it is not guaranteed that those operations will succeed.
- * If the modification operation fails, no indication is provided.
- * </p>
- *
- * @param src an existing file or directory to copy, must not be {@code null}
- * @param destDir the directory to place the copy in, must not be {@code null}
+ * Tests if the specified <code>File</code> is newer than the specified
+ * time reference.
*
- * @throws NullPointerException if source or destination is {@code null}
- * @throws IOException if source or destination is invalid
- * @throws IOException if an IO error occurs during copying
- * @see #copyDirectoryToDirectory(File, File)
- * @see #copyFileToDirectory(File, File)
- * @since 2.6
+ * @param file the <code>File</code> of which the modification date must
+ * be compared, must not be {@code null}
+ * @param timeMillis the time reference measured in milliseconds since the
+ * epoch (00:00:00 GMT, January 1, 1970)
+ * @return true if the <code>File</code> exists and has been modified after
+ * the given time reference.
+ * @throws IllegalArgumentException if the file is {@code null}
*/
- public static void copyToDirectory(final File src, final File destDir) throws IOException {
- if (src == null) {
- throw new NullPointerException("Source must not be null");
+ public static boolean isFileNewer(final File file, final long timeMillis) {
+ if (file == null) {
+ throw new IllegalArgumentException("No specified file");
}
- if (src.isFile()) {
- copyFileToDirectory(src, destDir);
- } else if (src.isDirectory()) {
- copyDirectoryToDirectory(src, destDir);
- } else {
- throw new IOException("The source " + src + " does not exist");
+ if (!file.exists()) {
+ return false;
}
+ return file.lastModified() > timeMillis;
}
/**
- * Copies a files to a directory preserving each file's date.
- * <p>
- * 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.
- * </p>
- * <p>
- * <strong>Note:</strong> This method tries to preserve the file's last
- * modified date/times using {@link File#setLastModified(long)}, however
- * it is not guaranteed that the operation will succeed.
- * If the modification operation fails, no indication is provided.
- * </p>
- *
- * @param srcs a existing files to copy, must not be {@code null}
- * @param destDir the directory to place the copy in, must not be {@code null}
+ * Tests if the specified <code>File</code> is older than the specified
+ * <code>Date</code>.
*
- * @throws NullPointerException if source or destination is null
- * @throws IOException if source or destination is invalid
- * @throws IOException if an IO error occurs during copying
- * @see #copyFileToDirectory(File, File)
- * @since 2.6
+ * @param file the <code>File</code> of which the modification date
+ * must be compared, must not be {@code null}
+ * @param date the date reference, must not be {@code null}
+ * @return true if the <code>File</code> exists and has been modified
+ * before the given <code>Date</code>.
+ * @throws IllegalArgumentException if the file is {@code null}
+ * @throws IllegalArgumentException if the date is {@code null}
*/
- public static void copyToDirectory(final Iterable<File> srcs, final File destDir) throws IOException {
- if (srcs == null) {
- throw new NullPointerException("Sources must not be null");
- }
- for (final File src : srcs) {
- copyFileToDirectory(src, destDir);
+ public static boolean isFileOlder(final File file, final Date date) {
+ if (date == null) {
+ throw new IllegalArgumentException("No specified date");
}
+ return isFileOlder(file, date.getTime());
}
//-----------------------------------------------------------------------
/**
- * Deletes a directory recursively.
+ * Tests if the specified <code>File</code> is older than the reference
+ * <code>File</code>.
*
- * @param directory directory to delete
- * @throws IOException in case deletion is unsuccessful
- * @throws IllegalArgumentException if {@code directory} does not exist or is not a directory
+ * @param file the <code>File</code> of which the modification date must
+ * be compared, must not be {@code null}
+ * @param reference the <code>File</code> of which the modification date
+ * is used, must not be {@code null}
+ * @return true if the <code>File</code> exists and has been modified before
+ * the reference <code>File</code>
+ * @throws IllegalArgumentException if the file is {@code null}
+ * @throws IllegalArgumentException if the reference file is {@code null} or doesn't exist
*/
- public static void deleteDirectory(final File directory) throws IOException {
- if (!directory.exists()) {
- return;
- }
-
- if (!isSymlink(directory)) {
- cleanDirectory(directory);
+ public static boolean isFileOlder(final File file, final File reference) {
+ if (reference == null) {
+ throw new IllegalArgumentException("No specified reference file");
}
-
- if (!directory.delete()) {
- final String message =
- "Unable to delete directory " + directory + ".";
- throw new IOException(message);
+ if (!reference.exists()) {
+ throw new IllegalArgumentException("The reference file '"
+ + reference + "' doesn't exist");
}
+ return isFileOlder(file, reference.lastModified());
}
/**
- * Deletes a file, never throwing an exception. If file is a directory, delete it and all sub-directories.
- * <p>
- * The difference between File.delete() and this method are:
- * </p>
- * <ul>
- * <li>A directory to be deleted does not have to be empty.</li>
- * <li>No exceptions are thrown when a file or directory cannot be deleted.</li>
- * </ul>
- *
- * @param file file or directory to delete, can be {@code null}
- * @return {@code true} if the file or directory was deleted, otherwise
- * {@code false}
+ * Tests if the specified <code>File</code> is older than the specified
+ * time reference.
*
- * @since 1.4
+ * @param file the <code>File</code> of which the modification date must
+ * be compared, must not be {@code null}
+ * @param timeMillis the time reference measured in milliseconds since the
+ * epoch (00:00:00 GMT, January 1, 1970)
+ * @return true if the <code>File</code> exists and has been modified before
+ * the given time reference.
+ * @throws IllegalArgumentException if the file is {@code null}
*/
- public static boolean deleteQuietly(final File file) {
+ public static boolean isFileOlder(final File file, final long timeMillis) {
if (file == null) {
- return false;
- }
- try {
- if (file.isDirectory()) {
- cleanDirectory(file);
- }
- } catch (final Exception ignored) {
- // ignore
+ throw new IllegalArgumentException("No specified file");
}
-
- try {
- return file.delete();
- } catch (final Exception ignored) {
+ if (!file.exists()) {
return false;
}
+ return file.lastModified() < timeMillis;
}
/**
- * Determines whether the {@code parent} directory contains the {@code child} element (a file or directory).
+ * Determines whether the specified file is a Symbolic Link rather than an actual file.
* <p>
- * Files are normalized before comparison.
+ * Will not return true if there is a Symbolic Link anywhere in the path,
+ * only if the specific file is.
+ * </p>
+ * <p>
+ * When using jdk1.7, this method delegates to {@code boolean java.nio.file.Files.isSymbolicLink(Path path)}
* </p>
*
- * Edge cases:
- * <ul>
- * <li>A {@code directory} must not be null: if null, throw IllegalArgumentException</li>
- * <li>A {@code directory} must be a directory: if not a directory, throw IllegalArgumentException</li>
- * <li>A directory does not contain itself: return false</li>
- * <li>A null child file is not contained in any parent: return false</li>
- * </ul>
+ * <p>
+ * <b>Note:</b> the current implementation always returns {@code false} if running on
+ * jkd1.6 and the system is detected as Windows using {@link FilenameUtils#isSystemWindows()}
+ * </p>
+ * <p>
+ * For code that runs on Java 1.7 or later, use the following method instead:
+ * </p>
*
- * @param directory the file to consider as the parent.
- * @param child the file to consider as the child.
- * @return true is the candidate leaf is under by the specified composite. False otherwise.
- * @throws IOException if an IO error occurs while checking the files.
- * @throws IllegalArgumentException if {@code directory} is null or not a directory.
- * @see FilenameUtils#directoryContains(String, String)
- * @since 2.2
+ * {@code boolean java.nio.file.Files.isSymbolicLink(Path path)}
+ * @param file the file to check
+ * @return true if the file is a Symbolic Link
+ * @since 2.0
*/
- public static boolean directoryContains(final File directory, final File child) throws IOException {
-
- // Fail fast against NullPointerException
- if (directory == null) {
- throw new IllegalArgumentException("Directory must not be null");
- }
-
- if (!directory.isDirectory()) {
- throw new IllegalArgumentException("Not a directory: " + directory);
- }
-
- if (child == null) {
- return false;
- }
-
- if (!directory.exists() || !child.exists()) {
- return false;
+ public static boolean isSymlink(final File file) {
+ if (file == null) {
+ throw new NullPointerException("File must not be null");
}
+ return Files.isSymbolicLink(file.toPath());
+ }
- // Canonicalize paths (normalizes relative paths)
- final String canonicalParent = directory.getCanonicalPath();
- final String canonicalChild = child.getCanonicalPath();
+ /**
+ * Allows iteration over the files in given directory (and optionally
+ * its subdirectories).
+ * <p>
+ * All files found are filtered by an IOFileFilter. This method is
+ * based on {@link #listFiles(File, IOFileFilter, IOFileFilter)},
+ * which supports Iterable ('foreach' loop).
+ * </p>
+ *
+ * @param directory the directory to search in
+ * @param fileFilter filter to apply when finding files.
+ * @param dirFilter optional filter to apply when finding subdirectories.
+ * If this parameter is {@code null}, subdirectories will not be included in the
+ * search. Use TrueFileFilter.INSTANCE to match all directories.
+ * @return an iterator of java.io.File for the matching files
+ * @see org.apache.commons.io.filefilter.FileFilterUtils
+ * @see org.apache.commons.io.filefilter.NameFileFilter
+ * @since 1.2
+ */
+ public static Iterator<File> iterateFiles(
+ final File directory, final IOFileFilter fileFilter, final IOFileFilter dirFilter) {
+ return listFiles(directory, fileFilter, dirFilter).iterator();
+ }
- return FilenameUtils.directoryContains(canonicalParent, canonicalChild);
+ /**
+ * Allows iteration over the files in a given directory (and optionally
+ * its subdirectories) which match an array of extensions. This method
+ * is based on {@link #listFiles(File, String[], boolean)},
+ * which supports Iterable ('foreach' loop).
+ *
+ * @param directory the directory to search in
+ * @param extensions an array of extensions, ex. {"java","xml"}. If this
+ * parameter is {@code null}, all files are returned.
+ * @param recursive if true all subdirectories are searched as well
+ * @return an iterator of java.io.File with the matching files
+ * @since 1.2
+ */
+ public static Iterator<File> iterateFiles(
+ final File directory, final String[] extensions, final boolean recursive) {
+ return listFiles(directory, extensions, recursive).iterator();
}
/**
- * Cleans a directory without deleting it.
+ * Allows iteration over the files in given directory (and optionally
+ * its subdirectories).
+ * <p>
+ * All files found are filtered by an IOFileFilter. This method is
+ * based on {@link #listFilesAndDirs(File, IOFileFilter, IOFileFilter)},
+ * which supports Iterable ('foreach' loop).
+ * </p>
+ * <p>
+ * The resulting iterator includes the subdirectories themselves.
+ * </p>
*
- * @param directory directory to clean
- * @throws IOException in case cleaning is unsuccessful
- * @throws IllegalArgumentException if {@code directory} does not exist or is not a directory
+ * @param directory the directory to search in
+ * @param fileFilter filter to apply when finding files.
+ * @param dirFilter optional filter to apply when finding subdirectories.
+ * If this parameter is {@code null}, subdirectories will not be included in the
+ * search. Use TrueFileFilter.INSTANCE to match all directories.
+ * @return an iterator of java.io.File for the matching files
+ * @see org.apache.commons.io.filefilter.FileFilterUtils
+ * @see org.apache.commons.io.filefilter.NameFileFilter
+ * @since 2.2
*/
- public static void cleanDirectory(final File directory) throws IOException {
- final File[] files = verifiedListFiles(directory);
-
- IOException exception = null;
- for (final File file : files) {
- try {
- forceDelete(file);
- } catch (final IOException ioe) {
- exception = ioe;
- }
- }
-
- if (null != exception) {
- throw exception;
- }
+ public static Iterator<File> iterateFilesAndDirs(final File directory, final IOFileFilter fileFilter,
+ final IOFileFilter dirFilter) {
+ return listFilesAndDirs(directory, fileFilter, dirFilter).iterator();
}
/**
- * Lists files in a directory, asserting that the supplied directory satisfies exists and is a directory.
+ * Returns an Iterator for the lines in a <code>File</code> using the default encoding for the VM.
*
- * @param directory The directory to list
- * @return The files in the directory, never null.
- * @throws IOException if an I/O error occurs
+ * @param file the file to open for input, must not be {@code null}
+ * @return an Iterator of the lines in the file, never {@code null}
+ * @throws IOException in case of an I/O error (file closed)
+ * @see #lineIterator(File, String)
+ * @since 1.3
*/
- private static File[] verifiedListFiles(final File directory) throws IOException {
- if (!directory.exists()) {
- final String message = directory + " does not exist";
- throw new IllegalArgumentException(message);
- }
-
- if (!directory.isDirectory()) {
- final String message = directory + " is not a directory";
- throw new IllegalArgumentException(message);
- }
-
- final File[] files = directory.listFiles();
- if (files == null) { // null if security restricted
- throw new IOException("Failed to list contents of " + directory);
- }
- return files;
+ public static LineIterator lineIterator(final File file) throws IOException {
+ return lineIterator(file, null);
}
- //-----------------------------------------------------------------------
/**
- * Waits for NFS to propagate a file creation, imposing a timeout.
+ * Returns an Iterator for the lines in a <code>File</code>.
* <p>
- * This method repeatedly tests {@link File#exists()} until it returns
- * true up to the maximum time specified in seconds.
+ * This method opens an <code>InputStream</code> for the file.
+ * When you have finished with the iterator you should close the stream
+ * to free internal resources. This can be done by calling the
+ * {@link LineIterator#close()} or
+ * {@link LineIterator#closeQuietly(LineIterator)} method.
+ * </p>
+ * <p>
+ * The recommended usage pattern is:
+ * </p>
+ * <pre>
+ * LineIterator it = FileUtils.lineIterator(file, "UTF-8");
+ * try {
+ * while (it.hasNext()) {
+ * String line = it.nextLine();
+ * /// do something with line
+ * }
+ * } finally {
+ * LineIterator.closeQuietly(iterator);
+ * }
+ * </pre>
+ * <p>
+ * If an exception occurs during the creation of the iterator, the
+ * underlying stream is closed.
* </p>
*
- * @param file the file to check, must not be {@code null}
- * @param seconds the maximum time in seconds to wait
- * @return true if file exists
- * @throws NullPointerException if the file is {@code null}
+ * @param file the file to open for input, must not be {@code null}
+ * @param encoding the encoding to use, {@code null} means platform default
+ * @return an Iterator of the lines in the file, never {@code null}
+ * @throws IOException in case of an I/O error (file closed)
+ * @since 1.2
*/
- public static boolean waitFor(final File file, final int seconds) {
- final long finishAt = System.currentTimeMillis() + (seconds * 1000L);
- boolean wasInterrupted = false;
+ public static LineIterator lineIterator(final File file, final String encoding) throws IOException {
+ InputStream in = null;
try {
- while (!file.exists()) {
- final long remaining = finishAt - System.currentTimeMillis();
- if (remaining < 0){
- return false;
- }
- try {
- Thread.sleep(Math.min(100, remaining));
- } catch (final InterruptedException ignore) {
- wasInterrupted = true;
- } catch (final Exception ex) {
- break;
+ in = openInputStream(file);
+ return IOUtils.lineIterator(in, encoding);
+ } catch (final IOException | RuntimeException ex) {
+ try {
+ if (in != null) {
+ in.close();
}
}
- } finally {
- if (wasInterrupted) {
- Thread.currentThread().interrupt();
+ catch (final IOException e) {
+ ex.addSuppressed(e);
}
+ throw ex;
}
- return true;
}
- //-----------------------------------------------------------------------
/**
- * Reads the contents of a file into a String.
- * The file is always closed.
+ * Finds files within a given directory (and optionally its
+ * subdirectories). All files found are filtered by an IOFileFilter.
+ * <p>
+ * 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.
+ * </p>
+ * <p>
+ * An example: If you want to search through all directories called
+ * "temp" you pass in <code>FileFilterUtils.NameFileFilter("temp")</code>
+ * </p>
+ * <p>
+ * Another common usage of this method is find files in a directory
+ * tree but ignoring the directories generated CVS. You can simply pass
+ * in <code>FileFilterUtils.makeCVSAware(null)</code>.
+ * </p>
*
- * @param file the file to read, must not be {@code null}
- * @param encoding the encoding to use, {@code null} means platform default
- * @return the file contents, never {@code null}
- * @throws IOException in case of an I/O error
- * @since 2.3
+ * @param directory the directory to search in
+ * @param fileFilter filter to apply when finding files. Must not be {@code null},
+ * use {@link TrueFileFilter#INSTANCE} to match all files in selected directories.
+ * @param dirFilter optional filter to apply when finding subdirectories.
+ * If this parameter is {@code null}, subdirectories will not be included in the
+ * search. Use {@link TrueFileFilter#INSTANCE} to match all directories.
+ * @return a collection of java.io.File with the matching files
+ * @see org.apache.commons.io.filefilter.FileFilterUtils
+ * @see org.apache.commons.io.filefilter.NameFileFilter
*/
- public static String readFileToString(final File file, final Charset encoding) throws IOException {
- try (InputStream in = openInputStream(file)) {
- return IOUtils.toString(in, Charsets.toCharset(encoding));
- }
+ public static Collection<File> listFiles(
+ final File directory, final IOFileFilter fileFilter, final IOFileFilter dirFilter) {
+ return innerListFilesOrDirectories(directory, fileFilter, dirFilter, false);
}
+
/**
- * Reads the contents of a file into a String. The file is always closed.
+ * Finds files within a given directory (and optionally its subdirectories)
+ * which match an array of extensions.
*
- * @param file the file to read, must not be {@code null}
- * @param encoding the encoding to use, {@code null} means platform default
- * @return the file contents, never {@code null}
- * @throws IOException in case of an I/O error
- * @throws java.nio.charset.UnsupportedCharsetException thrown instead of {@link java.io
- * .UnsupportedEncodingException} in version 2.2 if the encoding is not supported.
- * @since 2.3
+ * @param directory the directory to search in
+ * @param extensions an array of extensions, ex. {"java","xml"}. If this
+ * parameter is {@code null}, all files are returned.
+ * @param recursive if true all subdirectories are searched as well
+ * @return a collection of java.io.File with the matching files
*/
- public static String readFileToString(final File file, final String encoding) throws IOException {
- return readFileToString(file, Charsets.toCharset(encoding));
+ public static Collection<File> listFiles(
+ final File directory, final String[] extensions, final boolean recursive) {
+ IOFileFilter filter;
+ if (extensions == null) {
+ filter = TrueFileFilter.INSTANCE;
+ } else {
+ final String[] suffixes = toSuffixes(extensions);
+ filter = new SuffixFileFilter(suffixes);
+ }
+ return listFiles(directory, filter,
+ recursive ? TrueFileFilter.INSTANCE : FalseFileFilter.INSTANCE);
}
+ /**
+ * Finds files within a given directory (and optionally its
+ * subdirectories). All files found are filtered by an IOFileFilter.
+ * <p>
+ * The resulting collection includes the starting directory and
+ * any subdirectories that match the directory filter.
+ * </p>
+ *
+ * @param directory the directory to search in
+ * @param fileFilter filter to apply when finding files.
+ * @param dirFilter optional filter to apply when finding subdirectories.
+ * If this parameter is {@code null}, subdirectories will not be included in the
+ * search. Use TrueFileFilter.INSTANCE to match all directories.
+ * @return a collection of java.io.File with the matching files
+ * @see org.apache.commons.io.FileUtils#listFiles
+ * @see org.apache.commons.io.filefilter.FileFilterUtils
+ * @see org.apache.commons.io.filefilter.NameFileFilter
+ * @since 2.2
+ */
+ public static Collection<File> listFilesAndDirs(
+ final File directory, final IOFileFilter fileFilter, final IOFileFilter dirFilter) {
+ return innerListFilesOrDirectories(directory, fileFilter, dirFilter, true);
+ }
/**
- * Reads the contents of a file into a String using the default encoding for the VM.
- * The file is always closed.
+ * Moves a directory.
+ * <p>
+ * When the destination directory is on another file system, do a "copy and delete".
+ * </p>
*
- * @param file the file to read, must not be {@code null}
- * @return the file contents, never {@code null}
- * @throws IOException in case of an I/O error
- * @since 1.3.1
- * @deprecated 2.5 use {@link #readFileToString(File, Charset)} instead (and specify the appropriate encoding)
+ * @param srcDir the directory to be moved
+ * @param destDir the destination directory
+ * @throws NullPointerException if source or destination is {@code null}
+ * @throws FileExistsException if the destination directory exists
+ * @throws IOException if source or destination is invalid
+ * @throws IOException if an IO error occurs moving the file
+ * @since 1.4
*/
- @Deprecated
- public static String readFileToString(final File file) throws IOException {
- return readFileToString(file, Charset.defaultCharset());
+ public static void moveDirectory(final File srcDir, final File destDir) throws IOException {
+ validateMoveParameters(srcDir, destDir);
+ if (!srcDir.isDirectory()) {
+ throw new IOException("Source '" + srcDir + "' is not a directory");
+ }
+ if (destDir.exists()) {
+ throw new FileExistsException("Destination '" + destDir + "' already exists");
+ }
+ final boolean rename = srcDir.renameTo(destDir);
+ if (!rename) {
+ if (destDir.getCanonicalPath().startsWith(srcDir.getCanonicalPath() + File.separator)) {
+ throw new IOException("Cannot move directory: " + srcDir + " to a subdirectory of itself: " + destDir);
+ }
+ copyDirectory(srcDir, destDir);
+ deleteDirectory(srcDir);
+ if (srcDir.exists()) {
+ throw new IOException("Failed to delete original directory '" + srcDir +
+ "' after copy to '" + destDir + "'");
+ }
+ }
}
/**
- * Reads the contents of a file into a byte array.
- * The file is always closed.
+ * Moves a directory to another directory.
*
- * @param file the file to read, must not be {@code null}
- * @return the file contents, never {@code null}
- * @throws IOException in case of an I/O error
- * @since 1.1
+ * @param src the file to be moved
+ * @param destDir the destination file
+ * @param createDestDir If {@code true} create the destination directory,
+ * otherwise if {@code false} throw an IOException
+ * @throws NullPointerException if source or destination is {@code null}
+ * @throws FileExistsException if the directory exists in the destination directory
+ * @throws IOException if source or destination is invalid
+ * @throws IOException if an IO error occurs moving the file
+ * @since 1.4
*/
- public static byte[] readFileToByteArray(final File file) throws IOException {
- try (InputStream in = openInputStream(file)) {
- final long fileLength = file.length();
- // file.length() may return 0 for system-dependent entities, treat 0 as unknown length - see IO-453
- return fileLength > 0 ? IOUtils.toByteArray(in, fileLength) : IOUtils.toByteArray(in);
+ public static void moveDirectoryToDirectory(final File src, final File destDir, final boolean createDestDir)
+ throws IOException {
+ validateMoveParameters(src, destDir);
+ if (!destDir.exists() && createDestDir) {
+ destDir.mkdirs();
+ }
+ if (!destDir.exists()) {
+ throw new FileNotFoundException("Destination directory '" + destDir +
+ "' does not exist [createDestDir=" + createDestDir + "]");
+ }
+ if (!destDir.isDirectory()) {
+ throw new IOException("Destination '" + destDir + "' is not a directory");
}
+ moveDirectory(src, new File(destDir, src.getName()));
}
/**
- * Reads the contents of a file line by line to a List of Strings.
- * The file is always closed.
+ * Moves a file.
+ * <p>
+ * When the destination file is on another file system, do a "copy and delete".
+ * </p>
*
- * @param file the file to read, must not be {@code null}
- * @param encoding the encoding to use, {@code null} means platform default
- * @return the list of Strings representing each line in the file, never {@code null}
- * @throws IOException in case of an I/O error
- * @since 2.3
+ * @param srcFile the file to be moved
+ * @param destFile the destination file
+ * @throws NullPointerException if source or destination is {@code null}
+ * @throws FileExistsException if the destination file exists
+ * @throws IOException if source or destination is invalid
+ * @throws IOException if an IO error occurs moving the file
+ * @since 1.4
*/
- public static List<String> readLines(final File file, final Charset encoding) throws IOException {
- try (InputStream in = openInputStream(file)) {
- return IOUtils.readLines(in, Charsets.toCharset(encoding));
+ public static void moveFile(final File srcFile, final File destFile) throws IOException {
+ validateMoveParameters(srcFile, destFile);
+ if (srcFile.isDirectory()) {
+ throw new IOException("Source '" + srcFile + "' is a directory");
+ }
+ if (destFile.exists()) {
+ throw new FileExistsException("Destination '" + destFile + "' already exists");
+ }
+ if (destFile.isDirectory()) {
+ throw new IOException("Destination '" + destFile + "' is a directory");
+ }
+ final boolean rename = srcFile.renameTo(destFile);
+ if (!rename) {
+ copyFile(srcFile, destFile);
+ if (!srcFile.delete()) {
+ FileUtils.deleteQuietly(destFile);
+ throw new IOException("Failed to delete original file '" + srcFile +
+ "' after copy to '" + destFile + "'");
+ }
}
}
/**
- * Reads the contents of a file line by line to a List of Strings. The file is always closed.
+ * Moves a file to a directory.
*
- * @param file the file to read, must not be {@code null}
- * @param encoding the encoding to use, {@code null} means platform default
- * @return the list of Strings representing each line in the file, never {@code null}
- * @throws IOException in case of an I/O error
- * @throws java.nio.charset.UnsupportedCharsetException thrown instead of {@link java.io
- * .UnsupportedEncodingException} in version 2.2 if the encoding is not supported.
- * @since 1.1
+ * @param srcFile the file to be moved
+ * @param destDir the destination file
+ * @param createDestDir If {@code true} create the destination directory,
+ * otherwise if {@code false} throw an IOException
+ * @throws NullPointerException if source or destination is {@code null}
+ * @throws FileExistsException if the destination file exists
+ * @throws IOException if source or destination is invalid
+ * @throws IOException if an IO error occurs moving the file
+ * @since 1.4
*/
- public static List<String> readLines(final File file, final String encoding) throws IOException {
- return readLines(file, Charsets.toCharset(encoding));
+ public static void moveFileToDirectory(final File srcFile, final File destDir, final boolean createDestDir)
+ throws IOException {
+ validateMoveParameters(srcFile, destDir);
+ if (!destDir.exists() && createDestDir) {
+ destDir.mkdirs();
+ }
+ if (!destDir.exists()) {
+ throw new FileNotFoundException("Destination directory '" + destDir +
+ "' does not exist [createDestDir=" + createDestDir + "]");
+ }
+ if (!destDir.isDirectory()) {
+ throw new IOException("Destination '" + destDir + "' is not a directory");
+ }
+ moveFile(srcFile, new File(destDir, srcFile.getName()));
}
/**
- * Reads the contents of a file line by line to a List of Strings using the default encoding for the VM.
- * The file is always closed.
+ * Moves a file or directory to the destination directory.
+ * <p>
+ * When the destination is on another file system, do a "copy and delete".
+ * </p>
*
- * @param file the file to read, must not be {@code null}
- * @return the list of Strings representing each line in the file, never {@code null}
- * @throws IOException in case of an I/O error
- * @since 1.3
- * @deprecated 2.5 use {@link #readLines(File, Charset)} instead (and specify the appropriate encoding)
+ * @param src the file or directory to be moved
+ * @param destDir the destination directory
+ * @param createDestDir If {@code true} create the destination directory,
+ * otherwise if {@code false} throw an IOException
+ * @throws NullPointerException if source or destination is {@code null}
+ * @throws FileExistsException if the directory or file exists in the destination directory
+ * @throws IOException if source or destination is invalid
+ * @throws IOException if an IO error occurs moving the file
+ * @since 1.4
*/
- @Deprecated
- public static List<String> readLines(final File file) throws IOException {
- return readLines(file, Charset.defaultCharset());
+ public static void moveToDirectory(final File src, final File destDir, final boolean createDestDir)
+ throws IOException {
+ validateMoveParameters(src, destDir);
+ if (src.isDirectory()) {
+ moveDirectoryToDirectory(src, destDir, createDestDir);
+ } else {
+ moveFileToDirectory(src, destDir, createDestDir);
+ }
}
+ //-----------------------------------------------------------------------
+
+ //-----------------------------------------------------------------------
/**
- * Returns an Iterator for the lines in a <code>File</code>.
- * <p>
- * This method opens an <code>InputStream</code> for the file.
- * When you have finished with the iterator you should close the stream
- * to free internal resources. This can be done by calling the
- * {@link LineIterator#close()} or
- * {@link LineIterator#closeQuietly(LineIterator)} method.
- * </p>
+ * Opens a {@link FileInputStream} for the specified file, providing better
+ * error messages than simply calling <code>new FileInputStream(file)</code>.
* <p>
- * The recommended usage pattern is:
+ * At the end of the method either the stream will be successfully opened,
+ * or an exception will have been thrown.
* </p>
- * <pre>
- * LineIterator it = FileUtils.lineIterator(file, "UTF-8");
- * try {
- * while (it.hasNext()) {
- * String line = it.nextLine();
- * /// do something with line
- * }
- * } finally {
- * LineIterator.closeQuietly(iterator);
- * }
- * </pre>
* <p>
- * If an exception occurs during the creation of the iterator, the
- * underlying stream is closed.
+ * 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.
* </p>
*
- * @param file the file to open for input, must not be {@code null}
- * @param encoding the encoding to use, {@code null} means platform default
- * @return an Iterator of the lines in the file, never {@code null}
- * @throws IOException in case of an I/O error (file closed)
- * @since 1.2
+ * @param file the file to open for input, must not be {@code null}
+ * @return a new {@link FileInputStream} for the specified file
+ * @throws FileNotFoundException if the file does not exist
+ * @throws IOException if the file object is a directory
+ * @throws IOException if the file cannot be read
+ * @since 1.3
*/
- public static LineIterator lineIterator(final File file, final String encoding) throws IOException {
- InputStream in = null;
- try {
- in = openInputStream(file);
- return IOUtils.lineIterator(in, encoding);
- } catch (final IOException | RuntimeException ex) {
- try {
- if (in != null) {
- in.close();
- }
+ public static FileInputStream openInputStream(final File file) throws IOException {
+ if (file.exists()) {
+ if (file.isDirectory()) {
+ throw new IOException("File '" + file + "' exists but is a directory");
}
- catch (final IOException e) {
- ex.addSuppressed(e);
+ if (file.canRead() == false) {
+ throw new IOException("File '" + file + "' cannot be read");
}
- throw ex;
+ } else {
+ throw new FileNotFoundException("File '" + file + "' does not exist");
}
- }
-
- /**
- * Returns an Iterator for the lines in a <code>File</code> using the default encoding for the VM.
- *
- * @param file the file to open for input, must not be {@code null}
- * @return an Iterator of the lines in the file, never {@code null}
- * @throws IOException in case of an I/O error (file closed)
- * @see #lineIterator(File, String)
- * @since 1.3
- */
- public static LineIterator lineIterator(final File file) throws IOException {
- return lineIterator(file, null);
+ return new FileInputStream(file);
}
//-----------------------------------------------------------------------
-
/**
- * Writes a String to a file creating the file if it does not exist.
+ * Opens a {@link FileOutputStream} for the specified file, checking and
+ * creating the parent directory if it does not exist.
* <p>
- * NOTE: As from v1.3, the parent directories of the file will be created
- * if they do not exist.
+ * At the end of the method either the stream will be successfully opened,
+ * or an exception will have been thrown.
+ * </p>
+ * <p>
+ * 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.
* </p>
*
- * @param file the file to write
- * @param data the content to write to the file
- * @param encoding the encoding to use, {@code null} means platform default
- * @throws IOException in case of an I/O error
- * @throws java.io.UnsupportedEncodingException if the encoding is not supported by the VM
- * @since 2.4
+ * @param file the file to open for output, must not be {@code null}
+ * @return a new {@link FileOutputStream} for the specified file
+ * @throws IOException if the file object is a directory
+ * @throws IOException if the file cannot be written to
+ * @throws IOException if a parent directory needs creating but that fails
+ * @since 1.3
*/
- public static void writeStringToFile(final File file, final String data, final Charset encoding)
- throws IOException {
- writeStringToFile(file, data, encoding, false);
+ public static FileOutputStream openOutputStream(final File file) throws IOException {
+ return openOutputStream(file, false);
}
/**
- * Writes a String to a file creating the file if it does not exist.
+ * Opens a {@link FileOutputStream} for the specified file, checking and
+ * creating the parent directory if it does not exist.
* <p>
- * NOTE: As from v1.3, the parent directories of the file will be created
- * if they do not exist.
+ * At the end of the method either the stream will be successfully opened,
+ * or an exception will have been thrown.
+ * </p>
+ * <p>
+ * 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.
* </p>
*
- * @param file the file to write
- * @param data the content to write to the file
- * @param encoding the encoding to use, {@code null} means platform default
- * @throws IOException in case of an I/O error
- * @throws java.io.UnsupportedEncodingException if the encoding is not supported by the VM
+ * @param file the file to open for output, must not be {@code null}
+ * @param append if {@code true}, then bytes will be added to the
+ * end of the file rather than overwriting
+ * @return a new {@link FileOutputStream} for the specified file
+ * @throws IOException if the file object is a directory
+ * @throws IOException if the file cannot be written to
+ * @throws IOException if a parent directory needs creating but that fails
+ * @since 2.1
*/
- public static void writeStringToFile(final File file, final String data, final String encoding) throws IOException {
- writeStringToFile(file, data, encoding, false);
+ public static FileOutputStream openOutputStream(final File file, final boolean append) throws IOException {
+ if (file.exists()) {
+ if (file.isDirectory()) {
+ throw new IOException("File '" + file + "' exists but is a directory");
+ }
+ if (file.canWrite() == false) {
+ throw new IOException("File '" + file + "' cannot be written to");
+ }
+ } else {
+ final File parent = file.getParentFile();
+ if (parent != null) {
+ if (!parent.mkdirs() && !parent.isDirectory()) {
+ throw new IOException("Directory '" + parent + "' could not be created");
+ }
+ }
+ }
+ return new FileOutputStream(file, append);
}
/**
- * Writes a String to a file creating the file if it does not exist.
+ * Reads the contents of a file into a byte array.
+ * The file is always closed.
*
- * @param file the file to write
- * @param data the content to write to the file
- * @param encoding the encoding to use, {@code null} means platform default
- * @param append if {@code true}, then the String will be added to the
- * end of the file rather than overwriting
+ * @param file the file to read, must not be {@code null}
+ * @return the file contents, never {@code null}
* @throws IOException in case of an I/O error
- * @since 2.3
+ * @since 1.1
*/
- public static void writeStringToFile(final File file, final String data, final Charset encoding,
- final boolean append) throws IOException {
- try (OutputStream out = openOutputStream(file, append)) {
- IOUtils.write(data, out, encoding);
+ public static byte[] readFileToByteArray(final File file) throws IOException {
+ try (InputStream in = openInputStream(file)) {
+ final long fileLength = file.length();
+ // file.length() may return 0 for system-dependent entities, treat 0 as unknown length - see IO-453
+ return fileLength > 0 ? IOUtils.toByteArray(in, fileLength) : IOUtils.toByteArray(in);
}
}
/**
- * Writes a String to a file creating the file if it does not exist.
- *
- * @param file the file to write
- * @param data the content to write to the file
- * @param encoding the encoding to use, {@code null} means platform default
- * @param append if {@code true}, then the String will be added to the
- * end of the file rather than overwriting
- * @throws IOException in case of an I/O error
- * @throws java.nio.charset.UnsupportedCharsetException thrown instead of {@link java.io
- * .UnsupportedEncodingException} in version 2.2 if the encoding is not supported by the VM
- * @since 2.1
- */
- public static void writeStringToFile(final File file, final String data, final String encoding,
- final boolean append) throws IOException {
- writeStringToFile(file, data, Charsets.toCharset(encoding), append);
- }
-
- /**
- * Writes a String to a file creating the file if it does not exist using the default encoding for the VM.
+ * Reads the contents of a file into a String using the default encoding for the VM.
+ * The file is always closed.
*
- * @param file the file to write
- * @param data the content to write to the file
+ * @param file the file to read, must not be {@code null}
+ * @return the file contents, never {@code null}
* @throws IOException in case of an I/O error
- * @deprecated 2.5 use {@link #writeStringToFile(File, String, Charset)} instead (and specify the appropriate encoding)
+ * @since 1.3.1
+ * @deprecated 2.5 use {@link #readFileToString(File, Charset)} instead (and specify the appropriate encoding)
*/
@Deprecated
- public static void writeStringToFile(final File file, final String data) throws IOException {
- writeStringToFile(file, data, Charset.defaultCharset(), false);
+ public static String readFileToString(final File file) throws IOException {
+ return readFileToString(file, Charset.defaultCharset());
}
+ //-----------------------------------------------------------------------
/**
- * Writes a String to a file creating the file if it does not exist using the default encoding for the VM.
+ * Reads the contents of a file into a String.
+ * The file is always closed.
*
- * @param file the file to write
- * @param data the content to write to the file
- * @param append if {@code true}, then the String will be added to the
- * end of the file rather than overwriting
+ * @param file the file to read, must not be {@code null}
+ * @param encoding the encoding to use, {@code null} means platform default
+ * @return the file contents, never {@code null}
* @throws IOException in case of an I/O error
- * @since 2.1
- * @deprecated 2.5 use {@link #writeStringToFile(File, String, Charset, boolean)} instead (and specify the appropriate encoding)
+ * @since 2.3
*/
- @Deprecated
- public static void writeStringToFile(final File file, final String data, final boolean append) throws IOException {
- writeStringToFile(file, data, Charset.defaultCharset(), append);
+ public static String readFileToString(final File file, final Charset encoding) throws IOException {
+ try (InputStream in = openInputStream(file)) {
+ return IOUtils.toString(in, Charsets.toCharset(encoding));
+ }
}
/**
- * Writes a CharSequence to a file creating the file if it does not exist using the default encoding for the VM.
+ * Reads the contents of a file into a String. The file is always closed.
*
- * @param file the file to write
- * @param data the content to write to the file
- * @throws IOException in case of an I/O error
- * @since 2.0
- * @deprecated 2.5 use {@link #write(File, CharSequence, Charset)} instead (and specify the appropriate encoding)
+ * @param file the file to read, must not be {@code null}
+ * @param encoding the encoding to use, {@code null} means platform default
+ * @return the file contents, never {@code null}
+ * @throws IOException in case of an I/O error
+ * @throws java.nio.charset.UnsupportedCharsetException thrown instead of {@link java.io
+ * .UnsupportedEncodingException} in version 2.2 if the encoding is not supported.
+ * @since 2.3
*/
- @Deprecated
- public static void write(final File file, final CharSequence data) throws IOException {
- write(file, data, Charset.defaultCharset(), false);
+ public static String readFileToString(final File file, final String encoding) throws IOException {
+ return readFileToString(file, Charsets.toCharset(encoding));
}
/**
- * Writes a CharSequence to a file creating the file if it does not exist using the default encoding for the VM.
+ * Reads the contents of a file line by line to a List of Strings using the default encoding for the VM.
+ * The file is always closed.
*
- * @param file the file to write
- * @param data the content to write to the file
- * @param append if {@code true}, then the data will be added to the
- * end of the file rather than overwriting
+ * @param file the file to read, must not be {@code null}
+ * @return the list of Strings representing each line in the file, never {@code null}
* @throws IOException in case of an I/O error
- * @since 2.1
- * @deprecated 2.5 use {@link #write(File, CharSequence, Charset, boolean)} instead (and specify the appropriate encoding)
+ * @since 1.3
+ * @deprecated 2.5 use {@link #readLines(File, Charset)} instead (and specify the appropriate encoding)
*/
@Deprecated
- public static void write(final File file, final CharSequence data, final boolean append) throws IOException {
- write(file, data, Charset.defaultCharset(), append);
+ public static List<String> readLines(final File file) throws IOException {
+ return readLines(file, Charset.defaultCharset());
}
/**
- * Writes a CharSequence to a file creating the file if it does not exist.
+ * Reads the contents of a file line by line to a List of Strings.
+ * The file is always closed.
*
- * @param file the file to write
- * @param data the content to write to the file
+ * @param file the file to read, must not be {@code null}
* @param encoding the encoding to use, {@code null} means platform default
+ * @return the list of Strings representing each line in the file, never {@code null}
* @throws IOException in case of an I/O error
* @since 2.3
*/
- public static void write(final File file, final CharSequence data, final Charset encoding) throws IOException {
- write(file, data, encoding, false);
+ public static List<String> readLines(final File file, final Charset encoding) throws IOException {
+ try (InputStream in = openInputStream(file)) {
+ return IOUtils.readLines(in, Charsets.toCharset(encoding));
+ }
}
/**
- * Writes a CharSequence to a file creating the file if it does not exist.
+ * Reads the contents of a file line by line to a List of Strings. The file is always closed.
*
- * @param file the file to write
- * @param data the content to write to the file
+ * @param file the file to read, must not be {@code null}
* @param encoding the encoding to use, {@code null} means platform default
- * @throws IOException in case of an I/O error
- * @throws java.io.UnsupportedEncodingException if the encoding is not supported by the VM
- * @since 2.0
+ * @return the list of Strings representing each line in the file, never {@code null}
+ * @throws IOException in case of an I/O error
+ * @throws java.nio.charset.UnsupportedCharsetException thrown instead of {@link java.io
+ * .UnsupportedEncodingException} in version 2.2 if the encoding is not supported.
+ * @since 1.1
*/
- public static void write(final File file, final CharSequence data, final String encoding) throws IOException {
- write(file, data, encoding, false);
+ public static List<String> readLines(final File file, final String encoding) throws IOException {
+ return readLines(file, Charsets.toCharset(encoding));
}
/**
- * Writes a CharSequence to a file creating the file if it does not exist.
+ * Returns a filter that accepts directories in addition to the {@link File} objects accepted by the given filter.
*
- * @param file the file to write
- * @param data the content to write to the file
- * @param encoding the encoding to use, {@code null} means platform default
- * @param append if {@code true}, then the data will be added to the
- * end of the file rather than overwriting
- * @throws IOException in case of an I/O error
- * @since 2.3
+ * @param dirFilter a base filter to add to
+ * @return a filter that accepts directories
*/
- public static void write(final File file, final CharSequence data, final Charset encoding, final boolean append)
- throws IOException {
- final String str = data == null ? null : data.toString();
- writeStringToFile(file, str, encoding, append);
+ private static IOFileFilter setUpEffectiveDirFilter(final IOFileFilter dirFilter) {
+ return dirFilter == null ? FalseFileFilter.INSTANCE : FileFilterUtils.and(dirFilter,
+ DirectoryFileFilter.INSTANCE);
}
/**
- * Writes a CharSequence to a file creating the file if it does not exist.
+ * Returns a filter that accepts files in addition to the {@link File} objects accepted by the given filter.
*
- * @param file the file to write
- * @param data the content to write to the file
- * @param encoding the encoding to use, {@code null} means platform default
- * @param append if {@code true}, then the data will be added to the
- * end of the file rather than overwriting
- * @throws IOException in case of an I/O error
- * @throws java.nio.charset.UnsupportedCharsetException thrown instead of {@link java.io
- * .UnsupportedEncodingException} in version 2.2 if the encoding is not supported by the VM
- * @since 2.1
+ * @param fileFilter a base filter to add to
+ * @return a filter that accepts files
*/
- public static void write(final File file, final CharSequence data, final String encoding, final boolean append)
- throws IOException {
- write(file, data, Charsets.toCharset(encoding), append);
+ private static IOFileFilter setUpEffectiveFileFilter(final IOFileFilter fileFilter) {
+ return FileFilterUtils.and(fileFilter, FileFilterUtils.notFileFilter(DirectoryFileFilter.INSTANCE));
}
+ //-----------------------------------------------------------------------
/**
- * Writes a byte array to a file creating the file if it does not exist.
+ * Returns the size of the specified file or directory. If the provided
+ * {@link 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.
* <p>
- * NOTE: As from v1.3, the parent directories of the file will be created
- * if they do not exist.
+ * Note that overflow is not detected, and the return value may be negative if
+ * overflow occurs. See {@link #sizeOfAsBigInteger(File)} for an alternative
+ * method that does not overflow.
* </p>
*
- * @param file the file to write to
- * @param data the content to write to the file
- * @throws IOException in case of an I/O error
- * @since 1.1
+ * @param file the regular file or directory to return the size
+ * of (must not be {@code null}).
+ *
+ * @return the length of the file, or recursive size of the directory,
+ * provided (in bytes).
+ *
+ * @throws NullPointerException if the file is {@code null}
+ * @throws IllegalArgumentException if the file does not exist.
+ *
+ * @since 2.0
*/
- public static void writeByteArrayToFile(final File file, final byte[] data) throws IOException {
- writeByteArrayToFile(file, data, false);
+ public static long sizeOf(final File file) {
+
+ if (!file.exists()) {
+ final String message = file + " does not exist";
+ throw new IllegalArgumentException(message);
+ }
+
+ if (file.isDirectory()) {
+ return sizeOfDirectory0(file); // private method; expects directory
+ }
+ return file.length();
+
}
/**
- * Writes a byte array to a file creating the file if it does not exist.
- *
- * @param file the file to write to
- * @param data the content to write to the file
- * @param append if {@code true}, then bytes will be added to the
- * end of the file rather than overwriting
- * @throws IOException in case of an I/O error
- * @since 2.1
+ * the size of a file
+ * @param file the file to check
+ * @return the size of the file
*/
- public static void writeByteArrayToFile(final File file, final byte[] data, final boolean append)
- throws IOException {
- writeByteArrayToFile(file, data, 0, data.length, append);
+ private static long sizeOf0(final File file) {
+ if (file.isDirectory()) {
+ return sizeOfDirectory0(file);
+ }
+ return file.length(); // will be 0 if file does not exist
}
/**
- * Writes {@code len} bytes from the specified byte array starting
- * at offset {@code off} to a file, creating the file if it does
- * not exist.
+ * Returns the size of the specified file or directory. If the provided
+ * {@link 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.
*
- * @param file the file to write to
- * @param data the content to write to the file
- * @param off the start offset in the data
- * @param len the number of bytes to write
- * @throws IOException in case of an I/O error
- * @since 2.5
+ * @param file the regular file or directory to return the size
+ * of (must not be {@code null}).
+ *
+ * @return the length of the file, or recursive size of the directory,
+ * provided (in bytes).
+ *
+ * @throws NullPointerException if the file is {@code null}
+ * @throws IllegalArgumentException if the file does not exist.
+ *
+ * @since 2.4
*/
- public static void writeByteArrayToFile(final File file, final byte[] data, final int off, final int len)
- throws IOException {
- writeByteArrayToFile(file, data, off, len, false);
+ public static BigInteger sizeOfAsBigInteger(final File file) {
+
+ if (!file.exists()) {
+ final String message = file + " does not exist";
+ throw new IllegalArgumentException(message);
+ }
+
+ if (file.isDirectory()) {
+ return sizeOfDirectoryBig0(file); // internal method
+ }
+ return BigInteger.valueOf(file.length());
+
}
/**
- * Writes {@code len} bytes from the specified byte array starting
- * at offset {@code off} to a file, creating the file if it does
- * not exist.
- *
- * @param file the file to write to
- * @param data the content to write to the file
- * @param off the start offset in the data
- * @param len the number of bytes to write
- * @param append if {@code true}, then bytes will be added to the
- * end of the file rather than overwriting
- * @throws IOException in case of an I/O error
- * @since 2.5
+ * Returns the size of a file
+ * @param fileOrDir The file
+ * @return the size
*/
- public static void writeByteArrayToFile(final File file, final byte[] data, final int off, final int len,
- final boolean append) throws IOException {
- try (OutputStream out = openOutputStream(file, append)) {
- out.write(data, off, len);
+ private static BigInteger sizeOfBig0(final File fileOrDir) {
+ if (fileOrDir.isDirectory()) {
+ return sizeOfDirectoryBig0(fileOrDir);
}
+ return BigInteger.valueOf(fileOrDir.length());
}
/**
- * Writes the <code>toString()</code> value of each item in a collection to
- * the specified <code>File</code> line by line.
- * The specified character encoding and the default line ending will be used.
+ * Counts the size of a directory recursively (sum of the length of all files).
* <p>
- * NOTE: As from v1.3, the parent directories of the file will be created
- * if they do not exist.
+ * Note that overflow is not detected, and the return value may be negative if
+ * overflow occurs. See {@link #sizeOfDirectoryAsBigInteger(File)} for an alternative
+ * method that does not overflow.
* </p>
*
- * @param file the file to write to
- * @param encoding the encoding to use, {@code null} means platform default
- * @param lines the lines to write, {@code null} entries produce blank lines
- * @throws IOException in case of an I/O error
- * @throws java.io.UnsupportedEncodingException if the encoding is not supported by the VM
- * @since 1.1
+ * @param directory directory to inspect, must not be {@code null}
+ * @return size of directory in bytes, 0 if directory is security restricted, a negative number when the real total
+ * is greater than {@link Long#MAX_VALUE}.
+ * @throws NullPointerException if the directory is {@code null}
*/
- public static void writeLines(final File file, final String encoding, final Collection<?> lines)
- throws IOException {
- writeLines(file, encoding, lines, null, false);
+ public static long sizeOfDirectory(final File directory) {
+ checkDirectory(directory);
+ return sizeOfDirectory0(directory);
}
/**
- * Writes the <code>toString()</code> value of each item in a collection to
- * the specified <code>File</code> line by line, optionally appending.
- * The specified character encoding and the default line ending will be used.
- *
- * @param file the file to write to
- * @param encoding the encoding to use, {@code null} means platform default
- * @param lines the lines to write, {@code null} entries produce blank lines
- * @param append if {@code true}, then the lines will be added to the
- * end of the file rather than overwriting
- * @throws IOException in case of an I/O error
- * @throws java.io.UnsupportedEncodingException if the encoding is not supported by the VM
- * @since 2.1
+ * the size of a director
+ * @param directory the directory to check
+ * @return the size
*/
- public static void writeLines(final File file, final String encoding, final Collection<?> lines,
- final boolean append) throws IOException {
- writeLines(file, encoding, lines, null, append);
+ private static long sizeOfDirectory0(final File directory) {
+ final File[] files = directory.listFiles();
+ if (files == null) { // null if security restricted
+ return 0L;
+ }
+ long size = 0;
+
+ for (final File file : files) {
+ if (!isSymlink(file)) {
+ size += sizeOf0(file); // internal method
+ if (size < 0) {
+ break;
+ }
+ }
+ }
+
+ return size;
}
/**
- * Writes the <code>toString()</code> value of each item in a collection to
- * the specified <code>File</code> line by line.
- * The default VM encoding and the default line ending will be used.
+ * Counts the size of a directory recursively (sum of the length of all files).
*
- * @param file the file to write to
- * @param lines the lines to write, {@code null} entries produce blank lines
- * @throws IOException in case of an I/O error
- * @since 1.3
+ * @param directory directory to inspect, must not be {@code null}
+ * @return size of directory in bytes, 0 if directory is security restricted.
+ * @throws NullPointerException if the directory is {@code null}
+ * @since 2.4
*/
- public static void writeLines(final File file, final Collection<?> lines) throws IOException {
- writeLines(file, null, lines, null, false);
+ public static BigInteger sizeOfDirectoryAsBigInteger(final File directory) {
+ checkDirectory(directory);
+ return sizeOfDirectoryBig0(directory);
}
/**
- * Writes the <code>toString()</code> value of each item in a collection to
- * the specified <code>File</code> line by line.
- * The default VM encoding and the default line ending will be used.
+ * Finds the size of a directory
*
- * @param file the file to write to
- * @param lines the lines to write, {@code null} entries produce blank lines
- * @param append if {@code true}, then the lines will be added to the
- * end of the file rather than overwriting
- * @throws IOException in case of an I/O error
- * @since 2.1
+ * @param directory The directory
+ * @return the size
*/
- public static void writeLines(final File file, final Collection<?> lines, final boolean append) throws IOException {
- writeLines(file, null, lines, null, append);
+ private static BigInteger sizeOfDirectoryBig0(final File directory) {
+ final File[] files = directory.listFiles();
+ if (files == null) { // null if security restricted
+ return BigInteger.ZERO;
+ }
+ BigInteger size = BigInteger.ZERO;
+
+ for (final File file : files) {
+ if (!isSymlink(file)) {
+ size = size.add(sizeOfBig0(file));
+ }
+ }
+
+ return size;
}
+ //-----------------------------------------------------------------------
/**
- * Writes the <code>toString()</code> value of each item in a collection to
- * the specified <code>File</code> line by line.
- * The specified character encoding and the line ending will be used.
+ * Convert from a <code>URL</code> to a <code>File</code>.
* <p>
- * NOTE: As from v1.3, the parent directories of the file will be created
- * if they do not exist.
+ * From version 1.1 this method will decode the URL.
+ * Syntax such as <code>file:///my%20docs/file.txt</code> will be
+ * correctly decoded to <code>/my docs/file.txt</code>. 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.
* </p>
*
- * @param file the file to write to
- * @param encoding the encoding to use, {@code null} means platform default
- * @param lines the lines to write, {@code null} entries produce blank lines
- * @param lineEnding the line separator to use, {@code null} is system default
- * @throws IOException in case of an I/O error
- * @throws java.io.UnsupportedEncodingException if the encoding is not supported by the VM
- * @since 1.1
- */
- public static void writeLines(final File file, final String encoding, final Collection<?> lines,
- final String lineEnding) throws IOException {
- writeLines(file, encoding, lines, lineEnding, false);
- }
-
- /**
- * Writes the <code>toString()</code> value of each item in a collection to
- * the specified <code>File</code> line by line.
- * The specified character encoding and the line ending will be used.
- *
- * @param file the file to write to
- * @param encoding the encoding to use, {@code null} means platform default
- * @param lines the lines to write, {@code null} entries produce blank lines
- * @param lineEnding the line separator to use, {@code null} is system default
- * @param append if {@code true}, then the lines will be added to the
- * end of the file rather than overwriting
- * @throws IOException in case of an I/O error
- * @throws java.io.UnsupportedEncodingException if the encoding is not supported by the VM
- * @since 2.1
+ * @param url the file URL to convert, {@code null} returns {@code null}
+ * @return the equivalent <code>File</code> object, or {@code null}
+ * if the URL's protocol is not <code>file</code>
*/
- public static void writeLines(final File file, final String encoding, final Collection<?> lines,
- final String lineEnding, final boolean append) throws IOException {
- try (OutputStream out = new BufferedOutputStream(openOutputStream(file, append))) {
- IOUtils.writeLines(lines, lineEnding, out, encoding);
+ public static File toFile(final URL url) {
+ if (url == null || !"file".equalsIgnoreCase(url.getProtocol())) {
+ return null;
}
+ String filename = url.getFile().replace('/', File.separatorChar);
+ filename = decodeUrl(filename);
+ return new File(filename);
}
/**
- * Writes the <code>toString()</code> value of each item in a collection to
- * the specified <code>File</code> line by line.
- * The default VM encoding and the specified line ending will be used.
+ * Converts each of an array of <code>URL</code> to a <code>File</code>.
+ * <p>
+ * Returns an array of the same size as the input.
+ * If the input is {@code null}, an empty array is returned.
+ * If the input contains {@code null}, the output array contains {@code null} at the same
+ * index.
+ * </p>
+ * <p>
+ * This method will decode the URL.
+ * Syntax such as <code>file:///my%20docs/file.txt</code> will be
+ * correctly decoded to <code>/my docs/file.txt</code>.
+ * </p>
*
- * @param file the file to write to
- * @param lines the lines to write, {@code null} entries produce blank lines
- * @param lineEnding the line separator to use, {@code null} is system default
- * @throws IOException in case of an I/O error
- * @since 1.3
+ * @param urls the file URLs to convert, {@code null} returns empty array
+ * @return a non-{@code null} array of Files matching the input, with a {@code null} item
+ * if there was a {@code null} at that index in the input array
+ * @throws IllegalArgumentException if any file is not a URL file
+ * @throws IllegalArgumentException if any file is incorrectly encoded
+ * @since 1.1
*/
- public static void writeLines(final File file, final Collection<?> lines, final String lineEnding)
- throws IOException {
- writeLines(file, null, lines, lineEnding, false);
+ public static File[] toFiles(final URL... urls) {
+ if (urls == null || urls.length == 0) {
+ return EMPTY_FILE_ARRAY;
+ }
+ final File[] files = new File[urls.length];
+ for (int i = 0; i < urls.length; i++) {
+ final URL url = urls[i];
+ if (url != null) {
+ if (url.getProtocol().equals("file") == false) {
+ throw new IllegalArgumentException(
+ "URL could not be converted to a File: " + url);
+ }
+ files[i] = toFile(url);
+ }
+ }
+ return files;
}
+ //-----------------------------------------------------------------------
/**
- * Writes the <code>toString()</code> value of each item in a collection to
- * the specified <code>File</code> line by line.
- * The default VM encoding and the specified line ending will be used.
+ * Converts an array of file extensions to suffixes for use
+ * with IOFileFilters.
*
- * @param file the file to write to
- * @param lines the lines to write, {@code null} entries produce blank lines
- * @param lineEnding the line separator to use, {@code null} is system default
- * @param append if {@code true}, then the lines will be added to the
- * end of the file rather than overwriting
- * @throws IOException in case of an I/O error
- * @since 2.1
+ * @param extensions an array of extensions. Format: {"java", "xml"}
+ * @return an array of suffixes. Format: {".java", ".xml"}
*/
- public static void writeLines(final File file, final Collection<?> lines, final String lineEnding,
- final boolean append) throws IOException {
- writeLines(file, null, lines, lineEnding, append);
+ private static String[] toSuffixes(final String... extensions) {
+ final String[] suffixes = new String[extensions.length];
+ for (int i = 0; i < extensions.length; i++) {
+ suffixes[i] = "." + extensions[i];
+ }
+ return suffixes;
}
//-----------------------------------------------------------------------
/**
- * Deletes a file. If file is a directory, delete it and all sub-directories.
+ * Implements the same behaviour as the "touch" utility on Unix. It creates
+ * a new file with size 0 or, if the file exists already, it is opened and
+ * closed without modifying it, but updating the file date and time.
* <p>
- * The difference between File.delete() and this method are:
+ * 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.
* </p>
- * <ul>
- * <li>A directory to be deleted does not have to be empty.</li>
- * <li>You get exceptions when a file or directory cannot be deleted.
- * (java.io.File methods returns a boolean)</li>
- * </ul>
*
- * @param file file or directory to delete, must not be {@code null}
- * @throws NullPointerException if the directory is {@code null}
- * @throws FileNotFoundException if the file was not found
- * @throws IOException in case deletion is unsuccessful
+ * @param file the File to touch
+ * @throws IOException If an I/O problem occurs
*/
- public static void forceDelete(final File file) throws IOException {
- if (file.isDirectory()) {
- deleteDirectory(file);
- } else {
- final boolean filePresent = file.exists();
- if (!file.delete()) {
- if (!filePresent) {
- throw new FileNotFoundException("File does not exist: " + file);
- }
- final String message =
- "Unable to delete file: " + file;
- throw new IOException(message);
- }
+ public static void touch(final File file) throws IOException {
+ if (!file.exists()) {
+ openOutputStream(file).close();
+ }
+ final boolean success = file.setLastModified(System.currentTimeMillis());
+ if (!success) {
+ throw new IOException("Unable to set the last modification time for " + file);
}
}
/**
- * Schedules a file to be deleted when JVM exits.
- * If file is directory delete it and all sub-directories.
+ * Converts each of an array of <code>File</code> to a <code>URL</code>.
+ * <p>
+ * Returns an array of the same size as the input.
+ * </p>
*
- * @param file file or directory to delete, must not be {@code null}
- * @throws NullPointerException if the file is {@code null}
- * @throws IOException in case deletion is unsuccessful
+ * @param files the files to convert, must not be {@code null}
+ * @return an array of URLs matching the input
+ * @throws IOException if a file cannot be converted
+ * @throws NullPointerException if the parameter is null
*/
- public static void forceDeleteOnExit(final File file) throws IOException {
- if (file.isDirectory()) {
- deleteDirectoryOnExit(file);
- } else {
- file.deleteOnExit();
+ public static URL[] toURLs(final File... files) throws IOException {
+ final URL[] urls = new URL[files.length];
+
+ for (int i = 0; i < urls.length; i++) {
+ urls[i] = files[i].toURI().toURL();
}
+
+ return urls;
}
/**
- * Schedules a directory recursively for deletion on JVM exit.
+ * Validates the given arguments.
+ * <ul>
+ * <li>Throws {@link IllegalArgumentException} if {@code directory} is not a directory</li>
+ * <li>Throws {@link NullPointerException} if {@code fileFilter} is null</li>
+ * </ul>
*
- * @param directory directory to delete, must not be {@code null}
- * @throws NullPointerException if the directory is {@code null}
- * @throws IOException in case deletion is unsuccessful
+ * @param directory The File to test
+ * @param fileFilter The IOFileFilter to test
*/
- private static void deleteDirectoryOnExit(final File directory) throws IOException {
- if (!directory.exists()) {
- return;
+ private static void validateListFilesParameters(final File directory, final IOFileFilter fileFilter) {
+ if (!directory.isDirectory()) {
+ throw new IllegalArgumentException("Parameter 'directory' is not a directory: " + directory);
}
+ if (fileFilter == null) {
+ throw new NullPointerException("Parameter 'fileFilter' is null");
+ }
+ }
- directory.deleteOnExit();
- if (!isSymlink(directory)) {
- cleanDirectoryOnExit(directory);
+ /**
+ * Validates the given arguments.
+ * <ul>
+ * <li>Throws {@link NullPointerException} if {@code src} is null</li>
+ * <li>Throws {@link NullPointerException} if {@code dest} is null</li>
+ * <li>Throws {@link FileNotFoundException} if {@code src} does not exist</li>
+ * </ul>
+ *
+ * @param src the file or directory to be moved
+ * @param dest the destination file or directory
+ * @throws FileNotFoundException if {@code src} file does not exist
+ */
+ private static void validateMoveParameters(final File src, final File dest) throws FileNotFoundException {
+ if (src == null) {
+ throw new NullPointerException("Source must not be null");
+ }
+ if (dest == null) {
+ throw new NullPointerException("Destination must not be null");
+ }
+ if (!src.exists()) {
+ throw new FileNotFoundException("Source '" + src + "' does not exist");
}
}
/**
- * Cleans a directory without deleting it.
+ * Lists files in a directory, asserting that the supplied directory satisfies exists and is a directory.
*
- * @param directory directory to clean, must not be {@code null}
- * @throws NullPointerException if the directory is {@code null}
- * @throws IOException in case cleaning is unsuccessful
+ * @param directory The directory to list
+ * @return The files in the directory, never null.
+ * @throws IOException if an I/O error occurs
*/
- private static void cleanDirectoryOnExit(final File directory) throws IOException {
- final File[] files = verifiedListFiles(directory);
+ private static File[] verifiedListFiles(final File directory) throws IOException {
+ if (!directory.exists()) {
+ final String message = directory + " does not exist";
+ throw new IllegalArgumentException(message);
+ }
- IOException exception = null;
- for (final File file : files) {
- try {
- forceDeleteOnExit(file);
- } catch (final IOException ioe) {
- exception = ioe;
- }
+ if (!directory.isDirectory()) {
+ final String message = directory + " is not a directory";
+ throw new IllegalArgumentException(message);
}
- if (null != exception) {
- throw exception;
+ final File[] files = directory.listFiles();
+ if (files == null) { // null if security restricted
+ throw new IOException("Failed to list contents of " + directory);
}
+ return files;
}
+ //-----------------------------------------------------------------------
/**
- * Makes a directory, including any necessary but nonexistent parent
- * directories. If a file already exists with specified name but it is
- * not a directory then an IOException is thrown.
- * If the directory cannot be created (or the file already exists but is not a directory)
- * then an IOException is thrown.
+ * Waits for NFS to propagate a file creation, imposing a timeout.
+ * <p>
+ * This method repeatedly tests {@link File#exists()} until it returns
+ * true up to the maximum time specified in seconds.
+ * </p>
*
- * @param directory directory to create, must not be {@code null}
- * @throws NullPointerException if the directory is {@code null}
- * @throws IOException if the directory cannot be created or the file already exists but is not a directory
+ * @param file the file to check, must not be {@code null}
+ * @param seconds the maximum time in seconds to wait
+ * @return true if file exists
+ * @throws NullPointerException if the file is {@code null}
*/
- public static void forceMkdir(final File directory) throws IOException {
- if (directory.exists()) {
- if (!directory.isDirectory()) {
- final String message =
- "File "
- + directory
- + " exists and is "
- + "not a directory. Unable to create directory.";
- throw new IOException(message);
- }
- } else {
- if (!directory.mkdirs()) {
- // Double-check that some other thread or process hasn't made
- // the directory in the background
- if (!directory.isDirectory()) {
- final String message =
- "Unable to create directory " + directory;
- throw new IOException(message);
+ public static boolean waitFor(final File file, final int seconds) {
+ final long finishAt = System.currentTimeMillis() + (seconds * 1000L);
+ boolean wasInterrupted = false;
+ try {
+ while (!file.exists()) {
+ final long remaining = finishAt - System.currentTimeMillis();
+ if (remaining < 0){
+ return false;
+ }
+ try {
+ Thread.sleep(Math.min(100, remaining));
+ } catch (final InterruptedException ignore) {
+ wasInterrupted = true;
+ } catch (final Exception ex) {
+ break;
}
}
+ } finally {
+ if (wasInterrupted) {
+ Thread.currentThread().interrupt();
+ }
}
+ return true;
}
/**
- * Makes any necessary but nonexistent parent directories for a given File. If the parent directory cannot be
- * created then an IOException is thrown.
+ * Writes a CharSequence to a file creating the file if it does not exist using the default encoding for the VM.
*
- * @param file file with parent to create, must not be {@code null}
- * @throws NullPointerException if the file is {@code null}
- * @throws IOException if the parent directory cannot be created
- * @since 2.5
+ * @param file the file to write
+ * @param data the content to write to the file
+ * @throws IOException in case of an I/O error
+ * @since 2.0
+ * @deprecated 2.5 use {@link #write(File, CharSequence, Charset)} instead (and specify the appropriate encoding)
*/
- public static void forceMkdirParent(final File file) throws IOException {
- final File parent = file.getParentFile();
- if (parent == null) {
- return;
- }
- forceMkdir(parent);
+ @Deprecated
+ public static void write(final File file, final CharSequence data) throws IOException {
+ write(file, data, Charset.defaultCharset(), false);
}
- //-----------------------------------------------------------------------
/**
- * Returns the size of the specified file or directory. If the provided
- * {@link 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.
- * <p>
- * Note that overflow is not detected, and the return value may be negative if
- * overflow occurs. See {@link #sizeOfAsBigInteger(File)} for an alternative
- * method that does not overflow.
- * </p>
- *
- * @param file the regular file or directory to return the size
- * of (must not be {@code null}).
- *
- * @return the length of the file, or recursive size of the directory,
- * provided (in bytes).
- *
- * @throws NullPointerException if the file is {@code null}
- * @throws IllegalArgumentException if the file does not exist.
+ * Writes a CharSequence to a file creating the file if it does not exist using the default encoding for the VM.
*
- * @since 2.0
+ * @param file the file to write
+ * @param data the content to write to the file
+ * @param append if {@code true}, then the data will be added to the
+ * end of the file rather than overwriting
+ * @throws IOException in case of an I/O error
+ * @since 2.1
+ * @deprecated 2.5 use {@link #write(File, CharSequence, Charset, boolean)} instead (and specify the appropriate encoding)
*/
- public static long sizeOf(final File file) {
-
- if (!file.exists()) {
- final String message = file + " does not exist";
- throw new IllegalArgumentException(message);
- }
-
- if (file.isDirectory()) {
- return sizeOfDirectory0(file); // private method; expects directory
- }
- return file.length();
-
+ @Deprecated
+ public static void write(final File file, final CharSequence data, final boolean append) throws IOException {
+ write(file, data, Charset.defaultCharset(), append);
}
/**
- * Returns the size of the specified file or directory. If the provided
- * {@link 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.
- *
- * @param file the regular file or directory to return the size
- * of (must not be {@code null}).
- *
- * @return the length of the file, or recursive size of the directory,
- * provided (in bytes).
- *
- * @throws NullPointerException if the file is {@code null}
- * @throws IllegalArgumentException if the file does not exist.
+ * Writes a CharSequence to a file creating the file if it does not exist.
*
- * @since 2.4
+ * @param file the file to write
+ * @param data the content to write to the file
+ * @param encoding the encoding to use, {@code null} means platform default
+ * @throws IOException in case of an I/O error
+ * @since 2.3
*/
- public static BigInteger sizeOfAsBigInteger(final File file) {
-
- if (!file.exists()) {
- final String message = file + " does not exist";
- throw new IllegalArgumentException(message);
- }
-
- if (file.isDirectory()) {
- return sizeOfDirectoryBig0(file); // internal method
- }
- return BigInteger.valueOf(file.length());
-
+ public static void write(final File file, final CharSequence data, final Charset encoding) throws IOException {
+ write(file, data, encoding, false);
}
/**
- * Counts the size of a directory recursively (sum of the length of all files).
- * <p>
- * Note that overflow is not detected, and the return value may be negative if
- * overflow occurs. See {@link #sizeOfDirectoryAsBigInteger(File)} for an alternative
- * method that does not overflow.
- * </p>
+ * Writes a CharSequence to a file creating the file if it does not exist.
*
- * @param directory directory to inspect, must not be {@code null}
- * @return size of directory in bytes, 0 if directory is security restricted, a negative number when the real total
- * is greater than {@link Long#MAX_VALUE}.
- * @throws NullPointerException if the directory is {@code null}
+ * @param file the file to write
+ * @param data the content to write to the file
+ * @param encoding the encoding to use, {@code null} means platform default
+ * @param append if {@code true}, then the data will be added to the
+ * end of the file rather than overwriting
+ * @throws IOException in case of an I/O error
+ * @since 2.3
*/
- public static long sizeOfDirectory(final File directory) {
- checkDirectory(directory);
- return sizeOfDirectory0(directory);
+ public static void write(final File file, final CharSequence data, final Charset encoding, final boolean append)
+ throws IOException {
+ final String str = data == null ? null : data.toString();
+ writeStringToFile(file, str, encoding, append);
}
// Private method, must be invoked will a directory parameter
/**
- * the size of a director
- * @param directory the directory to check
- * @return the size
+ * Writes a CharSequence to a file creating the file if it does not exist.
+ *
+ * @param file the file to write
+ * @param data the content to write to the file
+ * @param encoding the encoding to use, {@code null} means platform default
+ * @throws IOException in case of an I/O error
+ * @throws java.io.UnsupportedEncodingException if the encoding is not supported by the VM
+ * @since 2.0
*/
- private static long sizeOfDirectory0(final File directory) {
- final File[] files = directory.listFiles();
- if (files == null) { // null if security restricted
- return 0L;
- }
- long size = 0;
-
- for (final File file : files) {
- if (!isSymlink(file)) {
- size += sizeOf0(file); // internal method
- if (size < 0) {
- break;
- }
- }
- }
-
- return size;
+ public static void write(final File file, final CharSequence data, final String encoding) throws IOException {
+ write(file, data, encoding, false);
}
// Internal method - does not check existence
/**
- * the size of a file
- * @param file the file to check
- * @return the size of the file
+ * Writes a CharSequence to a file creating the file if it does not exist.
+ *
+ * @param file the file to write
+ * @param data the content to write to the file
+ * @param encoding the encoding to use, {@code null} means platform default
+ * @param append if {@code true}, then the data will be added to the
+ * end of the file rather than overwriting
+ * @throws IOException in case of an I/O error
+ * @throws java.nio.charset.UnsupportedCharsetException thrown instead of {@link java.io
+ * .UnsupportedEncodingException} in version 2.2 if the encoding is not supported by the VM
+ * @since 2.1
*/
- private static long sizeOf0(final File file) {
- if (file.isDirectory()) {
- return sizeOfDirectory0(file);
- }
- return file.length(); // will be 0 if file does not exist
+ public static void write(final File file, final CharSequence data, final String encoding, final boolean append)
+ throws IOException {
+ write(file, data, Charsets.toCharset(encoding), append);
}
/**
- * Counts the size of a directory recursively (sum of the length of all files).
+ * Writes a byte array to a file creating the file if it does not exist.
+ * <p>
+ * NOTE: As from v1.3, the parent directories of the file will be created
+ * if they do not exist.
+ * </p>
*
- * @param directory directory to inspect, must not be {@code null}
- * @return size of directory in bytes, 0 if directory is security restricted.
- * @throws NullPointerException if the directory is {@code null}
- * @since 2.4
+ * @param file the file to write to
+ * @param data the content to write to the file
+ * @throws IOException in case of an I/O error
+ * @since 1.1
*/
- public static BigInteger sizeOfDirectoryAsBigInteger(final File directory) {
- checkDirectory(directory);
- return sizeOfDirectoryBig0(directory);
+ public static void writeByteArrayToFile(final File file, final byte[] data) throws IOException {
+ writeByteArrayToFile(file, data, false);
}
// Must be called with a directory
/**
- * Finds the size of a directory
+ * Writes a byte array to a file creating the file if it does not exist.
*
- * @param directory The directory
- * @return the size
+ * @param file the file to write to
+ * @param data the content to write to the file
+ * @param append if {@code true}, then bytes will be added to the
+ * end of the file rather than overwriting
+ * @throws IOException in case of an I/O error
+ * @since 2.1
*/
- private static BigInteger sizeOfDirectoryBig0(final File directory) {
- final File[] files = directory.listFiles();
- if (files == null) { // null if security restricted
- return BigInteger.ZERO;
- }
- BigInteger size = BigInteger.ZERO;
-
- for (final File file : files) {
- if (!isSymlink(file)) {
- size = size.add(sizeOfBig0(file));
- }
- }
-
- return size;
+ public static void writeByteArrayToFile(final File file, final byte[] data, final boolean append)
+ throws IOException {
+ writeByteArrayToFile(file, data, 0, data.length, append);
}
// internal method; if file does not exist will return 0
/**
- * Returns the size of a file
- * @param fileOrDir The file
- * @return the size
- */
- private static BigInteger sizeOfBig0(final File fileOrDir) {
- if (fileOrDir.isDirectory()) {
- return sizeOfDirectoryBig0(fileOrDir);
- }
- return BigInteger.valueOf(fileOrDir.length());
+ * Writes {@code len} bytes from the specified byte array starting
+ * at offset {@code off} to a file, creating the file if it does
+ * not exist.
+ *
+ * @param file the file to write to
+ * @param data the content to write to the file
+ * @param off the start offset in the data
+ * @param len the number of bytes to write
+ * @throws IOException in case of an I/O error
+ * @since 2.5
+ */
+ public static void writeByteArrayToFile(final File file, final byte[] data, final int off, final int len)
+ throws IOException {
+ writeByteArrayToFile(file, data, off, len, false);
}
/**
- * Checks that the given {@code File} exists and is a directory.
+ * Writes {@code len} bytes from the specified byte array starting
+ * at offset {@code off} to a file, creating the file if it does
+ * not exist.
*
- * @param directory The {@code File} to check.
- * @throws IllegalArgumentException if the given {@code File} does not exist or is not a directory.
+ * @param file the file to write to
+ * @param data the content to write to the file
+ * @param off the start offset in the data
+ * @param len the number of bytes to write
+ * @param append if {@code true}, then bytes will be added to the
+ * end of the file rather than overwriting
+ * @throws IOException in case of an I/O error
+ * @since 2.5
*/
- private static void checkDirectory(final File directory) {
- if (!directory.exists()) {
- throw new IllegalArgumentException(directory + " does not exist");
- }
- if (!directory.isDirectory()) {
- throw new IllegalArgumentException(directory + " is not a directory");
+ public static void writeByteArrayToFile(final File file, final byte[] data, final int off, final int len,
+ final boolean append) throws IOException {
+ try (OutputStream out = openOutputStream(file, append)) {
+ out.write(data, off, len);
}
}
- //-----------------------------------------------------------------------
/**
- * Tests if the specified <code>File</code> is newer than the reference
- * <code>File</code>.
+ * Writes the <code>toString()</code> value of each item in a collection to
+ * the specified <code>File</code> line by line.
+ * The default VM encoding and the default line ending will be used.
*
- * @param file the <code>File</code> of which the modification date must
- * be compared, must not be {@code null}
- * @param reference the <code>File</code> of which the modification date
- * is used, must not be {@code null}
- * @return true if the <code>File</code> exists and has been modified more
- * recently than the reference <code>File</code>
- * @throws IllegalArgumentException if the file is {@code null}
- * @throws IllegalArgumentException if the reference file is {@code null} or doesn't exist
+ * @param file the file to write to
+ * @param lines the lines to write, {@code null} entries produce blank lines
+ * @throws IOException in case of an I/O error
+ * @since 1.3
*/
- public static boolean isFileNewer(final File file, final File reference) {
- if (reference == null) {
- throw new IllegalArgumentException("No specified reference file");
- }
- if (!reference.exists()) {
- throw new IllegalArgumentException("The reference file '"
- + reference + "' doesn't exist");
- }
- return isFileNewer(file, reference.lastModified());
+ public static void writeLines(final File file, final Collection<?> lines) throws IOException {
+ writeLines(file, null, lines, null, false);
}
/**
- * Tests if the specified <code>File</code> is newer than the specified
- * <code>Date</code>.
+ * Writes the <code>toString()</code> value of each item in a collection to
+ * the specified <code>File</code> line by line.
+ * The default VM encoding and the default line ending will be used.
*
- * @param file the <code>File</code> of which the modification date
- * must be compared, must not be {@code null}
- * @param date the date reference, must not be {@code null}
- * @return true if the <code>File</code> exists and has been modified
- * after the given <code>Date</code>.
- * @throws IllegalArgumentException if the file is {@code null}
- * @throws IllegalArgumentException if the date is {@code null}
+ * @param file the file to write to
+ * @param lines the lines to write, {@code null} entries produce blank lines
+ * @param append if {@code true}, then the lines will be added to the
+ * end of the file rather than overwriting
+ * @throws IOException in case of an I/O error
+ * @since 2.1
*/
- public static boolean isFileNewer(final File file, final Date date) {
- if (date == null) {
- throw new IllegalArgumentException("No specified date");
- }
- return isFileNewer(file, date.getTime());
+ public static void writeLines(final File file, final Collection<?> lines, final boolean append) throws IOException {
+ writeLines(file, null, lines, null, append);
}
/**
- * Tests if the specified <code>File</code> is newer than the specified
- * time reference.
+ * Writes the <code>toString()</code> value of each item in a collection to
+ * the specified <code>File</code> line by line.
+ * The default VM encoding and the specified line ending will be used.
*
- * @param file the <code>File</code> of which the modification date must
- * be compared, must not be {@code null}
- * @param timeMillis the time reference measured in milliseconds since the
- * epoch (00:00:00 GMT, January 1, 1970)
- * @return true if the <code>File</code> exists and has been modified after
- * the given time reference.
- * @throws IllegalArgumentException if the file is {@code null}
+ * @param file the file to write to
+ * @param lines the lines to write, {@code null} entries produce blank lines
+ * @param lineEnding the line separator to use, {@code null} is system default
+ * @throws IOException in case of an I/O error
+ * @since 1.3
*/
- public static boolean isFileNewer(final File file, final long timeMillis) {
- if (file == null) {
- throw new IllegalArgumentException("No specified file");
- }
- if (!file.exists()) {
- return false;
- }
- return file.lastModified() > timeMillis;
+ public static void writeLines(final File file, final Collection<?> lines, final String lineEnding)
+ throws IOException {
+ writeLines(file, null, lines, lineEnding, false);
}
- //-----------------------------------------------------------------------
/**
- * Tests if the specified <code>File</code> is older than the reference
- * <code>File</code>.
+ * Writes the <code>toString()</code> value of each item in a collection to
+ * the specified <code>File</code> line by line.
+ * The default VM encoding and the specified line ending will be used.
*
- * @param file the <code>File</code> of which the modification date must
- * be compared, must not be {@code null}
- * @param reference the <code>File</code> of which the modification date
- * is used, must not be {@code null}
- * @return true if the <code>File</code> exists and has been modified before
- * the reference <code>File</code>
- * @throws IllegalArgumentException if the file is {@code null}
- * @throws IllegalArgumentException if the reference file is {@code null} or doesn't exist
+ * @param file the file to write to
+ * @param lines the lines to write, {@code null} entries produce blank lines
+ * @param lineEnding the line separator to use, {@code null} is system default
+ * @param append if {@code true}, then the lines will be added to the
+ * end of the file rather than overwriting
+ * @throws IOException in case of an I/O error
+ * @since 2.1
*/
- public static boolean isFileOlder(final File file, final File reference) {
- if (reference == null) {
- throw new IllegalArgumentException("No specified reference file");
- }
- if (!reference.exists()) {
- throw new IllegalArgumentException("The reference file '"
- + reference + "' doesn't exist");
- }
- return isFileOlder(file, reference.lastModified());
+ public static void writeLines(final File file, final Collection<?> lines, final String lineEnding,
+ final boolean append) throws IOException {
+ writeLines(file, null, lines, lineEnding, append);
}
/**
- * Tests if the specified <code>File</code> is older than the specified
- * <code>Date</code>.
+ * Writes the <code>toString()</code> value of each item in a collection to
+ * the specified <code>File</code> line by line.
+ * The specified character encoding and the default line ending will be used.
+ * <p>
+ * NOTE: As from v1.3, the parent directories of the file will be created
+ * if they do not exist.
+ * </p>
*
- * @param file the <code>File</code> of which the modification date
- * must be compared, must not be {@code null}
- * @param date the date reference, must not be {@code null}
- * @return true if the <code>File</code> exists and has been modified
- * before the given <code>Date</code>.
- * @throws IllegalArgumentException if the file is {@code null}
- * @throws IllegalArgumentException if the date is {@code null}
+ * @param file the file to write to
+ * @param encoding the encoding to use, {@code null} means platform default
+ * @param lines the lines to write, {@code null} entries produce blank lines
+ * @throws IOException in case of an I/O error
+ * @throws java.io.UnsupportedEncodingException if the encoding is not supported by the VM
+ * @since 1.1
*/
- public static boolean isFileOlder(final File file, final Date date) {
- if (date == null) {
- throw new IllegalArgumentException("No specified date");
- }
- return isFileOlder(file, date.getTime());
+ public static void writeLines(final File file, final String encoding, final Collection<?> lines)
+ throws IOException {
+ writeLines(file, encoding, lines, null, false);
}
/**
- * Tests if the specified <code>File</code> is older than the specified
- * time reference.
+ * Writes the <code>toString()</code> value of each item in a collection to
+ * the specified <code>File</code> line by line, optionally appending.
+ * The specified character encoding and the default line ending will be used.
*
- * @param file the <code>File</code> of which the modification date must
- * be compared, must not be {@code null}
- * @param timeMillis the time reference measured in milliseconds since the
- * epoch (00:00:00 GMT, January 1, 1970)
- * @return true if the <code>File</code> exists and has been modified before
- * the given time reference.
- * @throws IllegalArgumentException if the file is {@code null}
+ * @param file the file to write to
+ * @param encoding the encoding to use, {@code null} means platform default
+ * @param lines the lines to write, {@code null} entries produce blank lines
+ * @param append if {@code true}, then the lines will be added to the
+ * end of the file rather than overwriting
+ * @throws IOException in case of an I/O error
+ * @throws java.io.UnsupportedEncodingException if the encoding is not supported by the VM
+ * @since 2.1
*/
- public static boolean isFileOlder(final File file, final long timeMillis) {
- if (file == null) {
- throw new IllegalArgumentException("No specified file");
- }
- if (!file.exists()) {
- return false;
- }
- return file.lastModified() < timeMillis;
+ public static void writeLines(final File file, final String encoding, final Collection<?> lines,
+ final boolean append) throws IOException {
+ writeLines(file, encoding, lines, null, append);
}
- //-----------------------------------------------------------------------
/**
- * Computes the checksum of a file using the CRC32 checksum routine.
- * The value of the checksum is returned.
+ * Writes the <code>toString()</code> value of each item in a collection to
+ * the specified <code>File</code> line by line.
+ * The specified character encoding and the line ending will be used.
+ * <p>
+ * NOTE: As from v1.3, the parent directories of the file will be created
+ * if they do not exist.
+ * </p>
*
- * @param file the file to checksum, must not be {@code null}
- * @return the checksum value
- * @throws NullPointerException if the file or checksum is {@code null}
- * @throws IllegalArgumentException if the file is a directory
- * @throws IOException if an IO error occurs reading the file
- * @since 1.3
+ * @param file the file to write to
+ * @param encoding the encoding to use, {@code null} means platform default
+ * @param lines the lines to write, {@code null} entries produce blank lines
+ * @param lineEnding the line separator to use, {@code null} is system default
+ * @throws IOException in case of an I/O error
+ * @throws java.io.UnsupportedEncodingException if the encoding is not supported by the VM
+ * @since 1.1
*/
- public static long checksumCRC32(final File file) throws IOException {
- final CRC32 crc = new CRC32();
- checksum(file, crc);
- return crc.getValue();
+ public static void writeLines(final File file, final String encoding, final Collection<?> lines,
+ final String lineEnding) throws IOException {
+ writeLines(file, encoding, lines, lineEnding, false);
}
/**
- * Computes the checksum of a file using the specified checksum object.
- * Multiple files may be checked using one <code>Checksum</code> instance
- * if desired simply by reusing the same checksum object.
- * For example:
- * <pre>
- * long csum = FileUtils.checksum(file, new CRC32()).getValue();
- * </pre>
+ * Writes the <code>toString()</code> value of each item in a collection to
+ * the specified <code>File</code> line by line.
+ * The specified character encoding and the line ending will be used.
*
- * @param file the file to checksum, must not be {@code null}
- * @param checksum the checksum object to be used, must not be {@code null}
- * @return the checksum specified, updated with the content of the file
- * @throws NullPointerException if the file or checksum is {@code null}
- * @throws IllegalArgumentException if the file is a directory
- * @throws IOException if an IO error occurs reading the file
- * @since 1.3
+ * @param file the file to write to
+ * @param encoding the encoding to use, {@code null} means platform default
+ * @param lines the lines to write, {@code null} entries produce blank lines
+ * @param lineEnding the line separator to use, {@code null} is system default
+ * @param append if {@code true}, then the lines will be added to the
+ * end of the file rather than overwriting
+ * @throws IOException in case of an I/O error
+ * @throws java.io.UnsupportedEncodingException if the encoding is not supported by the VM
+ * @since 2.1
*/
- public static Checksum checksum(final File file, final Checksum checksum) throws IOException {
- if (file.isDirectory()) {
- throw new IllegalArgumentException("Checksums can't be computed on directories");
- }
- try (InputStream in = new CheckedInputStream(new FileInputStream(file), checksum)) {
- IOUtils.copy(in, new NullOutputStream());
+ public static void writeLines(final File file, final String encoding, final Collection<?> lines,
+ final String lineEnding, final boolean append) throws IOException {
+ try (OutputStream out = new BufferedOutputStream(openOutputStream(file, append))) {
+ IOUtils.writeLines(lines, lineEnding, out, encoding);
}
- return checksum;
}
/**
- * Moves a directory.
- * <p>
- * When the destination directory is on another file system, do a "copy and delete".
- * </p>
+ * Writes a String to a file creating the file if it does not exist using the default encoding for the VM.
*
- * @param srcDir the directory to be moved
- * @param destDir the destination directory
- * @throws NullPointerException if source or destination is {@code null}
- * @throws FileExistsException if the destination directory exists
- * @throws IOException if source or destination is invalid
- * @throws IOException if an IO error occurs moving the file
- * @since 1.4
+ * @param file the file to write
+ * @param data the content to write to the file
+ * @throws IOException in case of an I/O error
+ * @deprecated 2.5 use {@link #writeStringToFile(File, String, Charset)} instead (and specify the appropriate encoding)
*/
- public static void moveDirectory(final File srcDir, final File destDir) throws IOException {
- validateMoveParameters(srcDir, destDir);
- if (!srcDir.isDirectory()) {
- throw new IOException("Source '" + srcDir + "' is not a directory");
- }
- if (destDir.exists()) {
- throw new FileExistsException("Destination '" + destDir + "' already exists");
- }
- final boolean rename = srcDir.renameTo(destDir);
- if (!rename) {
- if (destDir.getCanonicalPath().startsWith(srcDir.getCanonicalPath() + File.separator)) {
- throw new IOException("Cannot move directory: " + srcDir + " to a subdirectory of itself: " + destDir);
- }
- copyDirectory(srcDir, destDir);
- deleteDirectory(srcDir);
- if (srcDir.exists()) {
- throw new IOException("Failed to delete original directory '" + srcDir +
- "' after copy to '" + destDir + "'");
- }
- }
+ @Deprecated
+ public static void writeStringToFile(final File file, final String data) throws IOException {
+ writeStringToFile(file, data, Charset.defaultCharset(), false);
}
/**
- * Moves a directory to another directory.
+ * Writes a String to a file creating the file if it does not exist using the default encoding for the VM.
*
- * @param src the file to be moved
- * @param destDir the destination file
- * @param createDestDir If {@code true} create the destination directory,
- * otherwise if {@code false} throw an IOException
- * @throws NullPointerException if source or destination is {@code null}
- * @throws FileExistsException if the directory exists in the destination directory
- * @throws IOException if source or destination is invalid
- * @throws IOException if an IO error occurs moving the file
- * @since 1.4
+ * @param file the file to write
+ * @param data the content to write to the file
+ * @param append if {@code true}, then the String will be added to the
+ * end of the file rather than overwriting
+ * @throws IOException in case of an I/O error
+ * @since 2.1
+ * @deprecated 2.5 use {@link #writeStringToFile(File, String, Charset, boolean)} instead (and specify the appropriate encoding)
*/
- public static void moveDirectoryToDirectory(final File src, final File destDir, final boolean createDestDir)
- throws IOException {
- validateMoveParameters(src, destDir);
- if (!destDir.exists() && createDestDir) {
- destDir.mkdirs();
- }
- if (!destDir.exists()) {
- throw new FileNotFoundException("Destination directory '" + destDir +
- "' does not exist [createDestDir=" + createDestDir + "]");
- }
- if (!destDir.isDirectory()) {
- throw new IOException("Destination '" + destDir + "' is not a directory");
- }
- moveDirectory(src, new File(destDir, src.getName()));
+ @Deprecated
+ public static void writeStringToFile(final File file, final String data, final boolean append) throws IOException {
+ writeStringToFile(file, data, Charset.defaultCharset(), append);
}
/**
- * Moves a file.
+ * Writes a String to a file creating the file if it does not exist.
* <p>
- * When the destination file is on another file system, do a "copy and delete".
+ * NOTE: As from v1.3, the parent directories of the file will be created
+ * if they do not exist.
* </p>
*
- * @param srcFile the file to be moved
- * @param destFile the destination file
- * @throws NullPointerException if source or destination is {@code null}
- * @throws FileExistsException if the destination file exists
- * @throws IOException if source or destination is invalid
- * @throws IOException if an IO error occurs moving the file
- * @since 1.4
+ * @param file the file to write
+ * @param data the content to write to the file
+ * @param encoding the encoding to use, {@code null} means platform default
+ * @throws IOException in case of an I/O error
+ * @throws java.io.UnsupportedEncodingException if the encoding is not supported by the VM
+ * @since 2.4
*/
- public static void moveFile(final File srcFile, final File destFile) throws IOException {
- validateMoveParameters(srcFile, destFile);
- if (srcFile.isDirectory()) {
- throw new IOException("Source '" + srcFile + "' is a directory");
- }
- if (destFile.exists()) {
- throw new FileExistsException("Destination '" + destFile + "' already exists");
- }
- if (destFile.isDirectory()) {
- throw new IOException("Destination '" + destFile + "' is a directory");
- }
- final boolean rename = srcFile.renameTo(destFile);
- if (!rename) {
- copyFile(srcFile, destFile);
- if (!srcFile.delete()) {
- FileUtils.deleteQuietly(destFile);
- throw new IOException("Failed to delete original file '" + srcFile +
- "' after copy to '" + destFile + "'");
- }
- }
+ public static void writeStringToFile(final File file, final String data, final Charset encoding)
+ throws IOException {
+ writeStringToFile(file, data, encoding, false);
}
/**
- * Moves a file to a directory.
+ * Writes a String to a file creating the file if it does not exist.
*
- * @param srcFile the file to be moved
- * @param destDir the destination file
- * @param createDestDir If {@code true} create the destination directory,
- * otherwise if {@code false} throw an IOException
- * @throws NullPointerException if source or destination is {@code null}
- * @throws FileExistsException if the destination file exists
- * @throws IOException if source or destination is invalid
- * @throws IOException if an IO error occurs moving the file
- * @since 1.4
+ * @param file the file to write
+ * @param data the content to write to the file
+ * @param encoding the encoding to use, {@code null} means platform default
+ * @param append if {@code true}, then the String will be added to the
+ * end of the file rather than overwriting
+ * @throws IOException in case of an I/O error
+ * @since 2.3
*/
- public static void moveFileToDirectory(final File srcFile, final File destDir, final boolean createDestDir)
- throws IOException {
- validateMoveParameters(srcFile, destDir);
- if (!destDir.exists() && createDestDir) {
- destDir.mkdirs();
- }
- if (!destDir.exists()) {
- throw new FileNotFoundException("Destination directory '" + destDir +
- "' does not exist [createDestDir=" + createDestDir + "]");
- }
- if (!destDir.isDirectory()) {
- throw new IOException("Destination '" + destDir + "' is not a directory");
+ public static void writeStringToFile(final File file, final String data, final Charset encoding,
+ final boolean append) throws IOException {
+ try (OutputStream out = openOutputStream(file, append)) {
+ IOUtils.write(data, out, encoding);
}
- moveFile(srcFile, new File(destDir, srcFile.getName()));
}
/**
- * Moves a file or directory to the destination directory.
+ * Writes a String to a file creating the file if it does not exist.
* <p>
- * When the destination is on another file system, do a "copy and delete".
+ * NOTE: As from v1.3, the parent directories of the file will be created
+ * if they do not exist.
* </p>
*
- * @param src the file or directory to be moved
- * @param destDir the destination directory
- * @param createDestDir If {@code true} create the destination directory,
- * otherwise if {@code false} throw an IOException
- * @throws NullPointerException if source or destination is {@code null}
- * @throws FileExistsException if the directory or file exists in the destination directory
- * @throws IOException if source or destination is invalid
- * @throws IOException if an IO error occurs moving the file
- * @since 1.4
+ * @param file the file to write
+ * @param data the content to write to the file
+ * @param encoding the encoding to use, {@code null} means platform default
+ * @throws IOException in case of an I/O error
+ * @throws java.io.UnsupportedEncodingException if the encoding is not supported by the VM
*/
- public static void moveToDirectory(final File src, final File destDir, final boolean createDestDir)
- throws IOException {
- validateMoveParameters(src, destDir);
- if (src.isDirectory()) {
- moveDirectoryToDirectory(src, destDir, createDestDir);
- } else {
- moveFileToDirectory(src, destDir, createDestDir);
- }
+ public static void writeStringToFile(final File file, final String data, final String encoding) throws IOException {
+ writeStringToFile(file, data, encoding, false);
}
/**
- * Validates the given arguments.
- * <ul>
- * <li>Throws {@link NullPointerException} if {@code src} is null</li>
- * <li>Throws {@link NullPointerException} if {@code dest} is null</li>
- * <li>Throws {@link FileNotFoundException} if {@code src} does not exist</li>
- * </ul>
+ * Writes a String to a file creating the file if it does not exist.
*
- * @param src the file or directory to be moved
- * @param dest the destination file or directory
- * @throws FileNotFoundException if {@code src} file does not exist
+ * @param file the file to write
+ * @param data the content to write to the file
+ * @param encoding the encoding to use, {@code null} means platform default
+ * @param append if {@code true}, then the String will be added to the
+ * end of the file rather than overwriting
+ * @throws IOException in case of an I/O error
+ * @throws java.nio.charset.UnsupportedCharsetException thrown instead of {@link java.io
+ * .UnsupportedEncodingException} in version 2.2 if the encoding is not supported by the VM
+ * @since 2.1
*/
- private static void validateMoveParameters(final File src, final File dest) throws FileNotFoundException {
- if (src == null) {
- throw new NullPointerException("Source must not be null");
- }
- if (dest == null) {
- throw new NullPointerException("Destination must not be null");
- }
- if (!src.exists()) {
- throw new FileNotFoundException("Source '" + src + "' does not exist");
- }
+ public static void writeStringToFile(final File file, final String data, final String encoding,
+ final boolean append) throws IOException {
+ writeStringToFile(file, data, Charsets.toCharset(encoding), append);
}
/**
- * Determines whether the specified file is a Symbolic Link rather than an actual file.
- * <p>
- * Will not return true if there is a Symbolic Link anywhere in the path,
- * only if the specific file is.
- * </p>
- * <p>
- * When using jdk1.7, this method delegates to {@code boolean java.nio.file.Files.isSymbolicLink(Path path)}
- * </p>
- *
- * <p>
- * <b>Note:</b> the current implementation always returns {@code false} if running on
- * jkd1.6 and the system is detected as Windows using {@link FilenameUtils#isSystemWindows()}
- * </p>
- * <p>
- * For code that runs on Java 1.7 or later, use the following method instead:
- * </p>
- *
- * {@code boolean java.nio.file.Files.isSymbolicLink(Path path)}
- * @param file the file to check
- * @return true if the file is a Symbolic Link
- * @since 2.0
+ * Instances should NOT be constructed in standard programming.
*/
- public static boolean isSymlink(final File file) {
- if (file == null) {
- throw new NullPointerException("File must not be null");
- }
- return Files.isSymbolicLink(file.toPath());
+ public FileUtils() {
+ super();
}
}