|
||||||||||
| 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(InputStream pInput)
Tries to close the given stream. |
static void |
close(OutputStream pOutput)
Tries to close the given stream. |
static boolean |
copy(File pFromFile,
File pToFile)
Copies the fromFile to the toFile location. |
static boolean |
copy(File pFromFile,
File pToFile,
boolean pOverWrite)
Copies the fromFile to the toFile location. |
static boolean |
copy(InputStream pFrom,
OutputStream pTo)
Copies all data from one stream to another. |
static boolean |
copy(String pFromFileName,
String pToFileName)
Copies the fromFile to the toFile location. |
static boolean |
copy(String pFromFileName,
String pToFileName,
boolean pOverWrite)
Copies the fromFile to the toFile location. |
static boolean |
delete(File pFile)
Deletes the specified file. |
static boolean |
delete(File pFile,
boolean pForce)
Deletes the specified file. |
static boolean |
delete(String pFilename)
Deletes the specified file. |
static boolean |
delete(String pFilename,
boolean pForce)
Deletes the specified file. |
static String |
getBasename(File pFile)
Gets the file name of the given file, without the extension (type). |
static String |
getBasename(String pFileName)
Gets the file name of the given file, without the extension (type). |
static String |
getBasename0(String pFileName)
|
static String |
getDirectoryname(String pPath)
Extracts the directory path without the filename, from a complete filename path. |
static String |
getDirectoryname(String pPath,
char pSeparator)
Extracts the directory path without the filename, from a complete filename path. |
static String |
getExtension(File pFile)
Gets the file (type) extension of the given file. |
static String |
getExtension(String pFileName)
Gets the file (type) extension of the given file. |
static String |
getFilename(String pPath)
Extracts the filename of a complete filename path. |
static String |
getFilename(String pPath,
char pSeparator)
Extracts the filename of a complete filename path. |
static long |
getFreeSpace(File pPath)
|
static String |
getTempDir()
Gets the default temp directory for the system. |
static File |
getTempDirFile()
Gets the default temp directory for the system as a File. |
static long |
getTotalSpace(File pPath)
|
static long |
getUsableSpace(File pPath)
|
static boolean |
isEmpty(File pFile)
Tests if a file or directory has no content. |
static File[] |
list(String pFolder)
Lists all files (and directories) in a specific folder. |
static File[] |
list(String pFolder,
String pFilenameMask)
Lists all files (and directories) in a specific folder which are embraced by the wildcard filename mask provided. |
static void |
main(String[] pArgs)
|
static byte[] |
read(File pFile)
Gets the contents of the given file, as a byte array. |
static byte[] |
read(InputStream pInput)
Reads all data from the input stream to a byte array. |
static byte[] |
read(String pFilename)
Gets the contents of the given file, as a byte array. |
static boolean |
rename(File pFrom,
File pTo)
Renames the specified file, if the destination does not exist. |
static boolean |
rename(File pFrom,
File pTo,
boolean pOverWrite)
Renames the specified file. |
static boolean |
rename(File pFrom,
String pTo)
Renames the specified file, if the destination does not exist. |
static boolean |
rename(File pFrom,
String pTo,
boolean pOverWrite)
Renames the specified file. |
static boolean |
rename(String pFrom,
String pTo)
Renames the specified file, if the destination does not exist. |
static boolean |
rename(String pFrom,
String pTo,
boolean pOverWrite)
Renames the specified file. |
static File |
resolve(File pPath)
|
static File[] |
resolve(File[] pPaths)
|
static File |
resolve(File pParent,
String pChild)
|
static File |
resolve(String pPath)
|
static File |
toFile(URL pURL)
Creates a File based on the path part of the URL, for
file-protocol (file:) based URLs. |
static String |
toHumanReadableSize(long pSizeInBytes)
Formats the given number to a human readable format. |
static void |
visitFiles(File pDirectory,
FileFilter pFilter,
Visitor<File> pVisitor)
Visits all files in pDirectory. |
static boolean |
write(File pFile,
byte[] pData)
Writes the contents from a byte array to a file. |
static boolean |
write(OutputStream pOutput,
byte[] pData)
Writes the contents from a byte array to an output stream. |
static boolean |
write(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(String[] pArgs)
throws IOException
IOException
public static boolean copy(String pFromFileName,
String pToFileName)
throws IOException
pFromFileName - The name of the file to copy frompToFileName - The name of the file to copy to
IOException - if an i/o error occurs during copy
public static boolean copy(String pFromFileName,
String pToFileName,
boolean pOverWrite)
throws 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.
IOException - if an i/o error occurs during copy
public static boolean copy(File pFromFile,
File pToFile)
throws IOException
pFromFile - The file to copy frompToFile - The file to copy to
IOException - if an i/o error occurs during copy
public static boolean copy(File pFromFile,
File pToFile,
boolean pOverWrite)
throws 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.
IOException - if an i/o error occurs during copypublic static void close(InputStream pInput)
pInput - the stream to closepublic static void close(OutputStream pOutput)
pOutput - the stream to close
public static boolean copy(InputStream pFrom,
OutputStream pTo)
throws IOException
pFrom - The input srteam to copy frompTo - The output stream to copy to
IOException - if an i/o error occurs during copy
IllegalArgumentException - if either pFrom or pTo is
nullpublic static String getExtension(String pFileName)
'.'.
If the filename contains no period, null is returned.
pFileName - the full filename with extension
nullpublic static String getExtension(File pFile)
'.'.
If the filename contains no period, null is returned.
pFile - the file
nullpublic static String getBasename(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 String getBasename(File pFile)
'.'.
If the filename contains no period, pFile.getName() is returned.
pFile - the file
public static String getBasename0(String pFileName)
public static String getDirectoryname(String pPath)
pPath - The full filename path.
File.getParent(),
getFilename(java.lang.String)
public static String getDirectoryname(String pPath,
char pSeparator)
pPath - The full filename path.pSeparator - the separator char used in pPath
File.getParent(),
getFilename(java.lang.String)public static String getFilename(String pPath)
pPath - The full filename path.
File.getName(),
getDirectoryname(java.lang.String)
public static String getFilename(String pPath,
char pSeparator)
pPath - The full filename path.pSeparator - The file separator.
File.getName(),
getDirectoryname(java.lang.String)public static boolean isEmpty(File pFile)
pFile - The file to test
true if the file is empty, otherwise
false.public static File getTempDirFile()
File, representing the default temp directory.File.createTempFile(java.lang.String, java.lang.String, java.io.File)public static 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(String pFilename)
throws IOException
pFilename - the name of the file to get content from
IOException - if the read operation fails
public static byte[] read(File pFile)
throws IOException
pFile - the file to get content from
IOException - if the read operation fails
public static byte[] read(InputStream pInput)
throws IOException
pInput - The input stream to read from
IOException - if an i/o error occurs during read.
public static boolean write(OutputStream pOutput,
byte[] pData)
throws IOException
pOutput - The output stream to write topData - The byte array to write
true, otherwise an IOException is thrown.
IOException - if an i/o error occurs during write.
public static boolean write(File pFile,
byte[] pData)
throws IOException
pFile - The file to write topData - The byte array to write
true, otherwise an IOException is thrown.
IOException - if an i/o error occurs during write.
public static boolean write(String pFilename,
byte[] pData)
throws IOException
pFilename - The name of the file to write topData - The byte array to write
true, otherwise an IOException is thrown.
IOException - if an i/o error occurs during write.
public static boolean delete(File pFile,
boolean pForce)
throws 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.
IOException - if an i/o error occurs during delete.
public static boolean delete(String pFilename,
boolean pForce)
throws 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.
IOException - if deletion fails
public static boolean delete(File pFile)
throws IOException
pFile - The file to delete
true, if the file existed and was deleted.
IOException - if deletion fails
public static boolean delete(String pFilename)
throws IOException
pFilename - The name of file to delete
true, if the file existed and was deleted.
IOException - if deletion fails
public static boolean rename(File pFrom,
File pTo,
boolean pOverWrite)
throws 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.
FileNotFoundException - if pFrom does not exist.
IOException
public static boolean rename(File pFrom,
File pTo)
throws IOException
pFrom - The file to renamepTo - The new file
true, if the file was renamed.
IOException - if rename fails
public static boolean rename(File pFrom,
String pTo,
boolean pOverWrite)
throws 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.
IOException - if rename fails
public static boolean rename(File pFrom,
String pTo)
throws IOException
pFrom - The file to renamepTo - The new name of the file
true, if the file was renamed.
IOException - if rename fails
public static boolean rename(String pFrom,
String pTo,
boolean pOverWrite)
throws 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.
IOException - if rename fails
public static boolean rename(String pFrom,
String pTo)
throws IOException
pFrom - The name of the file to renamepTo - The new name of the file
true, if the file was renamed.
IOException - if rename fails
public static File[] list(String pFolder)
throws FileNotFoundException
pFolder - The folder to list
java.io.File objects.
FileNotFoundException - if pFolder is not a readable file
public static File[] list(String pFolder,
String pFilenameMask)
throws FileNotFoundException
pFolder - The folder to listpFilenameMask - The wildcard filename mask
java.io.File objects.
FileNotFoundException - if pFolder is not a readable fileFile.listFiles(FilenameFilter)public static File toFile(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
NullPointerException - if pURL is null
IllegalArgumentException - if pURL is
not a file-protocol URL.File.toURI(),
File.File(java.net.URI)public static File resolve(String pPath)
public static File resolve(File pPath)
public static File resolve(File pParent,
String pChild)
public static File[] resolve(File[] pPaths)
public static long getFreeSpace(File pPath)
public static long getUsableSpace(File pPath)
public static long getTotalSpace(File pPath)
public static String toHumanReadableSize(long pSizeInBytes)
df -h.
pSizeInBytes - the size in byte
public static void visitFiles(File pDirectory,
FileFilter pFilter,
Visitor<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
IllegalArgumentException - if either pDirectory or pVisitor are nullVisitor
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||