- java.lang.Object
-
- com.aoapps.lang.io.FileUtils
-
public final class FileUtils extends Object
File utilities.
-
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static FilecheckIsDirectory(File directory)Ensures that the file is a directory.static booleancontentEquals(File file, byte[] contents)Compares the contents of a file to the provided array.static booleancontentEquals(File file1, File file2)Compares the contents of two files, not supporting directories.static intcontentHashCode(File file)Computes a hash code of the file contents, consistent with Arrays.hashCode(byte[]).static longcopy(File from, File to)Copies one file over another, possibly creating if needed.static longcopy(File from, OutputStream out)Copies a file to an output stream.static longcopy(File from, Writer out)Copies a file to a writer in system default locale.static longcopy(File from, Appendable out)Copies a file to an appendable in system default locale.static voidcopyRecursive(File from, File to)Recursively copies source to destination.static voidcopyRecursive(File from, File to, FileFilter fileFilter)Recursively copies source to destination.static longcopyToFile(InputStream in, File file)Copies a stream to a file.static FilecopyToTempFile(InputStream in, String prefix, String suffix)Copies a stream to a newly created temporary file in the given directory.static FilecopyToTempFile(InputStream in, String prefix, String suffix, File directory)Copies a stream to a newly created temporary file.static FilecreateTempDirectory(String prefix, String suffix)Deprecated.Please useFiles.createTempDirectory(java.lang.String, java.nio.file.attribute.FileAttribute...), which does not suffer from any race conditions.static FilecreateTempDirectory(String prefix, String suffix, File directory)Deprecated.Please useFiles.createTempDirectory(java.nio.file.Path, java.lang.String, java.nio.file.attribute.FileAttribute...), which does not suffer from any race conditions.static voiddelete(File file)Deprecated.Please useFiles.delete(java.nio.file.Path)static voiddeleteRecursive(File file)Deprecated.static StringgetExtension(String path)Gets the extension from the path, not including any period.static FilegetFile(URL url, String urlEncoding, boolean deleteOnExit)Deprecated.Please use TempFileContext asFile.deleteOnExit()is prone to memory leaks in long-running applications.static Filemkdir(File directory)Deprecated.static Filemkdirs(File directory)Deprecated.static StringreadFileAsString(File file)Reads the contents of a File and returns as a String in the system default character set.static StringreadFileAsString(File file, Charset charset)Reads the contents of a File and returns as a String in the provided character set.static voidrename(File from, File to)Atomically renames one file to another, throwing IOException when unsuccessful.static voidrenameAllowNonAtomic(File from, File to)Renames one file to another, throwing IOException when unsuccessful.
-
-
-
Method Detail
-
delete
@Deprecated public static void delete(File file) throws IOException
Deprecated.Please useFiles.delete(java.nio.file.Path)Deletes the provided file, throwing IOException if unsuccessful.- Throws:
IOException
-
deleteRecursive
@Deprecated public static void deleteRecursive(File file) throws IOException
Deprecated.Recursively deletes the provided file, being careful to not follow symbolic links (but there are still unavoidable race conditions).- Throws:
IOException
-
contentEquals
public static boolean contentEquals(File file, byte[] contents) throws IOException
Compares the contents of a file to the provided array.- Throws:
IOException
-
contentEquals
public static boolean contentEquals(File file1, File file2) throws IOException
Compares the contents of two files, not supporting directories.- Throws:
IOException
-
contentHashCode
public static int contentHashCode(File file) throws IOException
Computes a hash code of the file contents, consistent with Arrays.hashCode(byte[]).- Throws:
IOException- See Also:
Arrays.hashCode(byte[])
-
createTempDirectory
@Deprecated public static File createTempDirectory(String prefix, String suffix) throws IOException
Deprecated.Please useFiles.createTempDirectory(java.lang.String, java.nio.file.attribute.FileAttribute...), which does not suffer from any race conditions.Creates a temporary directory.- Throws:
IOException
-
createTempDirectory
@Deprecated public static File createTempDirectory(String prefix, String suffix, File directory) throws IOException
Deprecated.Please useFiles.createTempDirectory(java.nio.file.Path, java.lang.String, java.nio.file.attribute.FileAttribute...), which does not suffer from any race conditions.Creates a temporary directory.- Throws:
IOException
-
copyToFile
public static long copyToFile(InputStream in, File file) throws IOException
Copies a stream to a file.- Returns:
- the number of bytes copied
- Throws:
IOException
-
copyToTempFile
public static File copyToTempFile(InputStream in, String prefix, String suffix) throws IOException
Copies a stream to a newly created temporary file in the given directory.The file is created with the default permissions via
Files.createTempFile(java.lang.String, java.lang.String, java.nio.file.attribute.FileAttribute...).The file is not deleted on exit. If this is required, we recommend creating the temp file with the AO TempFiles project, then using
copyToFile(java.io.InputStream, java.io.File). This avoids the memory leak of the implementation ofFile.deleteOnExit().- Throws:
IOException
-
copyToTempFile
public static File copyToTempFile(InputStream in, String prefix, String suffix, File directory) throws IOException
Copies a stream to a newly created temporary file.The file is created with the default permissions via
Files.createTempFile(java.lang.String, java.lang.String, java.nio.file.attribute.FileAttribute...).The file is not deleted on exit. If this is required, we recommend creating the temp file with the AO TempFiles project, then using
copyToFile(java.io.InputStream, java.io.File). This avoids the memory leak of the implementation ofFile.deleteOnExit().- Throws:
IOException
-
mkdir
@Deprecated public static File mkdir(File directory) throws IOException
Deprecated.Makes a directory. The directory must not already exist.- Returns:
- The directory itself.
- Throws:
IOException- if mkdir fails.
-
mkdirs
@Deprecated public static File mkdirs(File directory) throws IOException
Deprecated.Makes a directory and all of its parents. The directory may optionally already exist.- Returns:
- The directory itself.
- Throws:
IOException- if mkdirs fails.
-
checkIsDirectory
public static File checkIsDirectory(File directory) throws IOException
Ensures that the file is a directory.- Returns:
- The directory itself.
- Throws:
IOException- if not a directory
-
copy
public static long copy(File from, File to) throws IOException
Copies one file over another, possibly creating if needed.- Returns:
- the number of bytes copied
- Throws:
IOException
-
copy
public static long copy(File from, OutputStream out) throws IOException
Copies a file to an output stream.- Returns:
- the number of bytes copied
- Throws:
IOException
-
copy
public static long copy(File from, Writer out) throws IOException
Copies a file to a writer in system default locale.- Returns:
- the number of characters copied
- Throws:
IOException
-
copy
public static long copy(File from, Appendable out) throws IOException
Copies a file to an appendable in system default locale.- Returns:
- the number of characters copied
- Throws:
IOException
-
copyRecursive
public static void copyRecursive(File from, File to) throws IOException
Recursively copies source to destination. Destination must not exist.- Throws:
IOException
-
copyRecursive
public static void copyRecursive(File from, File to, FileFilter fileFilter) throws IOException
Recursively copies source to destination. Destination must not exist.- Throws:
IOException
-
getExtension
public static String getExtension(String path)
Gets the extension from the path, not including any period. If no extension, returns an empty string.
-
getFile
@Deprecated public static File getFile(URL url, String urlEncoding, boolean deleteOnExit) throws IOException
Deprecated.Please use TempFileContext asFile.deleteOnExit()is prone to memory leaks in long-running applications.Gets a File for a URL, retrieving the contents into a temporary file if needed.The file is created with the default permissions via
Files.createTempFile(java.lang.String, java.lang.String, java.nio.file.attribute.FileAttribute...).- Parameters:
deleteOnExit- whentrue, any newly created temp file will be flagged fordelete on exit- Throws:
IOException
-
rename
public static void rename(File from, File to) throws IOException
Atomically renames one file to another, throwing IOException when unsuccessful.- Throws:
IOException
-
renameAllowNonAtomic
public static void renameAllowNonAtomic(File from, File to) throws IOException
Renames one file to another, throwing IOException when unsuccessful. Allow a non-atomic delete/rename pair when the underlying system is unable to rename one file over another, such as in Microsoft Windows.- Throws:
IOException
-
readFileAsString
public static String readFileAsString(File file) throws IOException
Reads the contents of a File and returns as a String in the system default character set.
-
readFileAsString
public static String readFileAsString(File file, Charset charset) throws IOException
Reads the contents of a File and returns as a String in the provided character set.- Throws:
IOException- See Also:
readFileAsString(java.io.File)
-
-