|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectcom.xmlmind.util.FileUtil
public final class FileUtil
A collection of utility functions (static methods) operating on Files.
| Field Summary | |
|---|---|
static java.io.File[] |
EMPTY_LIST
A ready-to-use empty list of Files. |
| Method Summary | |
|---|---|
static void |
checkedCreateNewFile(java.io.File file)
Like java.io.File.createNewFile() but raises an
IOException if this method returns false. |
static void |
checkedDelete(java.io.File file)
Like java.io.File.delete() but raises an IOException if
this method returns false. |
static java.lang.String[] |
checkedList(java.io.File dir)
Like java.io.File.list() but raises an IOException if this
method returns null. |
static java.lang.String[] |
checkedList(java.io.File dir,
java.io.FilenameFilter filter)
Like java.io.File.list(FilenameFilter) but raises an
IOException if this method returns null. |
static java.io.File[] |
checkedListFiles(java.io.File dir)
Like java.io.File.listFiles() but raises an IOException if
this method returns null. |
static java.io.File[] |
checkedListFiles(java.io.File dir,
java.io.FileFilter filter)
Like java.io.File.listFiles(FileFilter) but raises an
IOException if this method returns null. |
static java.io.File[] |
checkedListFiles(java.io.File dir,
java.io.FilenameFilter filter)
Like java.io.File.listFiles(FilenameFilter) but raises an
IOException if this method returns null. |
static void |
checkedMkdir(java.io.File dir)
Like java.io.File.mkdir() but raises an IOException if
this method returns false. |
static void |
checkedMkdirs(java.io.File dir)
Like java.io.File.mkdirs() but raises an IOException if
this method returns false. |
static void |
checkedRename(java.io.File from,
java.io.File to)
Like java.io.File.renameTo() but raises an IOException if
this method returns false. |
static void |
checkedSetLastModified(java.io.File file,
long date)
Like java.io.File.setLastModified() but raises an
IOException if this method returns false. |
static void |
checkIsEmptyDir(java.io.File file)
Check whether if specified file exists, is a directory and is empty. |
static void |
copyBytes(java.io.InputStream in,
java.io.OutputStream out)
Copies input to output. |
static void |
copyDir(java.io.File srcDir,
java.io.File dstDir,
boolean sameDate)
Recursively copies specified directory. |
static void |
copyDir(java.io.File srcDir,
java.io.File dstDir,
boolean sameDate,
Console console)
Recursively copies specified directory. |
static void |
copyFile(java.io.File srcFile,
java.io.File dstFile)
Copies specified file (not a directory). |
static void |
copyFile(java.io.File srcFile,
java.io.File dstFile,
boolean sameDate)
Copies specified file (not a directory). |
static void |
copyFile(java.io.File srcFile,
java.io.File dstFile,
boolean sameDate,
Console console)
Copies specified file (not a directory). |
static void |
copyFile(java.io.File srcFile,
java.io.OutputStream dst)
Copies specified file to specified destination. |
static void |
copyFile(java.io.InputStream src,
java.io.File dstFile)
Copies specified source to specified file. |
static void |
copyFile(java.io.InputStream in,
java.io.OutputStream out)
Copies input to output. |
static void |
copyFile(java.net.URL url,
java.io.File dstFile)
Copies the contents of specified URL to specified file. |
static void |
copyFileOrDir(java.io.File srcFileOrDir,
java.io.File dstFileOrDir,
boolean sameDate)
Copies specified file or directory. |
static void |
copyFileOrDir(java.io.File srcFileOrDir,
java.io.File dstFileOrDir,
boolean sameDate,
Console console)
Copies specified file or directory. |
static java.io.File |
createTempDirectory(java.lang.String prefix,
java.io.File directory)
Create a temporary directory. |
static void |
deleteDir(java.io.File dir)
Deletes specified directory. |
static void |
deleteDir(java.io.File dir,
Console console)
Deletes specified directory. |
static void |
deleteFile(java.io.File file)
Deletes specified file (not a directory). |
static void |
deleteFile(java.io.File file,
Console console)
Deletes specified file (not a directory). |
static void |
deleteFileOrDir(java.io.File fileOrDir)
Deletes specified file or directory. |
static void |
deleteFileOrDir(java.io.File fileOrDir,
Console console)
Deletes specified file or directory. |
static boolean |
doDeleteFileOrDir(java.io.File fileOrDir)
Deletes specified file or directory. |
static boolean |
doEmptyDir(java.io.File dir)
Removes all the files contained in specified directory. |
static void |
emptyDir(java.io.File dir)
Removes all the files contained in specified directory. |
static java.net.URI |
fileToURI(java.io.File file)
Similar to fileToURL(java.io.File), expect that it returns an URI. |
static java.net.URL |
fileToURL(java.io.File file)
Converts a File to a file: URL. |
static java.lang.String |
formatFileSize(long fileSize)
Equivalent to formatFileSize(fileSize, null). |
static java.lang.String |
formatFileSize(long fileSize,
java.util.Locale locale)
Returns a localized, formatted, form of specified file size. |
static java.lang.String |
getExtension(java.io.File file)
Returns the extension of specified file. |
static java.lang.String |
getExtension(java.lang.String path)
Similar to getExtension(File) except that it acts
on a filename rather than on a File. |
static java.lang.String |
getRelativePath(java.io.File file,
java.io.File base)
Returns the path of specified file relative to specified base. |
static java.lang.String |
getVolume(java.io.File file)
Returns the volume of specified file. |
static int |
indexOfDot(java.lang.String path,
char separatorChar)
Returns the extension of specified path. |
static boolean |
isEmptyDir(java.io.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(java.io.File file)
Loads the content of a binary file. |
static byte[] |
loadBytes(java.io.InputStream in)
Loads the content of an InputStream returning binary data. |
static java.lang.String |
loadString(java.io.File file)
Loads the content of a text file. |
static java.lang.String |
loadString(java.io.File file,
java.lang.String charsetName)
Loads the content of a text file. |
static java.lang.String |
loadString(java.io.InputStream stream,
java.lang.String charsetName)
Loads the content of an InputStream returning text. |
static void |
saveBytes(byte[] bytes,
java.io.File file)
Saves binary data to a file. |
static void |
saveString(java.lang.String string,
java.io.File file)
Saves some text to a file. |
static void |
saveString(java.lang.String string,
java.io.File file,
java.lang.String charsetName)
Saves some text to a file. |
static void |
saveString(java.lang.String string,
java.io.OutputStream stream,
java.lang.String charsetName)
Saves some text to an OutputStream. |
static java.io.File |
setExtension(java.io.File file,
java.lang.String extension)
Changes the extension of specified file to specified extension. |
static java.lang.String |
setExtension(java.lang.String path,
java.lang.String extension)
Similar to setExtension(File, String) except that it acts
on a filename rather than on a File. |
| Methods inherited from class java.lang.Object |
|---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static final java.io.File[] EMPTY_LIST
| Method Detail |
|---|
public static java.net.URI fileToURI(java.io.File file)
fileToURL(java.io.File), expect that it returns an URI.
URLUtil.urlToURI(java.net.URL)public static java.net.URL fileToURL(java.io.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 converted
URLUtil.urlToFile(java.net.URL)public static java.lang.String getExtension(java.io.File file)
null for "/tmp/test".
null for "~/.profile".
file - absolute or relative pathname possibly having an extension
null otherwisesetExtension(java.io.File, java.lang.String)public static java.lang.String getExtension(java.lang.String path)
getExtension(File) except that it acts
on a filename rather than on a File.
public static int indexOfDot(java.lang.String path,
char separatorChar)
path - an absolute or relative pathseparatorChar - the character used to separate path segments
null otherwise
public static java.io.File setExtension(java.io.File file,
java.lang.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 java.lang.String setExtension(java.lang.String path,
java.lang.String extension)
setExtension(File, String) except that it acts
on a filename rather than on a File.
public static java.lang.String getRelativePath(java.io.File file,
java.io.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 filename
public static java.lang.String getVolume(java.io.File file)
Returns something like "C:" or "\\server\share" on Windows. Returns "" on all the other platforms.
file - a relative or absolute filename
null if specified
filename is malformed (e.g. "\\server").
public static void checkedDelete(java.io.File file)
throws java.io.IOException
java.io.File.delete() but raises an IOException if
this method returns false.
java.io.IOException
public static void checkedRename(java.io.File from,
java.io.File to)
throws java.io.IOException
java.io.File.renameTo() but raises an IOException if
this method returns false.
java.io.IOException
public static void checkedMkdir(java.io.File dir)
throws java.io.IOException
java.io.File.mkdir() but raises an IOException if
this method returns false.
java.io.IOException
public static void checkedMkdirs(java.io.File dir)
throws java.io.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.
java.io.IOException
public static java.io.File[] checkedListFiles(java.io.File dir)
throws java.io.IOException
java.io.File.listFiles() but raises an IOException if
this method returns null.
java.io.IOException
public static java.io.File[] checkedListFiles(java.io.File dir,
java.io.FilenameFilter filter)
throws java.io.IOException
java.io.File.listFiles(FilenameFilter) but raises an
IOException if this method returns null.
java.io.IOException
public static java.io.File[] checkedListFiles(java.io.File dir,
java.io.FileFilter filter)
throws java.io.IOException
java.io.File.listFiles(FileFilter) but raises an
IOException if this method returns null.
java.io.IOException
public static java.lang.String[] checkedList(java.io.File dir)
throws java.io.IOException
java.io.File.list() but raises an IOException if this
method returns null.
java.io.IOException
public static java.lang.String[] checkedList(java.io.File dir,
java.io.FilenameFilter filter)
throws java.io.IOException
java.io.File.list(FilenameFilter) but raises an
IOException if this method returns null.
java.io.IOException
public static void checkedSetLastModified(java.io.File file,
long date)
throws java.io.IOException
java.io.File.setLastModified() but raises an
IOException if this method returns false.
java.io.IOException
public static void checkedCreateNewFile(java.io.File file)
throws java.io.IOException
java.io.File.createNewFile() but raises an
IOException if this method returns false.
java.io.IOException
public static java.io.File createTempDirectory(java.lang.String prefix,
java.io.File directory)
throws java.io.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 used
java.io.IOException - if the temporary directory cannot be createdpublic static java.lang.String formatFileSize(long fileSize)
formatFileSize(fileSize, null).
public static java.lang.String formatFileSize(long fileSize,
java.util.Locale locale)
For example, returns "56.5Gb" when passed 59279560 in the US locale.
public static void emptyDir(java.io.File dir)
throws java.io.IOException
dir - directory to be made empty
java.io.IOException - if, for any reason, the directory cannot be made
emptypublic static boolean doEmptyDir(java.io.File dir)
dir - directory to be made empty
true if the directory has been made empty;
false otherwisepublic static boolean isEmptyDir(java.io.File file)
true if specified file exists, is a directory and
is empty. Returns false otherwise.
checkIsEmptyDir(java.io.File)
public static void checkIsEmptyDir(java.io.File file)
throws java.io.IOException
java.io.IOExceptionisEmptyDir(java.io.File)
public static void deleteFileOrDir(java.io.File fileOrDir,
Console console)
throws java.io.IOException
fileOrDir - file or directory to be deletedconsole - a message describing the operation is displayed on this
console. May be null.
java.io.IOException - if, for any reason, specified file or directory
cannot be deleted
public static void deleteDir(java.io.File dir,
Console console)
throws java.io.IOException
dir - directory to be deletedconsole - a message describing the operation is displayed on this
console. May be null.
java.io.IOException - if, for any reason, specified directory cannot
be deleted
public static void deleteFile(java.io.File file,
Console console)
throws java.io.IOException
file - file to be deletedconsole - a message describing the operation is displayed on this
console. May be null.
java.io.IOException - if, for any reason, specified file cannot be
deleted
public static void deleteFileOrDir(java.io.File fileOrDir)
throws java.io.IOException
fileOrDir - file or directory to be deleted
java.io.IOException - if, for any reason, specified file or directory
cannot be deletedpublic static boolean doDeleteFileOrDir(java.io.File fileOrDir)
fileOrDir - file or directory to be deleted
true if specified file or directory has been
deleted; false otherwise
public static void deleteDir(java.io.File dir)
throws java.io.IOException
dir - directory to be deleted
java.io.IOException - if, for any reason, specified directory cannot
be deleted
public static void deleteFile(java.io.File file)
throws java.io.IOException
file - file to be deleted
java.io.IOException - if, for any reason, specified file cannot be
deleted
public static void copyFileOrDir(java.io.File srcFileOrDir,
java.io.File dstFileOrDir,
boolean sameDate,
Console console)
throws java.io.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.
java.io.IOException - if, for any reason, specified file or directory
cannot be copied
public static void copyDir(java.io.File srcDir,
java.io.File dstDir,
boolean sameDate,
Console console)
throws java.io.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.
java.io.IOException - if, for any reason, specified directory cannot
be copied
public static void copyFile(java.io.File srcFile,
java.io.File dstFile,
boolean sameDate,
Console console)
throws java.io.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.
java.io.IOException - if, for any reason, specified file cannot be
copied
public static void copyFileOrDir(java.io.File srcFileOrDir,
java.io.File dstFileOrDir,
boolean sameDate)
throws java.io.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.
java.io.IOException - if, for any reason, specified file or directory
cannot be copied
public static void copyDir(java.io.File srcDir,
java.io.File dstDir,
boolean sameDate)
throws java.io.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.
java.io.IOException - if, for any reason, specified directory cannot
be copied
public static void copyFile(java.io.File srcFile,
java.io.File dstFile,
boolean sameDate)
throws java.io.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.
java.io.IOException - if, for any reason, specified file cannot be
copied
public static void copyFile(java.io.File srcFile,
java.io.File dstFile)
throws java.io.IOException
srcFile - file to be copieddstFile - target of the copy operation
java.io.IOException - if, for any reason, specified file cannot be
copied
public static void copyFile(java.net.URL url,
java.io.File dstFile)
throws java.io.IOException
url - URL to be copieddstFile - destination file
java.io.IOException - if an I/O problem occurs
public static void copyFile(java.io.InputStream src,
java.io.File dstFile)
throws java.io.IOException
src - source streamdstFile - destination file
java.io.IOException - if an I/O problem occurs
public static void copyFile(java.io.File srcFile,
java.io.OutputStream dst)
throws java.io.IOException
srcFile - source filedst - destination stream
java.io.IOException - if an I/O problem occurs
public static final void copyFile(java.io.InputStream in,
java.io.OutputStream out)
throws java.io.IOException
java.io.IOExceptioncopyBytes(java.io.InputStream, java.io.OutputStream)
public static java.lang.String loadString(java.io.File file)
throws java.io.IOException
file - the text file
java.io.IOException - if there is an I/O problem
public static java.lang.String loadString(java.io.File file,
java.lang.String charsetName)
throws java.io.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
platform
java.io.IOException - if there is an I/O problem
public static java.lang.String loadString(java.io.InputStream stream,
java.lang.String charsetName)
throws java.io.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
platform
java.io.IOException - if there is an I/O problem
public static void saveString(java.lang.String string,
java.io.File file)
throws java.io.IOException
string - the text to be savedfile - the destination file
java.io.IOException - if there is an I/O problem
public static void saveString(java.lang.String string,
java.io.File file,
java.lang.String charsetName)
throws java.io.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
platform
java.io.IOException - if there is an I/O problem
public static void saveString(java.lang.String string,
java.io.OutputStream stream,
java.lang.String charsetName)
throws java.io.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
platform
java.io.IOException - if there is an I/O problempublic static final boolean isGzipped(byte[] bytes)
public static final void copyBytes(java.io.InputStream in,
java.io.OutputStream out)
throws java.io.IOException
java.io.IOExceptioncopyFile(InputStream, OutputStream)
public static byte[] loadBytes(java.io.File file)
throws java.io.IOException
file - the binary file
java.io.IOException - if there is an I/O problem
public static byte[] loadBytes(java.io.InputStream in)
throws java.io.IOException
in - the binary data source
java.io.IOException - if there is an I/O problem
public static void saveBytes(byte[] bytes,
java.io.File file)
throws java.io.IOException
bytes - the binary data to be savedfile - the destination file
java.io.IOException - if there is an I/O problem
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||