Class FileUtil
- java.lang.Object
-
- com.xmlmind.util.FileUtil
-
public final class FileUtil extends Object
A collection of utility functions (static methods) operating on Files.
-
-
Field Summary
Fields Modifier and Type Field Description static File[]
EMPTY_LIST
A ready-to-use empty list of Files.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
checkedCreateNewFile(File file)
Likejava.io.File.createNewFile()
but raises an IOException if this method returnsfalse
.static void
checkedDelete(File file)
Likejava.io.File.delete()
but raises an IOException if this method returnsfalse
.static String[]
checkedList(File dir)
Likejava.io.File.list()
but raises an IOException if this method returnsnull
.static String[]
checkedList(File dir, FilenameFilter filter)
Likejava.io.File.list(FilenameFilter)
but raises an IOException if this method returnsnull
.static File[]
checkedListFiles(File dir)
Likejava.io.File.listFiles()
but raises an IOException if this method returnsnull
.static File[]
checkedListFiles(File dir, FileFilter filter)
Likejava.io.File.listFiles(FileFilter)
but raises an IOException if this method returnsnull
.static File[]
checkedListFiles(File dir, FilenameFilter filter)
Likejava.io.File.listFiles(FilenameFilter)
but raises an IOException if this method returnsnull
.static void
checkedMkdir(File dir)
Likejava.io.File.mkdir()
but raises an IOException if this method returnsfalse
.static void
checkedMkdirs(File dir)
Likejava.io.File.mkdirs()
but raises an IOException if this method returnsfalse
.static void
checkedRename(File from, File to)
Likejava.io.File.renameTo()
but raises an IOException if this method returnsfalse
.static void
checkedSetLastModified(File file, long date)
Likejava.io.File.setLastModified()
but raises an IOException if this method returnsfalse
.static void
checkIsEmptyDir(File file)
Check whether if specified file exists, is a directory and is empty.static void
copyBytes(InputStream in, OutputStream out)
Copies input to output.static void
copyDir(File srcDir, File dstDir, boolean sameDate)
Recursively copies specified directory.static void
copyDir(File srcDir, File dstDir, boolean sameDate, Console console)
Recursively copies specified directory.static void
copyFile(File srcFile, File dstFile)
Copies specified file (not a directory).static void
copyFile(File srcFile, File dstFile, boolean sameDate)
Copies specified file (not a directory).static void
copyFile(File srcFile, File dstFile, boolean sameDate, Console console)
Copies specified file (not a directory).static void
copyFile(File srcFile, OutputStream dst)
Copies specified file to specified destination.static void
copyFile(InputStream src, File dstFile)
Copies specified source to specified file.static void
copyFile(InputStream in, OutputStream out)
Copies input to output.static void
copyFile(URL url, File dstFile)
Copies the contents of specified URL to specified file.static void
copyFileOrDir(File srcFileOrDir, File dstFileOrDir, boolean sameDate)
Copies specified file or directory.static void
copyFileOrDir(File srcFileOrDir, File dstFileOrDir, boolean sameDate, Console console)
Copies specified file or directory.static File
createTempDirectory(String prefix, File directory)
Create a temporary directory.static void
deleteDir(File dir)
Deletes specified directory.static void
deleteDir(File dir, Console console)
Deletes specified directory.static void
deleteFile(File file)
Deletes specified file (not a directory).static void
deleteFile(File file, Console console)
Deletes specified file (not a directory).static void
deleteFileOrDir(File fileOrDir)
Deletes specified file or directory.static void
deleteFileOrDir(File fileOrDir, Console console)
Deletes specified file or directory.static boolean
doDeleteFileOrDir(File fileOrDir)
Deletes specified file or directory.static boolean
doEmptyDir(File dir)
Removes all the files contained in specified directory.static void
emptyDir(File dir)
Removes all the files contained in specified directory.static URI
fileToURI(File file)
Similar tofileToURL(java.io.File)
, expect that it returns an URI.static URL
fileToURL(File file)
Converts a File to afile:
URL.static String
formatFileSize(long fileSize)
Equivalent toformatFileSize(fileSize, null)
.static String
formatFileSize(long fileSize, Locale locale)
Returns a localized, formatted, form of specified file size.static String
getExtension(File file)
Returns the extension of specified file.static String
getExtension(String path)
Similar togetExtension(File)
except that it acts on a filename rather than on a File.static String
getRelativePath(File file, File base)
Returns the path of specified file relative to specified base.static String
getVolume(File file)
Returns the volume of specified file.static int
indexOfDot(String path, char separatorChar)
Returns the extension of specified path.static boolean
isEmptyDir(File file)
Returnstrue
if specified file exists, is a directory and is empty.static boolean
isGzipped(byte[] bytes)
Tests if specified data has been compressed using gzip.static byte[]
loadBytes(File file)
Loads the content of a binary file.static byte[]
loadBytes(InputStream in)
Loads the content of an InputStream returning binary data.static String
loadString(File file)
Loads the content of a text file.static String
loadString(File file, String charsetName)
Loads the content of a text file.static String
loadString(InputStream stream, String charsetName)
Loads text using specified stream.static String
loadString(Reader in)
Loads text using specified reader.static void
saveBytes(byte[] bytes, File file)
Saves binary data to a file.static void
saveString(String string, File file)
Saves some text to a file.static void
saveString(String string, File file, String charsetName)
Saves some text to a file.static void
saveString(String string, OutputStream stream, String charsetName)
Saves some text to an OutputStream.static File
setExtension(File file, String extension)
Changes the extension of specified file to specified extension.static String
setExtension(String path, String extension)
Similar tosetExtension(File, String)
except that it acts on a filename rather than on a File.
-
-
-
Field Detail
-
EMPTY_LIST
public static final File[] EMPTY_LIST
A ready-to-use empty list of Files.
-
-
Method Detail
-
fileToURI
public static URI fileToURI(File file)
Similar tofileToURL(java.io.File)
, expect that it returns an URI.- See Also:
URLUtil.urlToURI(java.net.URL)
-
fileToURL
public static URL fileToURL(File file)
Converts a File to afile:
URL. Ends with a '/' if specified file is a directory.On Windows, this function supports UNC filenames. For example, it converts "
\\foo\bar\gee.txt
" to "file://foo/bar/gee.txt
". (Note thatURL.openStream
works fine on such URL.)- Parameters:
file
- the file to be converted- Returns:
- a
file:
URL - See Also:
URLUtil.urlToFile(java.net.URL)
-
getExtension
public static String getExtension(File file)
Returns the extension of specified file. To make it simple, the substring after last '.', not including last '.'.- Returns
null
for "/tmp/test
". - Returns the empty string for "
/tmp/test.
". - Returns
null
for "~/.profile
". - Returns "
gz
" for "/tmp/test.tar.gz
".
- Parameters:
file
- absolute or relative pathname possibly having an extension- Returns:
- extension if any;
null
otherwise - See Also:
setExtension(java.io.File, java.lang.String)
- Returns
-
getExtension
public static String getExtension(String path)
Similar togetExtension(File)
except that it acts on a filename rather than on a File.
-
indexOfDot
public static int indexOfDot(String path, char separatorChar)
Returns the extension of specified path. To make it simple, the substring after last '.', not including last '.'.- Parameters:
path
- an absolute or relative pathseparatorChar
- the character used to separate path segments- Returns:
- extension if any;
null
otherwise
-
setExtension
public static File setExtension(File file, String extension)
Changes the extension of specified file to specified extension. SeegetExtension(java.io.File)
for a description of the extension of a pathname.- Parameters:
file
- absolute or relative pathnameextension
- new extension. May benull
which means: remove the extension.- Returns:
- a pathname identical to
file
except that its extension has been changed or removed.Returns same pathname if specified pathname ends with
File.separator
.
-
setExtension
public static String setExtension(String path, String extension)
Similar tosetExtension(File, String)
except that it acts on a filename rather than on a File.
-
getRelativePath
public static String getRelativePath(File file, File base)
Returns the path of specified file relative to specified base.Example: returns
../local/bin/html2ps
for/usr/local/bin/html2ps
relative to/usr/bin/grep
.- Parameters:
file
- a relative or absolute filenamebase
- another a relative or absolute filename- Returns:
- first filename as a filename relative to the second filename. Returns first filename as is, if the relative filename cannot be computed.
-
getVolume
public static String getVolume(File file)
Returns the volume of specified file.Returns something like "C:" or "\\server\share" on Windows. Returns "" on all the other platforms.
- Parameters:
file
- a relative or absolute filename- Returns:
- the volume of specified file or
null
if specified filename is malformed (e.g. "\\server").
-
checkedDelete
public static void checkedDelete(File file) throws IOException
Likejava.io.File.delete()
but raises an IOException if this method returnsfalse
.- Throws:
IOException
-
checkedRename
public static void checkedRename(File from, File to) throws IOException
Likejava.io.File.renameTo()
but raises an IOException if this method returnsfalse
.- Throws:
IOException
-
checkedMkdir
public static void checkedMkdir(File dir) throws IOException
Likejava.io.File.mkdir()
but raises an IOException if this method returnsfalse
.- Throws:
IOException
-
checkedMkdirs
public static void checkedMkdirs(File dir) throws IOException
Likejava.io.File.mkdirs()
but raises an IOException if this method returnsfalse
.Unlike
java.io.File.mkdirs()
which does not seem to be thread-safe, this variant is thread-safe.- Throws:
IOException
-
checkedListFiles
public static File[] checkedListFiles(File dir) throws IOException
Likejava.io.File.listFiles()
but raises an IOException if this method returnsnull
.- Throws:
IOException
-
checkedListFiles
public static File[] checkedListFiles(File dir, FilenameFilter filter) throws IOException
Likejava.io.File.listFiles(FilenameFilter)
but raises an IOException if this method returnsnull
.- Throws:
IOException
-
checkedListFiles
public static File[] checkedListFiles(File dir, FileFilter filter) throws IOException
Likejava.io.File.listFiles(FileFilter)
but raises an IOException if this method returnsnull
.- Throws:
IOException
-
checkedList
public static String[] checkedList(File dir) throws IOException
Likejava.io.File.list()
but raises an IOException if this method returnsnull
.- Throws:
IOException
-
checkedList
public static String[] checkedList(File dir, FilenameFilter filter) throws IOException
Likejava.io.File.list(FilenameFilter)
but raises an IOException if this method returnsnull
.- Throws:
IOException
-
checkedSetLastModified
public static void checkedSetLastModified(File file, long date) throws IOException
Likejava.io.File.setLastModified()
but raises an IOException if this method returnsfalse
.- Throws:
IOException
-
checkedCreateNewFile
public static void checkedCreateNewFile(File file) throws IOException
Likejava.io.File.createNewFile()
but raises an IOException if this method returnsfalse
.- Throws:
IOException
-
createTempDirectory
public static File createTempDirectory(String prefix, File directory) throws IOException
Create a temporary directory.- Parameters:
prefix
- the prefix string to be used in generating the diretory name; must be at least three characters longdirectory
- the directory in which the new directory is to be created, ornull
if the default temporary-file directory is to be used- Returns:
- the newly created temporary directory
- Throws:
IOException
- if the temporary directory cannot be created
-
formatFileSize
public static String formatFileSize(long fileSize)
Equivalent toformatFileSize(fileSize, null)
.
-
formatFileSize
public static String formatFileSize(long fileSize, Locale locale)
Returns a localized, formatted, form of specified file size.For example, returns "56.5Gb" when passed 59279560 in the US locale.
-
emptyDir
public static void emptyDir(File dir) throws IOException
Removes all the files contained in specified directory.- Parameters:
dir
- directory to be made empty- Throws:
IOException
- if, for any reason, the directory cannot be made empty
-
doEmptyDir
public static boolean doEmptyDir(File dir)
Removes all the files contained in specified directory.- Parameters:
dir
- directory to be made empty- Returns:
true
if the directory has been made empty;false
otherwise
-
isEmptyDir
public static boolean isEmptyDir(File file)
Returnstrue
if specified file exists, is a directory and is empty. Returnsfalse
otherwise.- See Also:
checkIsEmptyDir(java.io.File)
-
checkIsEmptyDir
public static void checkIsEmptyDir(File file) throws IOException
Check whether if specified file exists, is a directory and is empty. If this is not the case, throw anIOException
.- Throws:
IOException
- See Also:
isEmptyDir(java.io.File)
-
deleteFileOrDir
public static void deleteFileOrDir(File fileOrDir, Console console) throws IOException
Deletes specified file or directory. A directory is recursively deleted.- Parameters:
fileOrDir
- file or directory to be deletedconsole
- a message describing the operation is displayed on this console. May benull
.- Throws:
IOException
- if, for any reason, specified file or directory cannot be deleted
-
deleteDir
public static void deleteDir(File dir, Console console) throws IOException
Deletes specified directory. A directory is recursively deleted.- Parameters:
dir
- directory to be deletedconsole
- a message describing the operation is displayed on this console. May benull
.- Throws:
IOException
- if, for any reason, specified directory cannot be deleted
-
deleteFile
public static void deleteFile(File file, Console console) throws IOException
Deletes specified file (not a directory).- Parameters:
file
- file to be deletedconsole
- a message describing the operation is displayed on this console. May benull
.- Throws:
IOException
- if, for any reason, specified file cannot be deleted
-
deleteFileOrDir
public static void deleteFileOrDir(File fileOrDir) throws IOException
Deletes specified file or directory. A directory is recursively deleted.- Parameters:
fileOrDir
- file or directory to be deleted- Throws:
IOException
- if, for any reason, specified file or directory cannot be deleted
-
doDeleteFileOrDir
public static boolean doDeleteFileOrDir(File fileOrDir)
Deletes specified file or directory. A directory is recursively deleted.- Parameters:
fileOrDir
- file or directory to be deleted- Returns:
true
if specified file or directory has been deleted;false
otherwise
-
deleteDir
public static void deleteDir(File dir) throws IOException
Deletes specified directory. A directory is recursively deleted.- Parameters:
dir
- directory to be deleted- Throws:
IOException
- if, for any reason, specified directory cannot be deleted
-
deleteFile
public static void deleteFile(File file) throws IOException
Deletes specified file (not a directory).- Parameters:
file
- file to be deleted- Throws:
IOException
- if, for any reason, specified file cannot be deleted
-
copyFileOrDir
public static void copyFileOrDir(File srcFileOrDir, File dstFileOrDir, boolean sameDate, Console console) throws IOException
Copies specified file or directory. Directories are recursively copied.If a directory is to be copied, the target of the copy operation must not exist. If a file is to be copied and the target of the copy operation already exists, this target is overwritten.
- Parameters:
srcFileOrDir
- file or directory to be copieddstFileOrDir
- target of the copy operationsameDate
- iftrue
, a copied file has the same last modified date as the original file. Iffalse
, a copied file has the date of its creation as its last modified date.console
- a message describing the operation is displayed on this console. May benull
.- Throws:
IOException
- if, for any reason, specified file or directory cannot be copied
-
copyDir
public static void copyDir(File srcDir, File dstDir, boolean sameDate, Console console) throws IOException
Recursively copies specified directory. The target of the copy operation must not exist.- Parameters:
srcDir
- directory to be copieddstDir
- target of the copy operationsameDate
- iftrue
, a copied file has the same last modified date as the original file. Iffalse
, a copied file has the date of its creation as its last modified date.console
- a message describing the operation is displayed on this console. May benull
.- Throws:
IOException
- if, for any reason, specified directory cannot be copied
-
copyFile
public static void copyFile(File srcFile, File dstFile, boolean sameDate, Console console) throws IOException
Copies specified file (not a directory). If the target of the copy operation already exists, this target is overwritten.- Parameters:
srcFile
- file to be copieddstFile
- target of the copy operationsameDate
- iftrue
, a copied file has the same last modified date as the original file. Iffalse
, a copied file has the date of its creation as its last modified date.console
- a message describing the operation is displayed on this console. May benull
.- Throws:
IOException
- if, for any reason, specified file cannot be copied
-
copyFileOrDir
public static void copyFileOrDir(File srcFileOrDir, File dstFileOrDir, boolean sameDate) throws IOException
Copies specified file or directory. Directories are recursively copied.If a directory is to be copied, the target of the copy operation must not exist. If a file is to be copied and the target of the copy operation already exists, this target is overwritten.
- Parameters:
srcFileOrDir
- file or directory to be copieddstFileOrDir
- target of the copy operationsameDate
- iftrue
, a copied file has the same last modified date as the original file. Iffalse
, a copied file has the date of its creation as its last modified date.- Throws:
IOException
- if, for any reason, specified file or directory cannot be copied
-
copyDir
public static void copyDir(File srcDir, File dstDir, boolean sameDate) throws IOException
Recursively copies specified directory. The target of the copy operation must not exist.- Parameters:
srcDir
- directory to be copieddstDir
- target of the copy operationsameDate
- iftrue
, a copied file has the same last modified date as the original file. Iffalse
, a copied file has the date of its creation as its last modified date.- Throws:
IOException
- if, for any reason, specified directory cannot be copied
-
copyFile
public static void copyFile(File srcFile, File dstFile, boolean sameDate) throws IOException
Copies specified file (not a directory). If the target of the copy operation already exists, this target is overwritten.- Parameters:
srcFile
- file to be copieddstFile
- target of the copy operationsameDate
- iftrue
, a copied file has the same last modified date as the original file. Iffalse
, a copied file has the date of its creation as its last modified date.- Throws:
IOException
- if, for any reason, specified file cannot be copied
-
copyFile
public static void copyFile(File srcFile, File dstFile) throws IOException
Copies specified file (not a directory). If the target of the copy operation already exists, this target is overwritten.- Parameters:
srcFile
- file to be copieddstFile
- target of the copy operation- Throws:
IOException
- if, for any reason, specified file cannot be copied
-
copyFile
public static void copyFile(URL url, File dstFile) throws IOException
Copies the contents of specified URL to specified file.- Parameters:
url
- URL to be copieddstFile
- destination file- Throws:
IOException
- if an I/O problem occurs
-
copyFile
public static void copyFile(InputStream src, File dstFile) throws IOException
Copies specified source to specified file.- Parameters:
src
- source streamdstFile
- destination file- Throws:
IOException
- if an I/O problem occurs
-
copyFile
public static void copyFile(File srcFile, OutputStream dst) throws IOException
Copies specified file to specified destination.- Parameters:
srcFile
- source filedst
- destination stream- Throws:
IOException
- if an I/O problem occurs
-
copyFile
public static final void copyFile(InputStream in, OutputStream out) throws IOException
Copies input to output. Does not close streams after copying the data.- Throws:
IOException
- See Also:
copyBytes(java.io.InputStream, java.io.OutputStream)
-
loadString
public static String loadString(File file) throws IOException
Loads the content of a text file. The encoding of the text is assumed to be the native encoding of the platform.- Parameters:
file
- the text file- Returns:
- the loaded String
- Throws:
IOException
- if there is an I/O problem
-
loadString
public static String loadString(File file, String charsetName) throws IOException
Loads the content of a text file. The encoding of the text is assumed to be the native encoding of the platform.- Parameters:
file
- the text filecharsetName
- the IANA charset of the text source if known;null
may be used to specify the native encoding of the platform- Returns:
- the loaded String
- Throws:
IOException
- if there is an I/O problem
-
loadString
public static String loadString(InputStream stream, String charsetName) throws IOException
Loads text using specified stream.- Parameters:
stream
- the text sourcecharsetName
- the IANA charset of the text source if known;null
may be used to specify the native encoding of the platform- Returns:
- the loaded String
- Throws:
IOException
- if there is an I/O problem
-
loadString
public static String loadString(Reader in) throws IOException
Loads text using specified reader.- Parameters:
in
- the text source- Returns:
- the loaded String
- Throws:
IOException
- if there is an I/O problem
-
saveString
public static void saveString(String string, File file) throws IOException
Saves some text to a file.- Parameters:
string
- the text to be savedfile
- the destination file- Throws:
IOException
- if there is an I/O problem
-
saveString
public static void saveString(String string, File file, String charsetName) throws IOException
Saves some text to a file.- Parameters:
string
- the text to be savedfile
- the destination filecharsetName
- the IANA charset of the saved file;null
may be used to specify the native encoding of the platform- Throws:
IOException
- if there is an I/O problem
-
saveString
public static void saveString(String string, OutputStream stream, String charsetName) throws IOException
Saves some text to an OutputStream.- Parameters:
string
- the text to be savedstream
- the text sinkcharsetName
- the IANA charset of the saved characters;null
may be used to specify the native encoding of the platform- Throws:
IOException
- if there is an I/O problem
-
isGzipped
public static final boolean isGzipped(byte[] bytes)
Tests if specified data has been compressed using gzip.
-
copyBytes
public static final void copyBytes(InputStream in, OutputStream out) throws IOException
Copies input to output. Closes both streams after copying the data.- Throws:
IOException
- See Also:
copyFile(InputStream, OutputStream)
-
loadBytes
public static byte[] loadBytes(File file) throws IOException
Loads the content of a binary file.- Parameters:
file
- the binary file- Returns:
- the loaded bytes
- Throws:
IOException
- if there is an I/O problem
-
loadBytes
public static byte[] loadBytes(InputStream in) throws IOException
Loads the content of an InputStream returning binary data.- Parameters:
in
- the binary data source- Returns:
- the loaded bytes
- Throws:
IOException
- if there is an I/O problem
-
saveBytes
public static void saveBytes(byte[] bytes, File file) throws IOException
Saves binary data to a file.- Parameters:
bytes
- the binary data to be savedfile
- the destination file- Throws:
IOException
- if there is an I/O problem
-
-