Package com.twelvemonkeys.io
Class FileUtil
- java.lang.Object
-
- com.twelvemonkeys.io.FileUtil
-
public final class FileUtil extends java.lang.Object
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
Fields Modifier and Type Field Description static int
BUF_SIZE
The size of the buffer used for copyingprivate static FileSystem
FS
private static java.lang.ThreadLocal<java.text.NumberFormat>
sNumberFormat
private static java.lang.String
TEMP_DIR
-
Constructor Summary
Constructors Modifier Constructor Description private
FileUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description 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.(package private) static void
close(java.io.Reader pReader)
(package private) static void
close(java.io.Writer pWriter)
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.private static boolean
copyDir(java.io.File pFrom, java.io.File pTo, boolean pOverWrite)
Copies a directory recursively.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.private static boolean
deleteDir(java.io.File pFile)
Deletes a directory recursively.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.private static java.lang.String
getExtension0(java.lang.String pFileName)
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)
private static java.text.NumberFormat
getSizeFormat()
private static java.lang.Long
getSpace16(java.lang.String pMethodName, 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 aFile
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 inpDirectory
.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.
-
-
-
Field Detail
-
BUF_SIZE
public static final int BUF_SIZE
The size of the buffer used for copying- See Also:
- Constant Field Values
-
TEMP_DIR
private static java.lang.String TEMP_DIR
-
FS
private static final FileSystem FS
-
sNumberFormat
private static java.lang.ThreadLocal<java.text.NumberFormat> sNumberFormat
-
-
Method Detail
-
main
public static void main(java.lang.String[] pArgs) throws java.io.IOException
- Throws:
java.io.IOException
-
copy
public static boolean copy(java.lang.String pFromFileName, java.lang.String pToFileName) throws java.io.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 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:
java.io.IOException
- if an i/o error occurs during copy
-
copy
public static boolean copy(java.lang.String pFromFileName, java.lang.String pToFileName, boolean pOverWrite) throws java.io.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:
java.io.IOException
- if an i/o error occurs during copy
-
copy
public static boolean copy(java.io.File pFromFile, java.io.File pToFile) throws java.io.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:
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:
java.io.IOException
- if an i/o error occurs during copy
-
copy
public static boolean copy(java.io.File pFromFile, java.io.File pToFile, boolean pOverWrite) throws java.io.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:
pFromFile
- The file to copy frompToFile
- 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 thepOwerWrite
parameter isfalse
. In all other cases, anIOExceptio
n is thrown, and the method does not return.- Throws:
java.io.IOException
- if an i/o error occurs during copy
-
close
public static void close(java.io.InputStream pInput)
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
public static void close(java.io.OutputStream pOutput)
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
static void close(java.io.Reader pReader)
-
close
static void close(java.io.Writer pWriter)
-
copyDir
private static boolean copyDir(java.io.File pFrom, java.io.File pTo, boolean pOverWrite) throws java.io.IOException
Copies a directory recursively. If the destination folder does not exist, it is created- Parameters:
pFrom
- the source directorypTo
- the destination directorypOverWrite
-true
if we should allow overwrting existing files- Returns:
true
if all files were copied sucessfully- Throws:
java.io.IOException
- ifpTo
exists, and it not a directory, or if copying of any of the files in the folder fails
-
copy
public static boolean copy(java.io.InputStream pFrom, java.io.OutputStream pTo) throws java.io.IOException
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:
java.io.IOException
- if an i/o error occurs during copyjava.lang.IllegalArgumentException
- if eitherpFrom
orpTo
isnull
-
getExtension
public static java.lang.String getExtension(java.lang.String pFileName)
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,null
is returned.- Parameters:
pFileName
- the full filename with extension- Returns:
- the extension (type) of the file, or
null
-
getExtension
public static java.lang.String getExtension(java.io.File pFile)
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,null
is returned.- Parameters:
pFile
- the file- Returns:
- the extension (type) of the file, or
null
-
getExtension0
private static java.lang.String getExtension0(java.lang.String pFileName)
-
getBasename
public static java.lang.String getBasename(java.lang.String pFileName)
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
public static java.lang.String getBasename(java.io.File pFile)
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
public static java.lang.String getBasename0(java.lang.String pFileName)
-
getDirectoryname
public static java.lang.String getDirectoryname(java.lang.String pPath)
Extracts the directory path without the filename, from a complete filename path.- Parameters:
pPath
- The full filename path.- Returns:
- the path without the filename.
- See Also:
File.getParent()
,getFilename(java.lang.String)
-
getDirectoryname
public static java.lang.String getDirectoryname(java.lang.String pPath, char pSeparator)
Extracts the directory path without the filename, from a complete filename path.- Parameters:
pPath
- The full filename path.pSeparator
- the separator char used inpPath
- Returns:
- the path without the filename.
- See Also:
File.getParent()
,getFilename(java.lang.String)
-
getFilename
public static java.lang.String getFilename(java.lang.String pPath)
Extracts the filename of a complete filename path.- Parameters:
pPath
- The full filename path.- Returns:
- the extracted filename.
- See Also:
File.getName()
,getDirectoryname(java.lang.String)
-
getFilename
public static java.lang.String getFilename(java.lang.String pPath, char pSeparator)
Extracts the filename of a complete filename path.- Parameters:
pPath
- The full filename path.pSeparator
- The file separator.- Returns:
- the extracted filename.
- See Also:
File.getName()
,getDirectoryname(java.lang.String)
-
isEmpty
public static boolean isEmpty(java.io.File pFile)
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:
true
if the file is empty, otherwisefalse
.
-
getTempDirFile
public static java.io.File getTempDirFile()
Gets the default temp directory for the system as a File.- Returns:
- a
File
, representing the default temp directory. - See Also:
File.createTempFile(java.lang.String, java.lang.String, java.io.File)
-
getTempDir
public static java.lang.String getTempDir()
Gets the default temp directory for the system.- Returns:
- a
String
, representing the path to the default temp directory. - See Also:
File.createTempFile(java.lang.String, java.lang.String, java.io.File)
-
read
public static byte[] read(java.lang.String pFilename) throws java.io.IOException
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:
java.io.IOException
- if the read operation fails
-
read
public static byte[] read(java.io.File pFile) throws java.io.IOException
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:
java.io.IOException
- if the read operation fails
-
read
public static byte[] read(java.io.InputStream pInput) throws java.io.IOException
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:
java.io.IOException
- if an i/o error occurs during read.
-
write
public static boolean write(java.io.OutputStream pOutput, byte[] pData) throws java.io.IOException
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:
java.io.IOException
- if an i/o error occurs during write.
-
write
public static boolean write(java.io.File pFile, byte[] pData) throws java.io.IOException
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:
java.io.IOException
- if an i/o error occurs during write.
-
write
public static boolean write(java.lang.String pFilename, byte[] pData) throws java.io.IOException
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:
java.io.IOException
- if an i/o error occurs during write.
-
delete
public static boolean delete(java.io.File pFile, boolean pForce) throws java.io.IOException
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:
java.io.IOException
- if an i/o error occurs during delete.
-
deleteDir
private static boolean deleteDir(java.io.File pFile) throws java.io.IOException
Deletes a directory recursively.- Parameters:
pFile
- the file to delete- Returns:
true
if the file was deleted sucessfully- Throws:
java.io.IOException
- if an i/o error occurs during delete.
-
delete
public static boolean delete(java.lang.String pFilename, boolean pForce) throws java.io.IOException
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:
java.io.IOException
- if deletion fails
-
delete
public static boolean delete(java.io.File pFile) throws java.io.IOException
Deletes the specified file.- Parameters:
pFile
- The file to delete- Returns:
true
, if the file existed and was deleted.- Throws:
java.io.IOException
- if deletion fails
-
delete
public static boolean delete(java.lang.String pFilename) throws java.io.IOException
Deletes the specified file.- Parameters:
pFilename
- The name of file to delete- Returns:
true
, if the file existed and was deleted.- Throws:
java.io.IOException
- if deletion fails
-
rename
public static boolean rename(java.io.File pFrom, java.io.File pTo, boolean pOverWrite) throws java.io.IOException
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:
java.io.FileNotFoundException
- ifpFrom
does not exist.java.io.IOException
-
rename
public static boolean rename(java.io.File pFrom, java.io.File pTo) throws java.io.IOException
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:
java.io.IOException
- if rename fails
-
rename
public static boolean rename(java.io.File pFrom, java.lang.String pTo, boolean pOverWrite) throws java.io.IOException
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:
java.io.IOException
- if rename fails
-
rename
public static boolean rename(java.io.File pFrom, java.lang.String pTo) throws java.io.IOException
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:
java.io.IOException
- if rename fails
-
rename
public static boolean rename(java.lang.String pFrom, java.lang.String pTo, boolean pOverWrite) throws java.io.IOException
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:
java.io.IOException
- if rename fails
-
rename
public static boolean rename(java.lang.String pFrom, java.lang.String pTo) throws java.io.IOException
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:
java.io.IOException
- if rename fails
-
list
public static java.io.File[] list(java.lang.String pFolder) throws java.io.FileNotFoundException
Lists all files (and directories) in a specific folder.- Parameters:
pFolder
- The folder to list- Returns:
- a list of
java.io.File
objects. - Throws:
java.io.FileNotFoundException
- ifpFolder
is not a readable file
-
list
public static java.io.File[] list(java.lang.String pFolder, java.lang.String pFilenameMask) throws java.io.FileNotFoundException
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.File
objects. - Throws:
java.io.FileNotFoundException
- ifpFolder
is not a readable file- See Also:
File.listFiles(FilenameFilter)
-
toFile
public static java.io.File toFile(java.net.URL pURL)
Creates aFile
based on the path part of the URL, for file-protocol (file:
) based URLs.- Parameters:
pURL
- thefile:
URL- Returns:
- a new
File
object representing the URL - Throws:
java.lang.NullPointerException
- ifpURL
isnull
java.lang.IllegalArgumentException
- ifpURL
is not a file-protocol URL.- See Also:
File.toURI()
,File(java.net.URI)
-
resolve
public static java.io.File resolve(java.lang.String pPath)
-
resolve
public static java.io.File resolve(java.io.File pPath)
-
resolve
public static java.io.File resolve(java.io.File pParent, java.lang.String pChild)
-
resolve
public static java.io.File[] resolve(java.io.File[] pPaths)
-
getFreeSpace
public static long getFreeSpace(java.io.File pPath)
-
getUsableSpace
public static long getUsableSpace(java.io.File pPath)
-
getTotalSpace
public static long getTotalSpace(java.io.File pPath)
-
getSpace16
private static java.lang.Long getSpace16(java.lang.String pMethodName, java.io.File pPath)
-
toHumanReadableSize
public static java.lang.String toHumanReadableSize(long pSizeInBytes)
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
private static java.text.NumberFormat getSizeFormat()
-
visitFiles
public static void visitFiles(java.io.File pDirectory, java.io.FileFilter pFilter, Visitor<java.io.File> pVisitor)
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:
java.lang.IllegalArgumentException
- if eitherpDirectory
orpVisitor
arenull
- See Also:
Visitor
-
-