|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.twelvemonkeys.io.FileUtil
public final class FileUtil
A utility class with some useful file and i/o related methods.
Versions exists take Input and OutputStreams as parameters, to allow for copying streams (URL's etc.).
| Field Summary | |
|---|---|
static int |
BUF_SIZE
The size of the buffer used for copying |
| Method Summary | |
|---|---|
static void |
close(java.io.InputStream pInput)
Tries to close the given stream. |
static void |
close(java.io.OutputStream pOutput)
Tries to close the given stream. |
static boolean |
copy(java.io.File pFromFile,
java.io.File pToFile)
Copies the fromFile to the toFile location. |
static boolean |
copy(java.io.File pFromFile,
java.io.File pToFile,
boolean pOverWrite)
Copies the fromFile to the toFile location. |
static boolean |
copy(java.io.InputStream pFrom,
java.io.OutputStream pTo)
Copies all data from one stream to another. |
static boolean |
copy(java.lang.String pFromFileName,
java.lang.String pToFileName)
Copies the fromFile to the toFile location. |
static boolean |
copy(java.lang.String pFromFileName,
java.lang.String pToFileName,
boolean pOverWrite)
Copies the fromFile to the toFile location. |
static boolean |
delete(java.io.File pFile)
Deletes the specified file. |
static boolean |
delete(java.io.File pFile,
boolean pForce)
Deletes the specified file. |
static boolean |
delete(java.lang.String pFilename)
Deletes the specified file. |
static boolean |
delete(java.lang.String pFilename,
boolean pForce)
Deletes the specified file. |
static java.lang.String |
getBasename(java.io.File pFile)
Gets the file name of the given file, without the extension (type). |
static java.lang.String |
getBasename(java.lang.String pFileName)
Gets the file name of the given file, without the extension (type). |
static java.lang.String |
getBasename0(java.lang.String pFileName)
|
static java.lang.String |
getDirectoryname(java.lang.String pPath)
Extracts the directory path without the filename, from a complete filename path. |
static java.lang.String |
getDirectoryname(java.lang.String pPath,
char pSeparator)
Extracts the directory path without the filename, from a complete filename path. |
static java.lang.String |
getExtension(java.io.File pFile)
Gets the file (type) extension of the given file. |
static java.lang.String |
getExtension(java.lang.String pFileName)
Gets the file (type) extension of the given file. |
static java.lang.String |
getFilename(java.lang.String pPath)
Extracts the filename of a complete filename path. |
static java.lang.String |
getFilename(java.lang.String pPath,
char pSeparator)
Extracts the filename of a complete filename path. |
static long |
getFreeSpace(java.io.File pPath)
|
static java.lang.String |
getTempDir()
Gets the default temp directory for the system. |
static java.io.File |
getTempDirFile()
Gets the default temp directory for the system as a File. |
static long |
getTotalSpace(java.io.File pPath)
|
static long |
getUsableSpace(java.io.File pPath)
|
static boolean |
isEmpty(java.io.File pFile)
Tests if a file or directory has no content. |
static java.io.File[] |
list(java.lang.String pFolder)
Lists all files (and directories) in a specific folder. |
static java.io.File[] |
list(java.lang.String pFolder,
java.lang.String pFilenameMask)
Lists all files (and directories) in a specific folder which are embraced by the wildcard filename mask provided. |
static void |
main(java.lang.String[] pArgs)
|
static byte[] |
read(java.io.File pFile)
Gets the contents of the given file, as a byte array. |
static byte[] |
read(java.io.InputStream pInput)
Reads all data from the input stream to a byte array. |
static byte[] |
read(java.lang.String pFilename)
Gets the contents of the given file, as a byte array. |
static boolean |
rename(java.io.File pFrom,
java.io.File pTo)
Renames the specified file, if the destination does not exist. |
static boolean |
rename(java.io.File pFrom,
java.io.File pTo,
boolean pOverWrite)
Renames the specified file. |
static boolean |
rename(java.io.File pFrom,
java.lang.String pTo)
Renames the specified file, if the destination does not exist. |
static boolean |
rename(java.io.File pFrom,
java.lang.String pTo,
boolean pOverWrite)
Renames the specified file. |
static boolean |
rename(java.lang.String pFrom,
java.lang.String pTo)
Renames the specified file, if the destination does not exist. |
static boolean |
rename(java.lang.String pFrom,
java.lang.String pTo,
boolean pOverWrite)
Renames the specified file. |
static java.io.File |
resolve(java.io.File pPath)
|
static java.io.File[] |
resolve(java.io.File[] pPaths)
|
static java.io.File |
resolve(java.io.File pParent,
java.lang.String pChild)
|
static java.io.File |
resolve(java.lang.String pPath)
|
static java.io.File |
toFile(java.net.URL pURL)
Creates a File based on the path part of the URL, for
file-protocol (file:) based URLs. |
static java.lang.String |
toHumanReadableSize(long pSizeInBytes)
Formats the given number to a human readable format. |
static void |
visitFiles(java.io.File pDirectory,
java.io.FileFilter pFilter,
Visitor<java.io.File> pVisitor)
Visits all files in pDirectory. |
static boolean |
write(java.io.File pFile,
byte[] pData)
Writes the contents from a byte array to a file. |
static boolean |
write(java.io.OutputStream pOutput,
byte[] pData)
Writes the contents from a byte array to an output stream. |
static boolean |
write(java.lang.String pFilename,
byte[] pData)
Writes the contents from a byte array to a file. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static final int BUF_SIZE
| Method Detail |
|---|
public static void main(java.lang.String[] pArgs)
throws java.io.IOException
java.io.IOException
public static boolean copy(java.lang.String pFromFileName,
java.lang.String pToFileName)
throws java.io.IOException
pFromFileName - The name of the file to copy frompToFileName - The name of the file to copy to
java.io.IOException - if an i/o error occurs during copy
public static boolean copy(java.lang.String pFromFileName,
java.lang.String pToFileName,
boolean pOverWrite)
throws java.io.IOException
pFromFileName - The name of the file to copy frompToFileName - The name of the file to copy topOverWrite - Specifies if the toFile should be overwritten, if it
exists.
java.io.IOException - if an i/o error occurs during copy
public static boolean copy(java.io.File pFromFile,
java.io.File pToFile)
throws java.io.IOException
pFromFile - The file to copy frompToFile - The file to copy to
java.io.IOException - if an i/o error occurs during copy
public static boolean copy(java.io.File pFromFile,
java.io.File pToFile,
boolean pOverWrite)
throws java.io.IOException
pFromFile - The file to copy frompToFile - The file to copy topOverWrite - Specifies if the toFile should be overwritten, if it
exists.
true if the file was copied successfully,
false if the output file exists, and the
pOwerWrite parameter is
false. In all other cases, an
IOException is thrown, and the method does not return.
java.io.IOException - if an i/o error occurs during copypublic static void close(java.io.InputStream pInput)
pInput - the stream to closepublic static void close(java.io.OutputStream pOutput)
pOutput - the stream to close
public static boolean copy(java.io.InputStream pFrom,
java.io.OutputStream pTo)
throws java.io.IOException
pFrom - The input srteam to copy frompTo - The output stream to copy to
java.io.IOException - if an i/o error occurs during copy
java.lang.IllegalArgumentException - if either pFrom or pTo is
nullpublic static java.lang.String getExtension(java.lang.String pFileName)
'.'.
If the filename contains no period, null is returned.
pFileName - the full filename with extension
nullpublic static java.lang.String getExtension(java.io.File pFile)
'.'.
If the filename contains no period, null is returned.
pFile - the file
nullpublic static java.lang.String getBasename(java.lang.String pFileName)
'.'.
If the filename contains no period, the complete file name is returned
(same as pFileName, if the string contains no path elements).
pFileName - the full filename with extension
public static java.lang.String getBasename(java.io.File pFile)
'.'.
If the filename contains no period, pFile.getName() is returned.
pFile - the file
public static java.lang.String getBasename0(java.lang.String pFileName)
public static java.lang.String getDirectoryname(java.lang.String pPath)
pPath - The full filename path.
File.getParent(),
getFilename(java.lang.String)
public static java.lang.String getDirectoryname(java.lang.String pPath,
char pSeparator)
pPath - The full filename path.pSeparator - the separator char used in pPath
File.getParent(),
getFilename(java.lang.String)public static java.lang.String getFilename(java.lang.String pPath)
pPath - The full filename path.
File.getName(),
getDirectoryname(java.lang.String)
public static java.lang.String getFilename(java.lang.String pPath,
char pSeparator)
pPath - The full filename path.pSeparator - The file separator.
File.getName(),
getDirectoryname(java.lang.String)public static boolean isEmpty(java.io.File pFile)
pFile - The file to test
true if the file is empty, otherwise
false.public static java.io.File getTempDirFile()
File, representing the default temp directory.File.createTempFile(java.lang.String, java.lang.String, java.io.File)public static java.lang.String getTempDir()
String, representing the path to the default temp
directory.File.createTempFile(java.lang.String, java.lang.String, java.io.File)
public static byte[] read(java.lang.String pFilename)
throws java.io.IOException
pFilename - the name of the file to get content from
java.io.IOException - if the read operation fails
public static byte[] read(java.io.File pFile)
throws java.io.IOException
pFile - the file to get content from
java.io.IOException - if the read operation fails
public static byte[] read(java.io.InputStream pInput)
throws java.io.IOException
pInput - The input stream to read from
java.io.IOException - if an i/o error occurs during read.
public static boolean write(java.io.OutputStream pOutput,
byte[] pData)
throws java.io.IOException
pOutput - The output stream to write topData - The byte array to write
true, otherwise an IOException is thrown.
java.io.IOException - if an i/o error occurs during write.
public static boolean write(java.io.File pFile,
byte[] pData)
throws java.io.IOException
pFile - The file to write topData - The byte array to write
true, otherwise an IOException is thrown.
java.io.IOException - if an i/o error occurs during write.
public static boolean write(java.lang.String pFilename,
byte[] pData)
throws java.io.IOException
pFilename - The name of the file to write topData - The byte array to write
true, otherwise an IOException is thrown.
java.io.IOException - if an i/o error occurs during write.
public static boolean delete(java.io.File pFile,
boolean pForce)
throws java.io.IOException
pFile - The file to deletepForce - Forces delete, even if the parameter is a directory, and
is not empty. Be careful!
true, if the file existed and was deleted.
java.io.IOException - if an i/o error occurs during delete.
public static boolean delete(java.lang.String pFilename,
boolean pForce)
throws java.io.IOException
pFilename - The name of file to deletepForce - Forces delete, even if the parameter is a directory, and
is not empty. Careful!
true, if the file existed and was deleted.
java.io.IOException - if deletion fails
public static boolean delete(java.io.File pFile)
throws java.io.IOException
pFile - The file to delete
true, if the file existed and was deleted.
java.io.IOException - if deletion fails
public static boolean delete(java.lang.String pFilename)
throws java.io.IOException
pFilename - The name of file to delete
true, if the file existed and was deleted.
java.io.IOException - if deletion fails
public static boolean rename(java.io.File pFrom,
java.io.File pTo,
boolean pOverWrite)
throws java.io.IOException
pFrom - The file to renamepTo - The new filepOverWrite - Specifies if the tofile should be overwritten, if it
exists
true, if the file was renamed.
java.io.FileNotFoundException - if pFrom does not exist.
java.io.IOException
public static boolean rename(java.io.File pFrom,
java.io.File pTo)
throws java.io.IOException
pFrom - The file to renamepTo - The new file
true, if the file was renamed.
java.io.IOException - if rename fails
public static boolean rename(java.io.File pFrom,
java.lang.String pTo,
boolean pOverWrite)
throws java.io.IOException
pFrom - The file to renamepTo - The new name of the filepOverWrite - Specifies if the tofile should be overwritten, if it
exists
true, if the file was renamed.
java.io.IOException - if rename fails
public static boolean rename(java.io.File pFrom,
java.lang.String pTo)
throws java.io.IOException
pFrom - The file to renamepTo - The new name of the file
true, if the file was renamed.
java.io.IOException - if rename fails
public static boolean rename(java.lang.String pFrom,
java.lang.String pTo,
boolean pOverWrite)
throws java.io.IOException
pFrom - The name of the file to renamepTo - The new name of the filepOverWrite - Specifies if the tofile should be overwritten, if it
exists
true, if the file was renamed.
java.io.IOException - if rename fails
public static boolean rename(java.lang.String pFrom,
java.lang.String pTo)
throws java.io.IOException
pFrom - The name of the file to renamepTo - The new name of the file
true, if the file was renamed.
java.io.IOException - if rename fails
public static java.io.File[] list(java.lang.String pFolder)
throws java.io.FileNotFoundException
pFolder - The folder to list
java.io.File objects.
java.io.FileNotFoundException - if pFolder is not a readable file
public static java.io.File[] list(java.lang.String pFolder,
java.lang.String pFilenameMask)
throws java.io.FileNotFoundException
pFolder - The folder to listpFilenameMask - The wildcard filename mask
java.io.File objects.
java.io.FileNotFoundException - if pFolder is not a readable fileFile.listFiles(FilenameFilter)public static java.io.File toFile(java.net.URL pURL)
File based on the path part of the URL, for
file-protocol (file:) based URLs.
pURL - the file: URL
File object representing the URL
java.lang.NullPointerException - if pURL is null
java.lang.IllegalArgumentException - if pURL is
not a file-protocol URL.File.toURI(),
File.File(java.net.URI)public static java.io.File resolve(java.lang.String pPath)
public static java.io.File resolve(java.io.File pPath)
public static java.io.File resolve(java.io.File pParent,
java.lang.String pChild)
public static java.io.File[] resolve(java.io.File[] pPaths)
public static long getFreeSpace(java.io.File pPath)
public static long getUsableSpace(java.io.File pPath)
public static long getTotalSpace(java.io.File pPath)
public static java.lang.String toHumanReadableSize(long pSizeInBytes)
df -h.
pSizeInBytes - the size in byte
public static void visitFiles(java.io.File pDirectory,
java.io.FileFilter pFilter,
Visitor<java.io.File> pVisitor)
pDirectory. Optionally filtered through a FileFilter.
pDirectory - the directory to visit files inpFilter - the filter, may be null, meaning all files will be visitedpVisitor - the visitor
java.lang.IllegalArgumentException - if either pDirectory or pVisitor are nullVisitor
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||