com.twelvemonkeys.io
Class FileUtil

java.lang.Object
  extended by com.twelvemonkeys.io.FileUtil

public final class FileUtil
extends 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 $
Author:
Harald Kuhr, Eirik Torske, last modified by $Author: haku $

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

BUF_SIZE

public static final int BUF_SIZE
The size of the buffer used for copying

See Also:
Constant Field Values
Method Detail

main

public static void main(String[] pArgs)
                 throws IOException
Throws:
IOException

copy

public static boolean copy(String pFromFileName,
                           String pToFileName)
                    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 from
pToFileName - 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 from
pToFileName - The name of the file to copy to
pOverWrite - 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

public static boolean copy(File pFromFile,
                           File pToFile)
                    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:
pFromFile - The file to copy from
pToFile - 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(File pFromFile,
                           File pToFile,
                           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:
pFromFile - The file to copy from
pToFile - The file to copy to
pOverWrite - 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 pOwerWrite parameter is false. In all other cases, an IOException is thrown, and the method does not return.
Throws:
IOException - if an i/o error occurs during copy

close

public static void close(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(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

copy

public static boolean copy(InputStream pFrom,
                           OutputStream pTo)
                    throws 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 from
pTo - 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 copy
IllegalArgumentException - if either pFrom or pTo is null

getExtension

public static String getExtension(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 String getExtension(File pFile)
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:
pFile - the file
Returns:
the extension (type) of the file, or null

getBasename

public static String getBasename(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 as pFileName, if the string contains no path elements).

Parameters:
pFileName - the full filename with extension
Returns:
the base name of the file

getBasename

public static String getBasename(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 String getBasename0(String pFileName)

getDirectoryname

public static String getDirectoryname(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 String getDirectoryname(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 in pPath
Returns:
the path without the filename.
See Also:
File.getParent(), getFilename(java.lang.String)

getFilename

public static String getFilename(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 String getFilename(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(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, otherwise false.

getTempDirFile

public static 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 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(String pFilename)
                   throws 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:
IOException - if the read operation fails

read

public static byte[] read(File pFile)
                   throws 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:
IOException - if the read operation fails

read

public static byte[] read(InputStream pInput)
                   throws 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:
IOException - if an i/o error occurs during read.

write

public static boolean write(OutputStream pOutput,
                            byte[] pData)
                     throws IOException
Writes the contents from a byte array to an output stream.

Parameters:
pOutput - The output stream to write to
pData - The byte array to write
Returns:
true, otherwise an IOException is thrown.
Throws:
IOException - if an i/o error occurs during write.

write

public static boolean write(File pFile,
                            byte[] pData)
                     throws IOException
Writes the contents from a byte array to a file.

Parameters:
pFile - The file to write to
pData - The byte array to write
Returns:
true, otherwise an IOException is thrown.
Throws:
IOException - if an i/o error occurs during write.

write

public static boolean write(String pFilename,
                            byte[] pData)
                     throws IOException
Writes the contents from a byte array to a file.

Parameters:
pFilename - The name of the file to write to
pData - The byte array to write
Returns:
true, otherwise an IOException is thrown.
Throws:
IOException - if an i/o error occurs during write.

delete

public static boolean delete(File pFile,
                             boolean pForce)
                      throws IOException
Deletes the specified file.

Parameters:
pFile - The file to delete
pForce - 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.

delete

public static boolean delete(String pFilename,
                             boolean pForce)
                      throws IOException
Deletes the specified file.

Parameters:
pFilename - The name of file to delete
pForce - 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

public static boolean delete(File pFile)
                      throws IOException
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

public static boolean delete(String pFilename)
                      throws IOException
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

public static boolean rename(File pFrom,
                             File pTo,
                             boolean pOverWrite)
                      throws 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 rename
pTo - The new file
pOverWrite - Specifies if the tofile should be overwritten, if it exists
Returns:
true, if the file was renamed.
Throws:
FileNotFoundException - if pFrom does not exist.
IOException

rename

public static boolean rename(File pFrom,
                             File pTo)
                      throws 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 rename
pTo - The new file
Returns:
true, if the file was renamed.
Throws:
IOException - if rename fails

rename

public static boolean rename(File pFrom,
                             String pTo,
                             boolean pOverWrite)
                      throws 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 rename
pTo - The new name of the file
pOverWrite - Specifies if the tofile should be overwritten, if it exists
Returns:
true, if the file was renamed.
Throws:
IOException - if rename fails

rename

public static boolean rename(File pFrom,
                             String pTo)
                      throws 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 rename
pTo - The new name of the file
Returns:
true, if the file was renamed.
Throws:
IOException - if rename fails

rename

public static boolean rename(String pFrom,
                             String pTo,
                             boolean pOverWrite)
                      throws 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 rename
pTo - The new name of the file
pOverWrite - Specifies if the tofile should be overwritten, if it exists
Returns:
true, if the file was renamed.
Throws:
IOException - if rename fails

rename

public static boolean rename(String pFrom,
                             String pTo)
                      throws 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 rename
pTo - The new name of the file
Returns:
true, if the file was renamed.
Throws:
IOException - if rename fails

list

public static File[] list(String pFolder)
                   throws 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:
FileNotFoundException - if pFolder is not a readable file

list

public static File[] list(String pFolder,
                          String pFilenameMask)
                   throws FileNotFoundException
Lists all files (and directories) in a specific folder which are embraced by the wildcard filename mask provided.

Parameters:
pFolder - The folder to list
pFilenameMask - The wildcard filename mask
Returns:
a list of java.io.File objects.
Throws:
FileNotFoundException - if pFolder is not a readable file
See Also:
File.listFiles(FilenameFilter)

toFile

public static File toFile(URL pURL)
Creates a File based on the path part of the URL, for file-protocol (file:) based URLs.

Parameters:
pURL - the file: URL
Returns:
a new File object representing the URL
Throws:
NullPointerException - if pURL is null
IllegalArgumentException - if pURL is not a file-protocol URL.
See Also:
File.toURI(), File.File(java.net.URI)

resolve

public static File resolve(String pPath)

resolve

public static File resolve(File pPath)

resolve

public static File resolve(File pParent,
                           String pChild)

resolve

public static File[] resolve(File[] pPaths)

getFreeSpace

public static long getFreeSpace(File pPath)

getUsableSpace

public static long getUsableSpace(File pPath)

getTotalSpace

public static long getTotalSpace(File pPath)

toHumanReadableSize

public static String toHumanReadableSize(long pSizeInBytes)
Formats the given number to a human readable format. Kind of like df -h.

Parameters:
pSizeInBytes - the size in byte
Returns:
a human readable string representation

visitFiles

public static void visitFiles(File pDirectory,
                              FileFilter pFilter,
                              Visitor<File> pVisitor)
Visits all files in pDirectory. Optionally filtered through a FileFilter.

Parameters:
pDirectory - the directory to visit files in
pFilter - the filter, may be null, meaning all files will be visited
pVisitor - the visitor
Throws:
IllegalArgumentException - if either pDirectory or pVisitor are null
See Also:
Visitor


Copyright © 2015. All Rights Reserved.