Class FileUtil
java.lang.Object
com.twelvemonkeys.io.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.).
- Version:
- $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/main/java/com/twelvemonkeys/io/FileUtil.java#3 $
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe size of the buffer used for copyingprivate static final FileSystemprivate static ThreadLocal<NumberFormat> private static String -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidclose(InputStream pInput) Tries to close the given stream.static voidclose(OutputStream pOutput) Tries to close the given stream.(package private) static void(package private) static voidstatic booleanCopies the fromFile to the toFile location.static booleanCopies the fromFile to the toFile location.static booleancopy(InputStream pFrom, OutputStream pTo) Copies all data from one stream to another.static booleanCopies the fromFile to the toFile location.static booleanCopies the fromFile to the toFile location.private static booleanCopies a directory recursively.static booleanDeletes the specified file.static booleanDeletes the specified file.static booleanDeletes the specified file.static booleanDeletes the specified file.private static booleanDeletes a directory recursively.static StringgetBasename(File pFile) Gets the file name of the given file, without the extension (type).static StringgetBasename(String pFileName) Gets the file name of the given file, without the extension (type).static StringgetBasename0(String pFileName) static StringgetDirectoryname(String pPath) Extracts the directory path without the filename, from a complete filename path.static StringgetDirectoryname(String pPath, char pSeparator) Extracts the directory path without the filename, from a complete filename path.static StringgetExtension(File pFile) Gets the file (type) extension of the given file.static StringgetExtension(String pFileName) Gets the file (type) extension of the given file.private static StringgetExtension0(String pFileName) static StringgetFilename(String pPath) Extracts the filename of a complete filename path.static StringgetFilename(String pPath, char pSeparator) Extracts the filename of a complete filename path.static longgetFreeSpace(File pPath) private static NumberFormatprivate static LonggetSpace16(String pMethodName, File pPath) static StringGets the default temp directory for the system.static FileGets the default temp directory for the system as a File.static longgetTotalSpace(File pPath) static longgetUsableSpace(File pPath) static booleanTests if a file or directory has no content.static File[]Lists all files (and directories) in a specific folder.static File[]Lists all files (and directories) in a specific folder which are embraced by the wildcard filename mask provided.static voidstatic byte[]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[]Gets the contents of the given file, as a byte array.static booleanRenames the specified file, if the destination does not exist.static booleanRenames the specified file.static booleanRenames the specified file, if the destination does not exist.static booleanRenames the specified file.static booleanRenames the specified file, if the destination does not exist.static booleanRenames the specified file.static Filestatic File[]static Filestatic Filestatic FileCreates aFilebased on the path part of the URL, for file-protocol (file:) based URLs.static StringtoHumanReadableSize(long pSizeInBytes) Formats the given number to a human readable format.static voidvisitFiles(File pDirectory, FileFilter pFilter, Visitor<File> pVisitor) Visits all files inpDirectory.static booleanWrites the contents from a byte array to a file.static booleanwrite(OutputStream pOutput, byte[] pData) Writes the contents from a byte array to an output stream.static booleanWrites the contents from a byte array to a file.
-
Field Details
-
BUF_SIZE
public static final int BUF_SIZEThe size of the buffer used for copying- See Also:
-
TEMP_DIR
-
FS
-
sNumberFormat
-
-
Constructor Details
-
FileUtil
private FileUtil()
-
-
Method Details
-
main
- Throws:
IOException
-
copy
Copies the fromFile to the toFile location. If toFile is a directory, a new file is created in that directory, with the name of the fromFile. If the toFile exists, the file will not be copied, unless owerWrite is true.- Parameters:
pFromFileName- The name of the file to copy frompToFileName- The name of the file to copy to- Returns:
- true if the file was copied successfully, false if the output file exists. In all other cases, an IOException is thrown, and the method does not return a value.
- Throws:
IOException- if an i/o error occurs during copy
-
copy
public static boolean copy(String pFromFileName, String pToFileName, boolean pOverWrite) throws IOException Copies the fromFile to the toFile location. If toFile is a directory, a new file is created in that directory, with the name of the fromFile. If the toFile exists, the file will not be copied, unless owerWrite is true.- Parameters:
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.- Returns:
- true if the file was copied successfully, false if the output file exists, and the owerWrite parameter is false. In all other cases, an IOException is thrown, and the method does not return a value.
- Throws:
IOException- if an i/o error occurs during copy
-
copy
Copies the fromFile to the toFile location. If toFile is a directory, a new file is created in that directory, with the name of the fromFile. If the toFile exists, the file will not be copied, unless owerWrite is true.- Parameters:
pFromFile- The file to copy frompToFile- The file to copy to- Returns:
- true if the file was copied successfully, false if the output file exists. In all other cases, an IOException is thrown, and the method does not return a value.
- Throws:
IOException- if an i/o error occurs during copy
-
copy
Copies the fromFile to the toFile location. If toFile is a directory, a new file is created in that directory, with the name of the fromFile. If the toFile exists, the file will not be copied, unless owerWrite is true.- Parameters:
pFromFile- The file to copy frompToFile- The file to copy topOverWrite- Specifies if the toFile should be overwritten, if it exists.- Returns:
trueif the file was copied successfully,falseif the output file exists, and thepOwerWriteparameter isfalse. In all other cases, anIOException is thrown, and the method does not return.- Throws:
IOException- if an i/o error occurs during copy
-
close
Tries to close the given stream. NOTE: If the stream cannot be closed, the IOException thrown is silently ignored.- Parameters:
pInput- the stream to close
-
close
Tries to close the given stream. NOTE: If the stream cannot be closed, the IOException thrown is silently ignored.- Parameters:
pOutput- the stream to close
-
close
-
close
-
copyDir
Copies a directory recursively. If the destination folder does not exist, it is created- Parameters:
pFrom- the source directorypTo- the destination directorypOverWrite-trueif we should allow overwrting existing files- Returns:
trueif all files were copied sucessfully- Throws:
IOException- ifpToexists, and it not a directory, or if copying of any of the files in the folder fails
-
copy
Copies all data from one stream to another. The data is copied from the fromStream to the toStream using buffered streams for efficiency.- Parameters:
pFrom- The input srteam to copy frompTo- The output stream to copy to- Returns:
- true. Otherwise, an IOException is thrown, and the method does not return a value.
- Throws:
IOException- if an i/o error occurs during copyIllegalArgumentException- if eitherpFromorpToisnull
-
getExtension
Gets the file (type) extension of the given file. A file extension is the part of the filename, after the last occurence of a period'.'. If the filename contains no period,nullis returned.- Parameters:
pFileName- the full filename with extension- Returns:
- the extension (type) of the file, or
null
-
getExtension
Gets the file (type) extension of the given file. A file extension is the part of the filename, after the last occurrence of a period'.'. If the filename contains no period,nullis returned.- Parameters:
pFile- the file- Returns:
- the extension (type) of the file, or
null
-
getExtension0
-
getBasename
Gets the file name of the given file, without the extension (type). A file extension is the part of the filename, after the last occurence of a period'.'. If the filename contains no period, the complete file name is returned (same aspFileName, if the string contains no path elements).- Parameters:
pFileName- the full filename with extension- Returns:
- the base name of the file
-
getBasename
Gets the file name of the given file, without the extension (type). A file extension is the part of the filename, after the last occurence of a period'.'. If the filename contains no period,pFile.getName()is returned.- Parameters:
pFile- the file- Returns:
- the base name of the file
-
getBasename0
-
getDirectoryname
-
getDirectoryname
-
getFilename
-
getFilename
-
isEmpty
Tests if a file or directory has no content. A file is empty if it has a length of 0L. A non-existing file is also considered empty. A directory is considered empty if it contains no files.- Parameters:
pFile- The file to test- Returns:
trueif the file is empty, otherwisefalse.
-
getTempDirFile
Gets the default temp directory for the system as a File.- Returns:
- a
File, representing the default temp directory. - See Also:
-
getTempDir
Gets the default temp directory for the system.- Returns:
- a
String, representing the path to the default temp directory. - See Also:
-
read
Gets the contents of the given file, as a byte array.- Parameters:
pFilename- the name of the file to get content from- Returns:
- the content of the file as a byte array.
- Throws:
IOException- if the read operation fails
-
read
Gets the contents of the given file, as a byte array.- Parameters:
pFile- the file to get content from- Returns:
- the content of the file as a byte array.
- Throws:
IOException- if the read operation fails
-
read
Reads all data from the input stream to a byte array.- Parameters:
pInput- The input stream to read from- Returns:
- The content of the stream as a byte array.
- Throws:
IOException- if an i/o error occurs during read.
-
write
Writes the contents from a byte array to an output stream.- Parameters:
pOutput- The output stream to write topData- The byte array to write- Returns:
true, otherwise an IOException is thrown.- Throws:
IOException- if an i/o error occurs during write.
-
write
Writes the contents from a byte array to a file.- Parameters:
pFile- The file to write topData- The byte array to write- Returns:
true, otherwise an IOException is thrown.- Throws:
IOException- if an i/o error occurs during write.
-
write
Writes the contents from a byte array to a file.- Parameters:
pFilename- The name of the file to write topData- The byte array to write- Returns:
true, otherwise an IOException is thrown.- Throws:
IOException- if an i/o error occurs during write.
-
delete
Deletes the specified file.- Parameters:
pFile- The file to deletepForce- Forces delete, even if the parameter is a directory, and is not empty. Be careful!- Returns:
true, if the file existed and was deleted.- Throws:
IOException- if an i/o error occurs during delete.
-
deleteDir
Deletes a directory recursively.- Parameters:
pFile- the file to delete- Returns:
trueif the file was deleted sucessfully- Throws:
IOException- if an i/o error occurs during delete.
-
delete
Deletes the specified file.- Parameters:
pFilename- The name of file to deletepForce- Forces delete, even if the parameter is a directory, and is not empty. Careful!- Returns:
true, if the file existed and was deleted.- Throws:
IOException- if deletion fails
-
delete
Deletes the specified file.- Parameters:
pFile- The file to delete- Returns:
true, if the file existed and was deleted.- Throws:
IOException- if deletion fails
-
delete
Deletes the specified file.- Parameters:
pFilename- The name of file to delete- Returns:
true, if the file existed and was deleted.- Throws:
IOException- if deletion fails
-
rename
Renames the specified file. If the destination is a directory (and the source is not), the source file is simply moved to the destination directory.- Parameters:
pFrom- The file to renamepTo- The new filepOverWrite- Specifies if the tofile should be overwritten, if it exists- Returns:
true, if the file was renamed.- Throws:
FileNotFoundException- ifpFromdoes not exist.IOException
-
rename
Renames the specified file, if the destination does not exist. If the destination is a directory (and the source is not), the source file is simply moved to the destination directory.- Parameters:
pFrom- The file to renamepTo- The new file- Returns:
true, if the file was renamed.- Throws:
IOException- if rename fails
-
rename
Renames the specified file. If the destination is a directory (and the source is not), the source file is simply moved to the destination directory.- Parameters:
pFrom- The file to renamepTo- The new name of the filepOverWrite- Specifies if the tofile should be overwritten, if it exists- Returns:
true, if the file was renamed.- Throws:
IOException- if rename fails
-
rename
Renames the specified file, if the destination does not exist. If the destination is a directory (and the source is not), the source file is simply moved to the destination directory.- Parameters:
pFrom- The file to renamepTo- The new name of the file- Returns:
true, if the file was renamed.- Throws:
IOException- if rename fails
-
rename
Renames the specified file. If the destination is a directory (and the source is not), the source file is simply moved to the destination directory.- Parameters:
pFrom- The name of the file to renamepTo- The new name of the filepOverWrite- Specifies if the tofile should be overwritten, if it exists- Returns:
true, if the file was renamed.- Throws:
IOException- if rename fails
-
rename
Renames the specified file, if the destination does not exist. If the destination is a directory (and the source is not), the source file is simply moved to the destination directory.- Parameters:
pFrom- The name of the file to renamepTo- The new name of the file- Returns:
true, if the file was renamed.- Throws:
IOException- if rename fails
-
list
Lists all files (and directories) in a specific folder.- Parameters:
pFolder- The folder to list- Returns:
- a list of
java.io.Fileobjects. - Throws:
FileNotFoundException- ifpFolderis not a readable file
-
list
Lists all files (and directories) in a specific folder which are embraced by the wildcard filename mask provided.- Parameters:
pFolder- The folder to listpFilenameMask- The wildcard filename mask- Returns:
- a list of
java.io.Fileobjects. - Throws:
FileNotFoundException- ifpFolderis not a readable file- See Also:
-
toFile
Creates aFilebased on the path part of the URL, for file-protocol (file:) based URLs.- Parameters:
pURL- thefile:URL- Returns:
- a new
Fileobject representing the URL - Throws:
NullPointerException- ifpURLisnullIllegalArgumentException- ifpURLis not a file-protocol URL.- See Also:
-
resolve
-
resolve
-
resolve
-
resolve
-
getFreeSpace
-
getUsableSpace
-
getTotalSpace
-
getSpace16
-
toHumanReadableSize
Formats the given number to a human readable format. Kind of likedf -h.- Parameters:
pSizeInBytes- the size in byte- Returns:
- a human readable string representation
-
getSizeFormat
-
visitFiles
Visits all files inpDirectory. Optionally filtered through aFileFilter.- Parameters:
pDirectory- the directory to visit files inpFilter- the filter, may benull, meaning all files will be visitedpVisitor- the visitor- Throws:
IllegalArgumentException- if eitherpDirectoryorpVisitorarenull- See Also:
-