|
|
|
@ -33,8 +33,6 @@ import java.net.URLConnection;
|
|
|
|
|
import java.nio.ByteBuffer; |
|
|
|
|
import java.nio.channels.FileChannel; |
|
|
|
|
import java.nio.charset.Charset; |
|
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
|
import java.nio.file.Files; |
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
import java.util.Collection; |
|
|
|
|
import java.util.Date; |
|
|
|
@ -48,6 +46,7 @@ import com.fr.third.org.apache.commons.io.filefilter.DirectoryFileFilter;
|
|
|
|
|
import com.fr.third.org.apache.commons.io.filefilter.FalseFileFilter; |
|
|
|
|
import com.fr.third.org.apache.commons.io.filefilter.FileFilterUtils; |
|
|
|
|
import com.fr.third.org.apache.commons.io.filefilter.IOFileFilter; |
|
|
|
|
import com.fr.third.org.apache.commons.io.filefilter.NameFileFilter; |
|
|
|
|
import com.fr.third.org.apache.commons.io.filefilter.SuffixFileFilter; |
|
|
|
|
import com.fr.third.org.apache.commons.io.filefilter.TrueFileFilter; |
|
|
|
|
import com.fr.third.org.apache.commons.io.output.NullOutputStream; |
|
|
|
@ -69,12 +68,9 @@ import com.fr.third.org.apache.commons.io.output.NullOutputStream;
|
|
|
|
|
* <li>calculating a checksum |
|
|
|
|
* </ul> |
|
|
|
|
* <p> |
|
|
|
|
* Note that a specific charset should be specified whenever possible. |
|
|
|
|
* Relying on the platform default means that the code is Locale-dependent. |
|
|
|
|
* Only use the default if the files are known to always use the platform default. |
|
|
|
|
* <p> |
|
|
|
|
* Origin of code: Excalibur, Alexandria, Commons-Utils |
|
|
|
|
* |
|
|
|
|
* @version $Id: FileUtils.java 1722481 2016-01-01 01:42:04Z dbrosius $ |
|
|
|
|
*/ |
|
|
|
|
public class FileUtils { |
|
|
|
|
|
|
|
|
@ -437,7 +433,8 @@ public class FileUtils {
|
|
|
|
|
*/ |
|
|
|
|
public static void touch(final File file) throws IOException { |
|
|
|
|
if (!file.exists()) { |
|
|
|
|
openOutputStream(file).close(); |
|
|
|
|
final OutputStream out = openOutputStream(file); |
|
|
|
|
IOUtils.closeQuietly(out); |
|
|
|
|
} |
|
|
|
|
final boolean success = file.setLastModified(System.currentTimeMillis()); |
|
|
|
|
if (!success) { |
|
|
|
@ -503,14 +500,13 @@ public class FileUtils {
|
|
|
|
|
* in <code>FileFilterUtils.makeCVSAware(null)</code>. |
|
|
|
|
* |
|
|
|
|
* @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 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 {@link TrueFileFilter#INSTANCE} to match all directories. |
|
|
|
|
* @return a collection of java.io.File with the matching files |
|
|
|
|
* @see com.fr.third.org.apache.commons.io.filefilter.FileFilterUtils |
|
|
|
|
* @see com.fr.third.org.apache.commons.io.filefilter.NameFileFilter |
|
|
|
|
* search. Use TrueFileFilter.INSTANCE to match all directories. |
|
|
|
|
* @return an collection of java.io.File with the matching files |
|
|
|
|
* @see FileFilterUtils |
|
|
|
|
* @see NameFileFilter |
|
|
|
|
*/ |
|
|
|
|
public static Collection<File> listFiles( |
|
|
|
|
final File directory, final IOFileFilter fileFilter, final IOFileFilter dirFilter) { |
|
|
|
@ -520,7 +516,7 @@ public class FileUtils {
|
|
|
|
|
final IOFileFilter effDirFilter = setUpEffectiveDirFilter(dirFilter); |
|
|
|
|
|
|
|
|
|
//Find files
|
|
|
|
|
final Collection<File> files = new java.util.LinkedList<>(); |
|
|
|
|
final Collection<File> files = new java.util.LinkedList<File>(); |
|
|
|
|
innerListFiles(files, directory, |
|
|
|
|
FileFilterUtils.or(effFileFilter, effDirFilter), false); |
|
|
|
|
return files; |
|
|
|
@ -579,10 +575,10 @@ public class FileUtils {
|
|
|
|
|
* @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 com.fr.third.org.apache.commons.io.FileUtils#listFiles |
|
|
|
|
* @see com.fr.third.org.apache.commons.io.filefilter.FileFilterUtils |
|
|
|
|
* @see com.fr.third.org.apache.commons.io.filefilter.NameFileFilter |
|
|
|
|
* @return an collection of java.io.File with the matching files |
|
|
|
|
* @see FileUtils#listFiles |
|
|
|
|
* @see FileFilterUtils |
|
|
|
|
* @see NameFileFilter |
|
|
|
|
* @since 2.2 |
|
|
|
|
*/ |
|
|
|
|
public static Collection<File> listFilesAndDirs( |
|
|
|
@ -593,7 +589,7 @@ public class FileUtils {
|
|
|
|
|
final IOFileFilter effDirFilter = setUpEffectiveDirFilter(dirFilter); |
|
|
|
|
|
|
|
|
|
//Find files
|
|
|
|
|
final Collection<File> files = new java.util.LinkedList<>(); |
|
|
|
|
final Collection<File> files = new java.util.LinkedList<File>(); |
|
|
|
|
if (directory.isDirectory()) { |
|
|
|
|
files.add(directory); |
|
|
|
|
} |
|
|
|
@ -616,8 +612,8 @@ public class FileUtils {
|
|
|
|
|
* 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 com.fr.third.org.apache.commons.io.filefilter.FileFilterUtils |
|
|
|
|
* @see com.fr.third.org.apache.commons.io.filefilter.NameFileFilter |
|
|
|
|
* @see FileFilterUtils |
|
|
|
|
* @see NameFileFilter |
|
|
|
|
* @since 1.2 |
|
|
|
|
*/ |
|
|
|
|
public static Iterator<File> iterateFiles( |
|
|
|
@ -641,8 +637,8 @@ public class FileUtils {
|
|
|
|
|
* 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 com.fr.third.org.apache.commons.io.filefilter.FileFilterUtils |
|
|
|
|
* @see com.fr.third.org.apache.commons.io.filefilter.NameFileFilter |
|
|
|
|
* @see FileFilterUtils |
|
|
|
|
* @see NameFileFilter |
|
|
|
|
* @since 2.2 |
|
|
|
|
*/ |
|
|
|
|
public static Iterator<File> iterateFilesAndDirs(final File directory, final IOFileFilter fileFilter, |
|
|
|
@ -675,7 +671,7 @@ public class FileUtils {
|
|
|
|
|
* @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 |
|
|
|
|
* @return an collection of java.io.File with the matching files |
|
|
|
|
*/ |
|
|
|
|
public static Collection<File> listFiles( |
|
|
|
|
final File directory, final String[] extensions, final boolean recursive) { |
|
|
|
@ -750,9 +746,16 @@ public class FileUtils {
|
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
try (InputStream input1 = new FileInputStream(file1); |
|
|
|
|
InputStream input2 = new FileInputStream(file2)) { |
|
|
|
|
InputStream input1 = null; |
|
|
|
|
InputStream input2 = null; |
|
|
|
|
try { |
|
|
|
|
input1 = new FileInputStream(file1); |
|
|
|
|
input2 = new FileInputStream(file2); |
|
|
|
|
return IOUtils.contentEquals(input1, input2); |
|
|
|
|
|
|
|
|
|
} finally { |
|
|
|
|
IOUtils.closeQuietly(input1); |
|
|
|
|
IOUtils.closeQuietly(input2); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -796,13 +799,22 @@ public class FileUtils {
|
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
try (Reader input1 = charsetName == null |
|
|
|
|
? new InputStreamReader(new FileInputStream(file1), Charset.defaultCharset()) |
|
|
|
|
: new InputStreamReader(new FileInputStream(file1), charsetName); |
|
|
|
|
Reader input2 = charsetName == null |
|
|
|
|
? new InputStreamReader(new FileInputStream(file2), Charset.defaultCharset()) |
|
|
|
|
: new InputStreamReader(new FileInputStream(file2), charsetName)) { |
|
|
|
|
Reader input1 = null; |
|
|
|
|
Reader input2 = null; |
|
|
|
|
try { |
|
|
|
|
if (charsetName == null) { |
|
|
|
|
// N.B. make explicit the use of the default charset
|
|
|
|
|
input1 = new InputStreamReader(new FileInputStream(file1), Charset.defaultCharset()); |
|
|
|
|
input2 = new InputStreamReader(new FileInputStream(file2), Charset.defaultCharset()); |
|
|
|
|
} else { |
|
|
|
|
input1 = new InputStreamReader(new FileInputStream(file1), charsetName); |
|
|
|
|
input2 = new InputStreamReader(new FileInputStream(file2), charsetName); |
|
|
|
|
} |
|
|
|
|
return IOUtils.contentEqualsIgnoreEOL(input1, input2); |
|
|
|
|
|
|
|
|
|
} finally { |
|
|
|
|
IOUtils.closeQuietly(input1); |
|
|
|
|
IOUtils.closeQuietly(input2); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -845,6 +857,7 @@ public class FileUtils {
|
|
|
|
|
* @return The decoded URL or {@code null} if the input was |
|
|
|
|
* {@code null}. |
|
|
|
|
*/ |
|
|
|
|
@SuppressWarnings("deprecation") // unavoidable until Java 7
|
|
|
|
|
static String decodeUrl(final String url) { |
|
|
|
|
String decoded = url; |
|
|
|
|
if (url != null && url.indexOf('%') >= 0) { |
|
|
|
@ -866,7 +879,7 @@ public class FileUtils {
|
|
|
|
|
} finally { |
|
|
|
|
if (bytes.position() > 0) { |
|
|
|
|
bytes.flip(); |
|
|
|
|
buffer.append(StandardCharsets.UTF_8.decode(bytes).toString()); |
|
|
|
|
buffer.append(Charsets.UTF_8.decode(bytes).toString()); |
|
|
|
|
bytes.clear(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -1090,8 +1103,11 @@ public class FileUtils {
|
|
|
|
|
* @since 2.1 |
|
|
|
|
*/ |
|
|
|
|
public static long copyFile(final File input, final OutputStream output) throws IOException { |
|
|
|
|
try (FileInputStream fis = new FileInputStream(input)) { |
|
|
|
|
final FileInputStream fis = new FileInputStream(input); |
|
|
|
|
try { |
|
|
|
|
return IOUtils.copyLarge(fis, output); |
|
|
|
|
} finally { |
|
|
|
|
fis.close(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -1118,10 +1134,15 @@ public class FileUtils {
|
|
|
|
|
throw new IOException("Destination '" + destFile + "' exists but is a directory"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
try (FileInputStream fis = new FileInputStream(srcFile); |
|
|
|
|
FileChannel input = fis.getChannel(); |
|
|
|
|
FileOutputStream fos = new FileOutputStream(destFile); |
|
|
|
|
FileChannel output = fos.getChannel()) { |
|
|
|
|
FileInputStream fis = null; |
|
|
|
|
FileOutputStream fos = null; |
|
|
|
|
FileChannel input = null; |
|
|
|
|
FileChannel output = null; |
|
|
|
|
try { |
|
|
|
|
fis = new FileInputStream(srcFile); |
|
|
|
|
fos = new FileOutputStream(destFile); |
|
|
|
|
input = fis.getChannel(); |
|
|
|
|
output = fos.getChannel(); |
|
|
|
|
final long size = input.size(); // TODO See IO-386
|
|
|
|
|
long pos = 0; |
|
|
|
|
long count = 0; |
|
|
|
@ -1134,6 +1155,8 @@ public class FileUtils {
|
|
|
|
|
} |
|
|
|
|
pos += bytesCopied; |
|
|
|
|
} |
|
|
|
|
} finally { |
|
|
|
|
IOUtils.closeQuietly(output, fos, input, fis); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
final long srcLen = srcFile.length(); // TODO See IO-386
|
|
|
|
@ -1356,7 +1379,7 @@ public class FileUtils {
|
|
|
|
|
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); |
|
|
|
|
exclusionList = new ArrayList<String>(srcFiles.length); |
|
|
|
|
for (final File srcFile : srcFiles) { |
|
|
|
|
final File copiedFile = new File(destDir, srcFile.getName()); |
|
|
|
|
exclusionList.add(copiedFile.getCanonicalPath()); |
|
|
|
@ -1372,7 +1395,7 @@ public class FileUtils {
|
|
|
|
|
* @param dest the destination |
|
|
|
|
* @throws FileNotFoundException if the destination does not exist |
|
|
|
|
*/ |
|
|
|
|
private static void checkFileRequirements(final File src, final File dest) throws FileNotFoundException { |
|
|
|
|
private static void checkFileRequirements(File src, File dest) throws FileNotFoundException { |
|
|
|
|
if (src == null) { |
|
|
|
|
throw new NullPointerException("Source must not be null"); |
|
|
|
|
} |
|
|
|
@ -1502,8 +1525,10 @@ public class FileUtils {
|
|
|
|
|
* @since 2.0 |
|
|
|
|
*/ |
|
|
|
|
public static void copyInputStreamToFile(final InputStream source, final File destination) throws IOException { |
|
|
|
|
try (InputStream in = source) { |
|
|
|
|
copyToFile(in, destination); |
|
|
|
|
try { |
|
|
|
|
copyToFile(source, destination); |
|
|
|
|
} finally { |
|
|
|
|
IOUtils.closeQuietly(source); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -1525,78 +1550,12 @@ public class FileUtils {
|
|
|
|
|
* @since 2.5 |
|
|
|
|
*/ |
|
|
|
|
public static void copyToFile(final InputStream source, final File destination) throws IOException { |
|
|
|
|
try (InputStream in = source; |
|
|
|
|
OutputStream out = openOutputStream(destination)) { |
|
|
|
|
IOUtils.copy(in, out); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 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> |
|
|
|
|
* 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> |
|
|
|
|
* <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. |
|
|
|
|
* |
|
|
|
|
* @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 |
|
|
|
|
* @see #copyDirectoryToDirectory(File, File) |
|
|
|
|
* @see #copyFileToDirectory(File, File) |
|
|
|
|
* @since 2.6 |
|
|
|
|
*/ |
|
|
|
|
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 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> |
|
|
|
|
* <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. |
|
|
|
|
* |
|
|
|
|
* @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 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 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); |
|
|
|
|
final FileOutputStream output = openOutputStream(destination); |
|
|
|
|
try { |
|
|
|
|
IOUtils.copy(source, output); |
|
|
|
|
output.close(); // don't swallow close Exception if copy completes normally
|
|
|
|
|
} finally { |
|
|
|
|
IOUtils.closeQuietly(output); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -1735,7 +1694,7 @@ public class FileUtils {
|
|
|
|
|
* @return The files in the directory, never null. |
|
|
|
|
* @throws IOException if an I/O error occurs |
|
|
|
|
*/ |
|
|
|
|
private static File[] verifiedListFiles(final File directory) throws IOException { |
|
|
|
|
private static File[] verifiedListFiles(File directory) throws IOException { |
|
|
|
|
if (!directory.exists()) { |
|
|
|
|
final String message = directory + " does not exist"; |
|
|
|
|
throw new IllegalArgumentException(message); |
|
|
|
@ -1766,11 +1725,11 @@ public class FileUtils {
|
|
|
|
|
* @throws NullPointerException if the file is {@code null} |
|
|
|
|
*/ |
|
|
|
|
public static boolean waitFor(final File file, final int seconds) { |
|
|
|
|
final long finishAt = System.currentTimeMillis() + (seconds * 1000L); |
|
|
|
|
long finishAt = System.currentTimeMillis() + (seconds * 1000L); |
|
|
|
|
boolean wasInterrupted = false; |
|
|
|
|
try { |
|
|
|
|
while (!file.exists()) { |
|
|
|
|
final long remaining = finishAt - System.currentTimeMillis(); |
|
|
|
|
long remaining = finishAt - System.currentTimeMillis(); |
|
|
|
|
if (remaining < 0){ |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
@ -1802,8 +1761,12 @@ public class FileUtils {
|
|
|
|
|
* @since 2.3 |
|
|
|
|
*/ |
|
|
|
|
public static String readFileToString(final File file, final Charset encoding) throws IOException { |
|
|
|
|
try (InputStream in = openInputStream(file)) { |
|
|
|
|
InputStream in = null; |
|
|
|
|
try { |
|
|
|
|
in = openInputStream(file); |
|
|
|
|
return IOUtils.toString(in, Charsets.toCharset(encoding)); |
|
|
|
|
} finally { |
|
|
|
|
IOUtils.closeQuietly(in); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -1831,7 +1794,7 @@ public class FileUtils {
|
|
|
|
|
* @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) |
|
|
|
|
* @deprecated 2.5 use {@link #readFileToString(File, Charset)} instead |
|
|
|
|
*/ |
|
|
|
|
@Deprecated |
|
|
|
|
public static String readFileToString(final File file) throws IOException { |
|
|
|
@ -1848,10 +1811,12 @@ public class FileUtils {
|
|
|
|
|
* @since 1.1 |
|
|
|
|
*/ |
|
|
|
|
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); |
|
|
|
|
InputStream in = null; |
|
|
|
|
try { |
|
|
|
|
in = openInputStream(file); |
|
|
|
|
return IOUtils.toByteArray(in); // Do NOT use file.length() - see IO-453
|
|
|
|
|
} finally { |
|
|
|
|
IOUtils.closeQuietly(in); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -1866,8 +1831,12 @@ public class FileUtils {
|
|
|
|
|
* @since 2.3 |
|
|
|
|
*/ |
|
|
|
|
public static List<String> readLines(final File file, final Charset encoding) throws IOException { |
|
|
|
|
try (InputStream in = openInputStream(file)) { |
|
|
|
|
InputStream in = null; |
|
|
|
|
try { |
|
|
|
|
in = openInputStream(file); |
|
|
|
|
return IOUtils.readLines(in, Charsets.toCharset(encoding)); |
|
|
|
|
} finally { |
|
|
|
|
IOUtils.closeQuietly(in); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -1894,7 +1863,7 @@ public class FileUtils {
|
|
|
|
|
* @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) |
|
|
|
|
* @deprecated 2.5 use {@link #readLines(File, Charset)} instead |
|
|
|
|
*/ |
|
|
|
|
@Deprecated |
|
|
|
|
public static List<String> readLines(final File file) throws IOException { |
|
|
|
@ -1937,15 +1906,11 @@ public class FileUtils {
|
|
|
|
|
try { |
|
|
|
|
in = openInputStream(file); |
|
|
|
|
return IOUtils.lineIterator(in, encoding); |
|
|
|
|
} catch (final IOException | RuntimeException ex) { |
|
|
|
|
try { |
|
|
|
|
if (in != null) { |
|
|
|
|
in.close(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
catch (final IOException e) { |
|
|
|
|
ex.addSuppressed(e); |
|
|
|
|
} |
|
|
|
|
} catch (final IOException ex) { |
|
|
|
|
IOUtils.closeQuietly(in); |
|
|
|
|
throw ex; |
|
|
|
|
} catch (final RuntimeException ex) { |
|
|
|
|
IOUtils.closeQuietly(in); |
|
|
|
|
throw ex; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -2010,10 +1975,15 @@ public class FileUtils {
|
|
|
|
|
* @throws IOException in case of an I/O error |
|
|
|
|
* @since 2.3 |
|
|
|
|
*/ |
|
|
|
|
public static void writeStringToFile(final File file, final String data, final Charset encoding, |
|
|
|
|
final boolean append) throws IOException { |
|
|
|
|
try (OutputStream out = openOutputStream(file, append)) { |
|
|
|
|
public static void writeStringToFile(final File file, final String data, final Charset encoding, final boolean |
|
|
|
|
append) throws IOException { |
|
|
|
|
OutputStream out = null; |
|
|
|
|
try { |
|
|
|
|
out = openOutputStream(file, append); |
|
|
|
|
IOUtils.write(data, out, encoding); |
|
|
|
|
out.close(); // don't swallow close Exception if copy completes normally
|
|
|
|
|
} finally { |
|
|
|
|
IOUtils.closeQuietly(out); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -2041,7 +2011,7 @@ public class FileUtils {
|
|
|
|
|
* @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) |
|
|
|
|
* @deprecated 2.5 use {@link #writeStringToFile(File, String, Charset)} instead |
|
|
|
|
*/ |
|
|
|
|
@Deprecated |
|
|
|
|
public static void writeStringToFile(final File file, final String data) throws IOException { |
|
|
|
@ -2057,7 +2027,7 @@ public class FileUtils {
|
|
|
|
|
* 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) |
|
|
|
|
* @deprecated 2.5 use {@link #writeStringToFile(File, String, Charset, boolean)} instead |
|
|
|
|
*/ |
|
|
|
|
@Deprecated |
|
|
|
|
public static void writeStringToFile(final File file, final String data, final boolean append) throws IOException { |
|
|
|
@ -2071,7 +2041,7 @@ public class FileUtils {
|
|
|
|
|
* @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) |
|
|
|
|
* @deprecated 2.5 use {@link #write(File, CharSequence, Charset)} instead |
|
|
|
|
*/ |
|
|
|
|
@Deprecated |
|
|
|
|
public static void write(final File file, final CharSequence data) throws IOException { |
|
|
|
@ -2087,7 +2057,7 @@ public class FileUtils {
|
|
|
|
|
* 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) |
|
|
|
|
* @deprecated 2.5 use {@link #write(File, CharSequence, Charset, boolean)} instead |
|
|
|
|
*/ |
|
|
|
|
@Deprecated |
|
|
|
|
public static void write(final File file, final CharSequence data, final boolean append) throws IOException { |
|
|
|
@ -2219,8 +2189,13 @@ public class FileUtils {
|
|
|
|
|
*/ |
|
|
|
|
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)) { |
|
|
|
|
OutputStream out = null; |
|
|
|
|
try { |
|
|
|
|
out = openOutputStream(file, append); |
|
|
|
|
out.write(data, off, len); |
|
|
|
|
out.close(); // don't swallow close Exception if copy completes normally
|
|
|
|
|
} finally { |
|
|
|
|
IOUtils.closeQuietly(out); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -2331,8 +2306,15 @@ public class FileUtils {
|
|
|
|
|
*/ |
|
|
|
|
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); |
|
|
|
|
FileOutputStream out = null; |
|
|
|
|
try { |
|
|
|
|
out = openOutputStream(file, append); |
|
|
|
|
final BufferedOutputStream buffer = new BufferedOutputStream(out); |
|
|
|
|
IOUtils.writeLines(lines, lineEnding, buffer, encoding); |
|
|
|
|
buffer.flush(); |
|
|
|
|
out.close(); // don't swallow close Exception if copy completes normally
|
|
|
|
|
} finally { |
|
|
|
|
IOUtils.closeQuietly(out); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -2634,9 +2616,9 @@ public class FileUtils {
|
|
|
|
|
/** |
|
|
|
|
* the size of a file |
|
|
|
|
* @param file the file to check |
|
|
|
|
* @return the size of the file |
|
|
|
|
* @return the size of the fil |
|
|
|
|
*/ |
|
|
|
|
private static long sizeOf0(final File file) { |
|
|
|
|
private static long sizeOf0(File file) { |
|
|
|
|
if (file.isDirectory()) { |
|
|
|
|
return sizeOfDirectory0(file); |
|
|
|
|
} else { |
|
|
|
@ -2688,7 +2670,7 @@ public class FileUtils {
|
|
|
|
|
// internal method; if file does not exist will return 0
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Returns the size of a file |
|
|
|
|
* Returns the sid of a file |
|
|
|
|
* @param fileOrDir The file |
|
|
|
|
* @return the size |
|
|
|
|
*/ |
|
|
|
@ -2887,8 +2869,12 @@ public class FileUtils {
|
|
|
|
|
if (file.isDirectory()) { |
|
|
|
|
throw new IllegalArgumentException("Checksums can't be computed on directories"); |
|
|
|
|
} |
|
|
|
|
try (InputStream in = new CheckedInputStream(new FileInputStream(file), checksum)) { |
|
|
|
|
InputStream in = null; |
|
|
|
|
try { |
|
|
|
|
in = new CheckedInputStream(new FileInputStream(file), checksum); |
|
|
|
|
IOUtils.copy(in, new NullOutputStream()); |
|
|
|
|
} finally { |
|
|
|
|
IOUtils.closeQuietly(in); |
|
|
|
|
} |
|
|
|
|
return checksum; |
|
|
|
|
} |
|
|
|
@ -3101,9 +3087,61 @@ public class FileUtils {
|
|
|
|
|
* @since 2.0 |
|
|
|
|
*/ |
|
|
|
|
public static boolean isSymlink(final File file) throws IOException { |
|
|
|
|
if ( Java7Support.isAtLeastJava7() ) |
|
|
|
|
{ |
|
|
|
|
return Java7Support.isSymLink( file ); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (file == null) { |
|
|
|
|
throw new NullPointerException("File must not be null"); |
|
|
|
|
} |
|
|
|
|
return Files.isSymbolicLink(file.toPath()); |
|
|
|
|
if (FilenameUtils.isSystemWindows()) { |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
File fileInCanonicalDir = null; |
|
|
|
|
if (file.getParent() == null) { |
|
|
|
|
fileInCanonicalDir = file; |
|
|
|
|
} else { |
|
|
|
|
final File canonicalDir = file.getParentFile().getCanonicalFile(); |
|
|
|
|
fileInCanonicalDir = new File(canonicalDir, file.getName()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (fileInCanonicalDir.getCanonicalFile().equals(fileInCanonicalDir.getAbsoluteFile())) { |
|
|
|
|
return isBrokenSymlink(file); |
|
|
|
|
} else { |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Determines if the specified file is possibly a broken symbolic link. |
|
|
|
|
* |
|
|
|
|
* @param file the file to check |
|
|
|
|
|
|
|
|
|
* @return true if the file is a Symbolic Link |
|
|
|
|
* @throws IOException if an IO error occurs while checking the file |
|
|
|
|
*/ |
|
|
|
|
private static boolean isBrokenSymlink(final File file) throws IOException { |
|
|
|
|
// if file exists then if it is a symlink it's not broken
|
|
|
|
|
if (file.exists()) { |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
// a broken symlink will show up in the list of files of its parent directory
|
|
|
|
|
final File canon = file.getCanonicalFile(); |
|
|
|
|
File parentDir = canon.getParentFile(); |
|
|
|
|
if (parentDir == null || !parentDir.exists()) { |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// is it worthwhile to create a FileFilterUtil method for this?
|
|
|
|
|
// is it worthwhile to create an "identity" IOFileFilter for this?
|
|
|
|
|
File[] fileInDir = parentDir.listFiles( |
|
|
|
|
new FileFilter() { |
|
|
|
|
public boolean accept(File aFile) { |
|
|
|
|
return aFile.equals(canon); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
); |
|
|
|
|
return fileInDir != null && fileInDir.length > 0; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|