public final class FileUtil extends Object
Modifier and Type | Field and Description |
---|---|
static File[] |
EMPTY_LIST
A ready-to-use empty list of Files.
|
Modifier and Type | Method and Description |
---|---|
static void |
checkedCreateNewFile(File file)
Like
java.io.File.createNewFile() but raises an
IOException if this method returns false . |
static void |
checkedDelete(File file)
Like
java.io.File.delete() but raises an IOException if
this method returns false . |
static String[] |
checkedList(File dir)
Like
java.io.File.list() but raises an IOException if this
method returns null . |
static String[] |
checkedList(File dir,
FilenameFilter filter)
Like
java.io.File.list(FilenameFilter) but raises an
IOException if this method returns null . |
static File[] |
checkedListFiles(File dir)
Like
java.io.File.listFiles() but raises an IOException if
this method returns null . |
static File[] |
checkedListFiles(File dir,
FileFilter filter)
Like
java.io.File.listFiles(FileFilter) but raises an
IOException if this method returns null . |
static File[] |
checkedListFiles(File dir,
FilenameFilter filter)
Like
java.io.File.listFiles(FilenameFilter) but raises an
IOException if this method returns null . |
static void |
checkedMkdir(File dir)
Like
java.io.File.mkdir() but raises an IOException if
this method returns false . |
static void |
checkedMkdirs(File dir)
Like
java.io.File.mkdirs() but raises an IOException if
this method returns false . |
static void |
checkedRename(File from,
File to)
Like
java.io.File.renameTo() but raises an IOException if
this method returns false . |
static void |
checkedSetLastModified(File file,
long date)
Like
java.io.File.setLastModified() but raises an
IOException if this method returns false . |
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 to
fileToURL(java.io.File) , expect that it returns an URI. |
static URL |
fileToURL(File file)
Converts a File to a file: URL.
|
static String |
formatFileSize(long fileSize)
Equivalent to
formatFileSize(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 to
getExtension(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)
Returns
true 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 to
setExtension(File, String) except that it acts
on a filename rather than on a File. |
public static final File[] EMPTY_LIST
public static URI fileToURI(File file)
fileToURL(java.io.File)
, expect that it returns an URI.URLUtil.urlToURI(java.net.URL)
public static URL fileToURL(File file)
On Windows, this function supports UNC filenames. For example, it
converts "\\foo\bar\gee.txt" to
"file://foo/bar/gee.txt".
(Note that URL.openStream
works fine on such URL.)
file
- the file to be convertedURLUtil.urlToFile(java.net.URL)
public static String getExtension(File file)
null
for "/tmp/test".
null
for "~/.profile".
file
- absolute or relative pathname possibly having an extensionnull
otherwisesetExtension(java.io.File, java.lang.String)
public static String getExtension(String path)
getExtension(File)
except that it acts
on a filename rather than on a File.public static int indexOfDot(String path, char separatorChar)
path
- an absolute or relative pathseparatorChar
- the character used to separate path segmentsnull
otherwisepublic static File setExtension(File file, String extension)
getExtension(java.io.File)
for a description of the extension of a pathname.file
- absolute or relative pathnameextension
- new extension. May be null
which means:
remove the extension.Returns same pathname if specified pathname ends with
File.separator
.
public static String setExtension(String path, String extension)
setExtension(File, String)
except that it acts
on a filename rather than on a File.public static String getRelativePath(File file, File base)
Example: returns ../local/bin/html2ps for /usr/local/bin/html2ps relative to /usr/bin/grep.
file
- a relative or absolute filenamebase
- another a relative or absolute filenamepublic static String getVolume(File file)
Returns something like "C:" or "\\server\share" on Windows. Returns "" on all the other platforms.
file
- a relative or absolute filenamenull
if specified
filename is malformed (e.g. "\\server").public static void checkedDelete(File file) throws IOException
java.io.File.delete()
but raises an IOException if
this method returns false
.IOException
public static void checkedRename(File from, File to) throws IOException
java.io.File.renameTo()
but raises an IOException if
this method returns false
.IOException
public static void checkedMkdir(File dir) throws IOException
java.io.File.mkdir()
but raises an IOException if
this method returns false
.IOException
public static void checkedMkdirs(File dir) throws IOException
java.io.File.mkdirs()
but raises an IOException if
this method returns false
.
Unlike java.io.File.mkdirs()
which does not seem
to be thread-safe, this variant is thread-safe.
IOException
public static File[] checkedListFiles(File dir) throws IOException
java.io.File.listFiles()
but raises an IOException if
this method returns null
.IOException
public static File[] checkedListFiles(File dir, FilenameFilter filter) throws IOException
java.io.File.listFiles(FilenameFilter)
but raises an
IOException if this method returns null
.IOException
public static File[] checkedListFiles(File dir, FileFilter filter) throws IOException
java.io.File.listFiles(FileFilter)
but raises an
IOException if this method returns null
.IOException
public static String[] checkedList(File dir) throws IOException
java.io.File.list()
but raises an IOException if this
method returns null
.IOException
public static String[] checkedList(File dir, FilenameFilter filter) throws IOException
java.io.File.list(FilenameFilter)
but raises an
IOException if this method returns null
.IOException
public static void checkedSetLastModified(File file, long date) throws IOException
java.io.File.setLastModified()
but raises an
IOException if this method returns false
.IOException
public static void checkedCreateNewFile(File file) throws IOException
java.io.File.createNewFile()
but raises an
IOException if this method returns false
.IOException
public static File createTempDirectory(String prefix, File directory) throws IOException
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, or null
if the default
temporary-file directory is to be usedIOException
- if the temporary directory cannot be createdpublic static String formatFileSize(long fileSize)
formatFileSize(fileSize, null)
.public static String formatFileSize(long fileSize, Locale locale)
For example, returns "56.5Gb" when passed 59279560 in the US locale.
public static void emptyDir(File dir) throws IOException
dir
- directory to be made emptyIOException
- if, for any reason, the directory cannot be made
emptypublic static boolean doEmptyDir(File dir)
dir
- directory to be made emptytrue
if the directory has been made empty;
false
otherwisepublic static boolean isEmptyDir(File file)
true
if specified file exists, is a directory and
is empty. Returns false
otherwise.checkIsEmptyDir(java.io.File)
public static void checkIsEmptyDir(File file) throws IOException
IOException
isEmptyDir(java.io.File)
public static void deleteFileOrDir(File fileOrDir, Console console) throws IOException
fileOrDir
- file or directory to be deletedconsole
- a message describing the operation is displayed on this
console. May be null
.IOException
- if, for any reason, specified file or directory
cannot be deletedpublic static void deleteDir(File dir, Console console) throws IOException
dir
- directory to be deletedconsole
- a message describing the operation is displayed on this
console. May be null
.IOException
- if, for any reason, specified directory cannot
be deletedpublic static void deleteFile(File file, Console console) throws IOException
file
- file to be deletedconsole
- a message describing the operation is displayed on this
console. May be null
.IOException
- if, for any reason, specified file cannot be
deletedpublic static void deleteFileOrDir(File fileOrDir) throws IOException
fileOrDir
- file or directory to be deletedIOException
- if, for any reason, specified file or directory
cannot be deletedpublic static boolean doDeleteFileOrDir(File fileOrDir)
fileOrDir
- file or directory to be deletedtrue
if specified file or directory has been
deleted; false
otherwisepublic static void deleteDir(File dir) throws IOException
dir
- directory to be deletedIOException
- if, for any reason, specified directory cannot
be deletedpublic static void deleteFile(File file) throws IOException
file
- file to be deletedIOException
- if, for any reason, specified file cannot be
deletedpublic static void copyFileOrDir(File srcFileOrDir, File dstFileOrDir, boolean sameDate, Console console) throws IOException
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.
srcFileOrDir
- file or directory to be copieddstFileOrDir
- target of the copy operationsameDate
- if true
, a copied file has the same last
modified date as the original file. If false
, 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 be null
.IOException
- if, for any reason, specified file or directory
cannot be copiedpublic static void copyDir(File srcDir, File dstDir, boolean sameDate, Console console) throws IOException
srcDir
- directory to be copieddstDir
- target of the copy operationsameDate
- if true
, a copied file has the same last
modified date as the original file. If false
, 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 be null
.IOException
- if, for any reason, specified directory cannot
be copiedpublic static void copyFile(File srcFile, File dstFile, boolean sameDate, Console console) throws IOException
srcFile
- file to be copieddstFile
- target of the copy operationsameDate
- if true
, a copied file has the same last
modified date as the original file. If false
, 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 be null
.IOException
- if, for any reason, specified file cannot be
copiedpublic static void copyFileOrDir(File srcFileOrDir, File dstFileOrDir, boolean sameDate) throws IOException
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.
srcFileOrDir
- file or directory to be copieddstFileOrDir
- target of the copy operationsameDate
- if true
, a copied file has the same last
modified date as the original file. If false
, a copied
file has the date of its creation as its last modified date.IOException
- if, for any reason, specified file or directory
cannot be copiedpublic static void copyDir(File srcDir, File dstDir, boolean sameDate) throws IOException
srcDir
- directory to be copieddstDir
- target of the copy operationsameDate
- if true
, a copied file has the same last
modified date as the original file. If false
, a copied
file has the date of its creation as its last modified date.IOException
- if, for any reason, specified directory cannot
be copiedpublic static void copyFile(File srcFile, File dstFile, boolean sameDate) throws IOException
srcFile
- file to be copieddstFile
- target of the copy operationsameDate
- if true
, a copied file has the same last
modified date as the original file. If false
, a copied
file has the date of its creation as its last modified date.IOException
- if, for any reason, specified file cannot be
copiedpublic static void copyFile(File srcFile, File dstFile) throws IOException
srcFile
- file to be copieddstFile
- target of the copy operationIOException
- if, for any reason, specified file cannot be
copiedpublic static void copyFile(URL url, File dstFile) throws IOException
url
- URL to be copieddstFile
- destination fileIOException
- if an I/O problem occurspublic static void copyFile(InputStream src, File dstFile) throws IOException
src
- source streamdstFile
- destination fileIOException
- if an I/O problem occurspublic static void copyFile(File srcFile, OutputStream dst) throws IOException
srcFile
- source filedst
- destination streamIOException
- if an I/O problem occurspublic static final void copyFile(InputStream in, OutputStream out) throws IOException
IOException
copyBytes(java.io.InputStream, java.io.OutputStream)
public static String loadString(File file) throws IOException
file
- the text fileIOException
- if there is an I/O problempublic static String loadString(File file, String charsetName) throws IOException
file
- the text filecharsetName
- the IANA charset of the text source if known;
null
may be used to specify the native encoding of the
platformIOException
- if there is an I/O problempublic static String loadString(InputStream stream, String charsetName) throws IOException
stream
- the text sourcecharsetName
- the IANA charset of the text source if known;
null
may be used to specify the native encoding of the
platformIOException
- if there is an I/O problempublic static String loadString(Reader in) throws IOException
in
- the text sourceIOException
- if there is an I/O problempublic static void saveString(String string, File file) throws IOException
string
- the text to be savedfile
- the destination fileIOException
- if there is an I/O problempublic static void saveString(String string, File file, String charsetName) throws IOException
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
platformIOException
- if there is an I/O problempublic static void saveString(String string, OutputStream stream, String charsetName) throws IOException
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
platformIOException
- if there is an I/O problempublic static final boolean isGzipped(byte[] bytes)
public static final void copyBytes(InputStream in, OutputStream out) throws IOException
IOException
copyFile(InputStream, OutputStream)
public static byte[] loadBytes(File file) throws IOException
file
- the binary fileIOException
- if there is an I/O problempublic static byte[] loadBytes(InputStream in) throws IOException
in
- the binary data sourceIOException
- if there is an I/O problempublic static void saveBytes(byte[] bytes, File file) throws IOException
bytes
- the binary data to be savedfile
- the destination fileIOException
- if there is an I/O problem