Class FileUtil
java.lang.Object
jodd.io.FileUtil
File utilities.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final Stringprivate static final Stringprivate static final Stringprivate static final Stringprivate static final Stringprivate static final Stringprivate static final intstatic StringDefault prefix for temporary files.private static final Stringprivate static final int -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static void_copyDirectory(File srcDir, File destDir) private static voidInternal file copy when most of the pre-checking has passed.private static void_moveDirectory(File srcDest, File destDir) Moves a directory.private static voidMoves aFile.static voidappendBytes(File dest, byte[] data) static voidappendBytes(File dest, byte[] data, int off, int len) Appends bytes.static voidappendBytes(String dest, byte[] data) static voidappendBytes(String dest, byte[] data, int off, int len) static voidappendString(File dest, String data) static voidappendString(File dest, String data, Charset encoding) Appends String.static voidappendString(String dest, String data) static voidappendString(String dest, String data, Charset encoding) private static FilecheckCreateDirectory(File dir) Checks if directory can be created.private static voidChecks if directory can be deleted.private static voidcheckDirCopy(File srcDir, File destDir) Checks that srcDir exists, that it is a directory and if srcDir and destDir are not equal.private static voidcheckExists(File file) Checks ifFileexists.private static voidChecks if directory exists.private static voidcheckFileCopy(File srcFile, File destFile) Checks that file copy can occur.private static voidcheckIsDirectory(File dir) Checks ifFileis a directory.private static voidcheckIsFile(File file) Checks ifFileis a file.private static voidcheckReferenceExists(File file) Checks ifFileexists.static voidCleans a directory without deleting it.static voidstatic booleanCompare the contents of twoFiles to determine if they are equal or not.static booleanstatic voidSmart copy.static voidstatic voidCopies directory with specified copy params.static voidstatic voidstatic voidstatic FilecopyFileToDir(File srcFile, File destDir) Copies aFileto directory with specified copy params and returns copied destination.static FilecopyFileToDir(String srcFile, String destDir) static Filestatic FilecreateTempDirectory(String prefix, String suffix) static FilecreateTempDirectory(String prefix, String suffix, File tempDir) Creates temporary directory.static Filestatic FilecreateTempFile(String prefix, String suffix, File tempDir) Creates temporaryFile.static FilecreateTempFile(String prefix, String suffix, File tempDir, boolean create) Creates temporaryFile.static voidSmart delete of destination file or directory.static voidstatic voidDeletes a directory.static voidstatic voiddeleteFile(File destFile) Deletes aFile.static voiddeleteFile(String destFile) private static CharsetDetect encoding onUnicodeInputStreamby usingUnicodeInputStream.getDetectedEncoding().private static Charsetencoding()Returns default encoding.static booleanstatic booleanprivate static FileSimple factory forFileobjects.static FileSimple factory forFileobjects but with home resolving.static FilegetParentFile(File file) Returns parent for the file.static booleanisAncestor(File ancestor, File file, boolean strict) Check if oneFileis an ancestor of second one.static booleanChecks the start of the file for ASCII control charactersstatic booleanisExistingFile(File file) ReturnstrueifFileexists.static booleanisExistingFolder(File folder) Returnstrueif directory exists.static booleanisFilePathAcceptable(File file, FileFilter fileFilter) Checks if file and its ancestors are acceptable by usingFileFilter.accept(File).static booleanTests if the specifiedFileis newer than the specified time reference.static booleanUsesFile.lastModified()for reference.static booleanstatic booleanstatic booleanTests if the specifiedFileis older than the specified time reference.static booleanUsesFile.lastModified()for reference.static booleanstatic booleanstatic booleanDeprecated.static StringCreates MD5 digest of aFile.static FileCreates single directory.static Filestatic FileCreates all directories at once.static Filestatic voidSmart move.static voidstatic Filestatic Filestatic Filestatic Filestatic FilemoveFileToDir(File srcFile, File destDir) Moves a file to a directory.static FilemoveFileToDir(String srcFile, String destDir) protected static voidWrites data toFiledestination.protected static voidWrites characters toFiledestination.protected static voidWrites data using encoding toFile.static byte[]static byte[]Read file and returns byte array with contents.static byte[]static char[]static char[]Reads file content as char array.static char[]static char[]static String[]static String[]static String[]static String[]static StringreadString(File source) static StringreadString(File file, Charset encoding) static StringreadString(String source) static StringreadString(String source, Charset encoding) static char[]readUTFChars(File file) Reads UTF file content as char array.static char[]readUTFChars(String fileName) static StringreadUTFString(File file) static StringreadUTFString(InputStream inputStream) Detects optional BOM and reads UTFStringfrom anInputStream.static StringreadUTFString(String fileName) static StringCreates SHA-256 digest of a file.static StringCreates SHA-512 digest of a file.private static InputStreamReturns either newFileInputStreamor newUnicodeInputStream.static FiletoContainerFile(URL url) Returns a file of either a folder or a containing archive.static Filestatic StringtoFileName(URL url) static voidImplements the Unix "touch" utility.static voidstatic URLprivate static UnicodeInputStreamunicodeInputStreamOf(File file) private static UnicodeInputStreamunicodeInputStreamOf(InputStream input, Charset targetEncoding) Returns newUnicodeInputStreamusingInputStreamand targetEncoding.static voidwriteBytes(File dest, byte[] data) static voidwriteBytes(File dest, byte[] data, int off, int len) Write bytes.static voidwriteBytes(String dest, byte[] data) static voidwriteBytes(String dest, byte[] data, int off, int len) static voidwriteChars(File dest, char[] data) static voidwriteChars(File dest, char[] data, Charset encoding) Write characters.static voidwriteChars(String dest, char[] data) static voidwriteChars(String dest, char[] data, Charset encoding) static voidwriteStream(FileOutputStream out, InputStream in) WriteInputStreamin toFileOutputStream.static voidwriteStream(File dest, InputStream in) static voidwriteStream(String dest, InputStream in) static voidwriteString(File dest, String data) static voidwriteString(File dest, String data, Charset encoding) Writes String.static voidwriteString(String dest, String data) static voidwriteString(String dest, String data, Charset encoding)
-
Field Details
-
tempFilePrefix
Default prefix for temporary files. -
MSG_NOT_A_DIRECTORY
- See Also:
-
MSG_CANT_CREATE
- See Also:
-
MSG_NOT_FOUND
- See Also:
-
MSG_NOT_A_FILE
- See Also:
-
MSG_UNABLE_TO_DELETE
- See Also:
-
ZERO
private static final int ZERO- See Also:
-
NEGATIVE_ONE
private static final int NEGATIVE_ONE- See Also:
-
FILE_PROTOCOL
- See Also:
-
USER_HOME
- See Also:
-
-
Constructor Details
-
FileUtil
public FileUtil()
-
-
Method Details
-
file
-
file
-
equals
-
equals
-
toFile
-
toURL
- Returns:
URLornullin case of error.- Throws:
MalformedURLException- ifFilecannot be converted.
-
toFileName
-
toContainerFile
-
isExistingFile
-
isExistingFolder
Returnstrueif directory exists. -
mkdirs
- Throws:
IOException- See Also:
-
mkdirs
Creates all directories at once.- Parameters:
dirs- Directories to make.- Throws:
IOException- if cannot create directory.
-
mkdir
- Throws:
IOException- See Also:
-
mkdir
Creates single directory.- Throws:
IOException- if cannot create directory.
-
touch
- Throws:
IOException- See Also:
-
touch
Implements the Unix "touch" utility. It creates a newFilewith size 0 or, if theFileexists already, it is opened and closed without modifying it, but updating theFiledate and time.- Throws:
IOException
-
copyFile
- Throws:
IOException- See Also:
-
copyFile
- Parameters:
srcFile- SourceFile.destFile- DestinationFile.- Throws:
IOException- if cannot copy
-
_copyFile
Internal file copy when most of the pre-checking has passed.- Parameters:
srcFile- SourceFile.destFile- DestinationFile.- Throws:
IOException- if cannot copy
-
copyFileToDir
- Throws:
IOException- See Also:
-
copyFileToDir
Copies aFileto directory with specified copy params and returns copied destination.- Throws:
IOException
-
copyDir
- Throws:
IOException- See Also:
-
copyDir
Copies directory with specified copy params.- Throws:
IOException- See Also:
-
_copyDirectory
- Parameters:
srcDir-destDir-- Throws:
IOException
-
moveFile
- Throws:
IOException- See Also:
-
moveFile
- Throws:
IOException- See Also:
-
_moveFile
Moves aFile.- Parameters:
srcFile- SourceFile.destFile- Destination directory.- Throws:
IOException
-
moveFileToDir
- Throws:
IOException- See Also:
-
moveFileToDir
Moves a file to a directory.- Parameters:
srcFile- SourceFile.destDir- Destination directory.- Throws:
IOException- if there is an error during move.
-
moveDir
- Throws:
IOException- See Also:
-
moveDir
- Throws:
IOException- See Also:
-
_moveDirectory
Moves a directory.- Parameters:
srcDest- Source directorydestDir- Destination directory.- Throws:
IOException- if there is an error during move.
-
deleteFile
- Throws:
IOException- See Also:
-
deleteFile
Deletes aFile.- Parameters:
destFile- Destination to delete.- Throws:
IOException- if there was an error deleting.
-
deleteDir
- Throws:
IOException- See Also:
-
deleteDir
Deletes a directory.- Parameters:
destDir- Destination to delete.- Throws:
IOException- if there was an error deleting.
-
cleanDir
- Throws:
IOException- See Also:
-
cleanDir
Cleans a directory without deleting it.- Parameters:
destDir- destination to clean.- Throws:
IOException- if something went wrong.
-
readUTFChars
- Throws:
IOException- See Also:
-
readUTFChars
Reads UTF file content as char array.- Parameters:
file-Fileto read.- Returns:
- array of characters.
- Throws:
IOException- if something went wrong.
-
readChars
Reads file content as char array.- Parameters:
file-Fileto read.encoding- Encoding to use.- Returns:
- array of characters.
- Throws:
IOException- if something went wrong.
-
readChars
- Throws:
IOException- See Also:
-
readChars
- Throws:
IOException- See Also:
-
readChars
- Throws:
IOException- See Also:
-
writeChars
- Throws:
IOException- See Also:
-
writeChars
- Throws:
IOException- See Also:
-
writeChars
- Throws:
IOException- See Also:
-
writeChars
Write characters. append = false- Throws:
IOException- See Also:
-
outChars
protected static void outChars(File dest, char[] data, Charset encoding, boolean append) throws IOException Writes characters toFiledestination.- Parameters:
dest- destinationFiledata- Data as aStringencoding- Encoding as aStringappend-trueif appending;falseifFileshould be overwritten.- Throws:
IOException- if something went wrong.
-
readUTFString
- Throws:
IOException- See Also:
-
readUTFString
- Parameters:
file-Fileto read.- Returns:
- String in UTF encoding.
- Throws:
IOException- if copy toInputStreamerrors.- See Also:
-
readUTFString
Detects optional BOM and reads UTFStringfrom anInputStream. If BOM is missing, UTF-8 is assumed.- Parameters:
inputStream-InputStreamto read.- Returns:
- String in UTF encoding.
- Throws:
IOException- if copy toInputStreamerrors.- See Also:
-
readString
ReadsFilecontent asStringencoded in provided encoding. For UTF encoded files, detects optional BOM characters.- Parameters:
file-Fileto read.encoding- Encoding to use.- Returns:
- String representing
Filecontent. - Throws:
IOException- if copy toInputStreamerrors.- See Also:
-
readString
- Throws:
IOException- See Also:
-
readString
- Throws:
IOException- See Also:
-
readString
- Throws:
IOException- See Also:
-
writeString
- Throws:
IOException- See Also:
-
writeString
- Throws:
IOException- See Also:
-
writeString
- Throws:
IOException- See Also:
-
writeString
Writes String. append = false- Throws:
IOException- See Also:
-
appendString
- Throws:
IOException- See Also:
-
appendString
- Throws:
IOException- See Also:
-
appendString
- Throws:
IOException- See Also:
-
appendString
Appends String. append = true- Throws:
IOException- See Also:
-
outString
protected static void outString(File dest, String data, Charset encoding, boolean append) throws IOException Writes data using encoding toFile.- Parameters:
dest- destinationFiledata- Data as aStringencoding- Encoding as aStringappend-trueif appending;falseifFileshould be overwritten.- Throws:
IOException- if something went wrong.
-
writeStream
- Throws:
IOException- See Also:
-
writeStream
- Throws:
IOException- See Also:
-
writeStream
WriteInputStreamin toFileOutputStream.- Parameters:
out-FileOutputStreamto write to.in-InputStreamto read.- Throws:
IOException- if there is an issue reading/writing.
-
readLines
- Throws:
IOException- See Also:
-
readLines
- Throws:
IOException- See Also:
-
readLines
- Throws:
IOException- See Also:
-
readLines
-
readBytes
- Throws:
IOException- See Also:
-
readBytes
- Throws:
IOException- See Also:
-
readBytes
Read file and returns byte array with contents.- Parameters:
file-Fileto readcount- number of bytes to read- Returns:
- byte array from
Filecontents. - Throws:
IOException- if not aFileorFiledoes not exist or file size is larger thanInteger.MAX_VALUE.
-
writeBytes
- Throws:
IOException- See Also:
-
writeBytes
- Throws:
IOException- See Also:
-
writeBytes
- Throws:
IOException- See Also:
-
writeBytes
Write bytes. append = false- Throws:
IOException- See Also:
-
appendBytes
- Throws:
IOException- See Also:
-
appendBytes
- Throws:
IOException- See Also:
-
appendBytes
- Throws:
IOException- See Also:
-
appendBytes
Appends bytes. append = true- Throws:
IOException- See Also:
-
outBytes
protected static void outBytes(File dest, byte[] data, int off, int len, boolean append) throws IOException Writes data toFiledestination.- Parameters:
dest- destinationFiledata- Data as aStringoff- the start offset in the data.len- the number of bytes to write.append-trueif appending;falseifFileshould be overwritten.- Throws:
IOException- if something went wrong.
-
compare
- Throws:
IOException
-
compare
Compare the contents of twoFiles to determine if they are equal or not.This method checks to see if the two
Files are different lengths or if they point to the sameFile, before resorting to byte-by-byte comparison of the contents.Code origin: Avalon
- Throws:
IOException
-
isOlder
-
isNewer
-
isNewer
UsesFile.lastModified()for reference.- See Also:
-
isOlder
UsesFile.lastModified()for reference.- See Also:
-
isNewer
Tests if the specifiedFileis newer than the specified time reference. -
isNewer
- See Also:
-
isOlder
Tests if the specifiedFileis older than the specified time reference. -
isOlder
- See Also:
-
copy
- Throws:
IOException- See Also:
-
copy
Smart copy. If source is a directory, copy it to destination. Otherwise, if destination is directory, copy source file to it. Otherwise, try to copy source file to destination file.- Parameters:
src- sourceFiledest- destinationFile- Throws:
IOException- if there is an error copying.- See Also:
-
move
- Throws:
IOException- See Also:
-
move
-
delete
- Throws:
IOException- See Also:
-
delete
Smart delete of destination file or directory.- Throws:
IOException- if there is an issue deleting the file/directory.- See Also:
-
isAncestor
-
getParentFile
-
isFilePathAcceptable
Checks if file and its ancestors are acceptable by usingFileFilter.accept(File).- Parameters:
file-Fileto check.fileFilter-FileFilterto use.- Returns:
- if file and its ancestors are acceptable
-
createTempDirectory
- Throws:
IOException- See Also:
-
createTempDirectory
- Throws:
IOException- See Also:
-
createTempDirectory
public static File createTempDirectory(String prefix, String suffix, File tempDir) throws IOException Creates temporary directory.- Throws:
IOException- See Also:
-
createTempFile
- Throws:
IOException- See Also:
-
createTempFile
public static File createTempFile(String prefix, String suffix, File tempDir, boolean create) throws IOException Creates temporaryFile.- Parameters:
prefix- The prefix string to be used in generating the file's name; must be at least three characters longsuffix- The suffix string to be used in generating the file's name; may benull, in which case the suffix".tmp"will be usedtempDir- The directory in which the file is to be created, ornullif the default temporary-file directory is to be usedcreate- Ifcreateis set totrueFilewill be physically created on the file system. Otherwise, it will be created and then deleted - trick that will make tempFileexist only if they are used.- Returns:
- File
- Throws:
IOException
-
createTempFile
Creates temporaryFile. Wraps Java method and repeats creation several times if something fails.- Parameters:
prefix- The prefix string to be used in generating the file's name; must be at least three characters longsuffix- The suffix string to be used in generating the file's name; may benull, in which case the suffix".tmp"will be usedtempDir- The directory in which the file is to be created, ornullif the default temporary-file directory is to be used- Throws:
IOException
-
isSymlink
Deprecated.Files.isSymbolicLink(java.nio.file.Path)provides this functionality natively as of Java 1.7.Determines whether the specified file is a symbolic link rather than an actual file. -
md5
Creates MD5 digest of aFile.- Parameters:
file-Fileto create digest of.- Returns:
- MD5 digest of the
File. - Throws:
IOException
-
sha256
Creates SHA-256 digest of a file.- Parameters:
file-Fileto create digest of.- Returns:
- SHA-256 digest of the
File. - Throws:
IOException
-
sha512
Creates SHA-512 digest of a file.- Parameters:
file-Fileto create digest of.- Returns:
- SHA-512 digest of the
File. - Throws:
IOException
-
isBinary
Checks the start of the file for ASCII control characters- Parameters:
file-File- Returns:
- true if the the start of the
Fileis ASCII control characters. - Throws:
IOException
-
unicodeInputStreamOf
- Throws:
IOException- See Also:
-
unicodeInputStreamOf
Returns newUnicodeInputStreamusingInputStreamand targetEncoding.- Parameters:
input-InputStreamtargetEncoding- Encoding to use.- Returns:
- new
UnicodeInputStream.
-
streamOf
Returns either newFileInputStreamor newUnicodeInputStream.- Returns:
- either
FileInputStreamorUnicodeInputStream. - Throws:
IOException- if something went wrong.- See Also:
-
detectEncoding
Detect encoding onUnicodeInputStreamby usingUnicodeInputStream.getDetectedEncoding().- Parameters:
in-UnicodeInputStream- Returns:
- UTF encoding as a String. If encoding could not be detected, defaults to UTF_8
- See Also:
-
checkExists
Checks ifFileexists. Throws IOException if not.- Parameters:
file-File- Throws:
FileNotFoundException- if file does not exist.
-
checkReferenceExists
Checks ifFileexists. Throws IllegalArgumentException if not.- Parameters:
file-File- Throws:
IllegalArgumentException- if file does not exist.
-
checkIsFile
Checks ifFileis a file. Throws IOException if not.- Parameters:
file-File- Throws:
IOException- ifFileis not a file.
-
checkIsDirectory
Checks ifFileis a directory. Throws IOException if not.- Parameters:
dir- Directory- Throws:
IOException- ifFileis not a directory.
-
checkExistsAndDirectory
Checks if directory exists. Throws IOException if it does not.- Parameters:
dir- Directory- Throws:
IOException- if directory does not exist.- See Also:
-
checkCreateDirectory
Checks if directory can be created. Throws IOException if it cannot.This actually creates directory (and its ancestors) (as per
File.mkdirs()}).- Parameters:
dir- Directory- Throws:
IOException- if directory cannot be created.
-
checkDeleteSuccessful
Checks if directory can be deleted. Throws IOException if it cannot. This actually deletes directory (as perFile.delete()).- Parameters:
dir- Directory- Throws:
IOException- if directory cannot be created.
-
checkDirCopy
Checks that srcDir exists, that it is a directory and if srcDir and destDir are not equal.- Parameters:
srcDir- Source directorydestDir- Destination directory- Throws:
IOException- if any of the above conditions are not true.
-
checkFileCopy
Checks that file copy can occur.- Parameters:
srcFile- SourceFiledestFile- DestinationFile- Throws:
IOException- if srcFile does not exist or is not a file or srcFile and destFile are equal or cannot create ancestor directories.
-
encoding
Returns default encoding.
-
Files.isSymbolicLink(java.nio.file.Path)provides this functionality natively as of Java 1.7.