com.mizar.commons.io
Class FileUtils

java.lang.Object
  extended by org.apache.commons.io.FileUtils
      extended by com.mizar.commons.io.FileUtils

public class FileUtils
extends org.apache.commons.io.FileUtils


Field Summary
 
Fields inherited from class org.apache.commons.io.FileUtils
EMPTY_FILE_ARRAY, ONE_GB, ONE_KB, ONE_MB
 
Constructor Summary
protected FileUtils()
           
 
Method Summary
static void close(java.io.BufferedReader inStream)
          The method handles null values and ignores any IOException
static void close(java.io.FileReader inStream)
          The method handles null values and ignores any IOException
static void close(java.io.InputStream inStream)
          The method handles null values and ignores any IOException
static void close(java.io.OutputStream outStream)
          The method handles null values and ignores any IOException
static boolean confirmFile(java.lang.String fileName)
           
static boolean confirmFolder(java.lang.String folder)
          Confirms the existance of a folder.
static boolean confirmParentFolder(java.lang.String fileName)
          Confirms, or creates, the existance of the parent folder of a given filename
static boolean containsFiles(java.io.File folder)
           
static boolean containsFileType(java.io.File folder, java.lang.String ext)
           
static boolean containsFolders(java.io.File folder)
           
static void copyFileToDirectory(java.lang.String filePath, java.lang.String dirPath)
          The method is a wrapper for org.apache.commons.io.copyFileToDirectory(File, File)
static boolean copyToAbsoluteFile(java.lang.String source, java.lang.String dest)
           
static java.util.Map<java.lang.String,java.lang.Long> countExtensions(java.io.File folder)
          Folders are returned with a key value of "folder"
static java.lang.String findFolder(java.lang.String startFolder, java.lang.String hitPattern, java.lang.String missPattern, java.lang.String ext)
           
static void findFolders(java.util.List<java.lang.String> matchedFolders, java.lang.String startFolder, java.lang.String hitPattern, java.lang.String missPattern, java.lang.String ext)
          This method is recursive which is why we have to pass the matchedFolders List as an argument
static long getDirSize(java.io.File dir)
           
static java.lang.String getExtension(java.io.File file)
          This method is different than in that it does not force the return to uppercase and it does try to standardize the type, e.g.
static java.lang.String getExtension(java.lang.String filename)
          This method is different than in that it does not force the return to uppercase and it does try to standardize the type, e.g.
static java.util.List<java.lang.String> getFilesInDir(java.util.List<java.lang.String> list, java.io.File dir)
           
static java.lang.String getFileType(java.lang.String fileName)
          The method matches the pattern filename.file-type where file-type is a the last 3 characters in fileName and is immediately preceded by a period character.
static java.lang.String getName(java.lang.String filePath)
           
static java.lang.String getParentFolder(java.lang.String filePath)
          Returns the parent folder of the specified file.
static java.lang.String getReadableFileSize(long bytes)
          Deprecated. - use MathUtils.getReadableByteSize
static boolean isBinary(java.io.File file)
           
static boolean isBinary(java.lang.String fileName)
           
static boolean looksLike(java.lang.String folder, java.lang.String hitPattern, java.lang.String missPattern)
           
static void main(java.lang.String[] args)
           
static boolean moreThanLines(java.io.File file, int maxLines)
           
static java.util.List<java.lang.String> readAsciiFile(java.io.File file, int maxLines)
          The method reads an ASCII file into a List of Strings
static java.lang.String readFromAbsoluteFile(java.lang.String fullPath)
           
static java.lang.String readInputStream(java.io.InputStream is)
           
static java.util.List<java.lang.String> readLines(java.io.File file, int maxLines)
          The method is very much like <@link readLines(File) readLines(File)> except that it only reads maxLines> making it useful to looking at the beginnings of large files.
static boolean removeFolder(java.io.File folder)
          Deletes a folder including all child folders and files
static boolean removeFolder(java.lang.String folderName)
           
static boolean renameTo(java.io.File file, java.lang.String newname)
          This is a hack to replace File.renameTo(File) because as of Java 1.5 the File.renameTo(File) method can get hung up on the garbage collector.
static boolean renameToConsistentCase(java.io.File file, java.lang.String filename)
          The fact that Windows files are nominally case insensitive results in an observed laziness with respect to name case consistency.
static java.lang.String replaceExtension(java.lang.String filename, java.lang.String newExtension)
           
static java.lang.String replaceFolder(java.io.File file, java.io.File folder)
           
static java.lang.String replaceFolder(java.lang.String filename, java.io.File folder)
           
static java.lang.String replaceFolder(java.lang.String filename, java.lang.String newFolderName)
           
static java.lang.String simpleFileName(java.lang.String anyString)
          The method handles any leading numbers and removes spaces and most special characters
static java.lang.String uniqueFileName(java.lang.String absolutePath)
           
static java.lang.String uniqueFileName(java.lang.String absolutePath, java.lang.String extension)
          The method will append extension to absolutePath and test if the file exists.
static int writeContentToAbsoluteFile(byte[] bytes, java.lang.String fileName)
           
 
Methods inherited from class org.apache.commons.io.FileUtils
byteCountToDisplaySize, checksum, checksumCRC32, cleanDirectory, contentEquals, convertFileCollectionToFileArray, copyDirectory, copyDirectory, copyDirectory, copyDirectory, copyDirectoryToDirectory, copyFile, copyFile, copyFileToDirectory, copyFileToDirectory, copyInputStreamToFile, copyURLToFile, copyURLToFile, deleteDirectory, deleteQuietly, forceDelete, forceDeleteOnExit, forceMkdir, getTempDirectory, getTempDirectoryPath, getUserDirectory, getUserDirectoryPath, isFileNewer, isFileNewer, isFileNewer, isFileOlder, isFileOlder, isFileOlder, isSymlink, iterateFiles, iterateFiles, lineIterator, lineIterator, listFiles, listFiles, moveDirectory, moveDirectoryToDirectory, moveFile, moveFileToDirectory, moveToDirectory, openInputStream, openOutputStream, readFileToByteArray, readFileToString, readFileToString, readLines, readLines, sizeOf, sizeOfDirectory, toFile, toFiles, touch, toURLs, waitFor, write, write, writeByteArrayToFile, writeLines, writeLines, writeLines, writeLines, writeStringToFile, writeStringToFile
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileUtils

protected FileUtils()
Method Detail

getFileType

public static java.lang.String getFileType(java.lang.String fileName)

The method matches the pattern filename.file-type where file-type is a the last 3 characters in fileName and is immediately preceded by a period character. Two notable extensions to this simple pattern are JPEG and TIFF which return JPG and TIF respectively.

The method uses the java.util.regex package to match against the following patterns:

  1. For *.XXX -- ".\*\.[a-z][a-z][a-z]$"
  2. For *.JP[E]G -- ".\*\.jp(?:e)?g$"
  3. For *.TI[F]F -- ".\*\.ti(?:f)?f$"
  4. For *.HTM[L] -- ".\*\.htm[l]?$"

Parameters:
fileName - which may or may not include a folder path
Returns:
file-type a 3 character (uppercase) file extension or "UNKNOWN" if a file type match cannot be found.
See Also:
getExtension(File)

getExtension

public static java.lang.String getExtension(java.io.File file)
This method is different than in that it does not force the return to uppercase and it does try to standardize the type, e.g. it returns Jpeg for x.Jpeg not JPG as would

Parameters:
file -
Returns:
the part of the file name that follows the last period "."
See Also:
getFileType(java.lang.String)

getExtension

public static java.lang.String getExtension(java.lang.String filename)
This method is different than in that it does not force the return to uppercase and it does try to standardize the type, e.g. it returns Jpeg for x.Jpeg not JPG as would

Parameters:
filename -
Returns:
the part of the file name that follows the last period "."
See Also:
getFileType(java.lang.String), FilenameUtils.getExtension(java.lang.String)

replaceExtension

public static java.lang.String replaceExtension(java.lang.String filename,
                                                java.lang.String newExtension)

countExtensions

public static java.util.Map<java.lang.String,java.lang.Long> countExtensions(java.io.File folder)
Folders are returned with a key value of "folder"

Parameters:
folder - A directory folder to inspect
Returns:
a Map keyed by file type with the instance count as values

replaceFolder

public static java.lang.String replaceFolder(java.lang.String filename,
                                             java.lang.String newFolderName)

replaceFolder

public static java.lang.String replaceFolder(java.lang.String filename,
                                             java.io.File folder)

replaceFolder

public static java.lang.String replaceFolder(java.io.File file,
                                             java.io.File folder)

confirmFolder

public static boolean confirmFolder(java.lang.String folder)
Confirms the existance of a folder. If the folder does not exist, it is created.

Parameters:
folder - Folder to find, if not found, create it
Returns:
true if the folder exists, false if it does not exist and couldn't be created.

confirmParentFolder

public static boolean confirmParentFolder(java.lang.String fileName)
Confirms, or creates, the existance of the parent folder of a given filename

Parameters:
fileName - file name to create a parent directory for
Returns:
true on success, false on failure

getParentFolder

public static java.lang.String getParentFolder(java.lang.String filePath)
Returns the parent folder of the specified file. If filePath does not exist; it is created as an empty file; which causes the parent folder to be created.

Parameters:
filePath - a fully defined file name (with path)
Returns:
the path of the parent folder

getName

public static java.lang.String getName(java.lang.String filePath)

simpleFileName

public static java.lang.String simpleFileName(java.lang.String anyString)
The method handles any leading numbers and removes spaces and most special characters

Parameters:
anyString -
Returns:
a simplified file name

uniqueFileName

public static java.lang.String uniqueFileName(java.lang.String absolutePath)
Parameters:
absolutePath -
Returns:

main

public static void main(java.lang.String[] args)

uniqueFileName

public static java.lang.String uniqueFileName(java.lang.String absolutePath,
                                              java.lang.String extension)
The method will append extension to absolutePath and test if the file exists.

Parameters:
absolutePath - the absolute path of a file but without the extension
extension - the extension to append for the complete file name
Returns:
a unique filename based on the absolutePath but without the extension

confirmFile

public static boolean confirmFile(java.lang.String fileName)
Parameters:
fileName - the absolute path to a file
Returns:
true if the file exists and false if no
See Also:
File.exists()

writeContentToAbsoluteFile

public static int writeContentToAbsoluteFile(byte[] bytes,
                                             java.lang.String fileName)
                                      throws java.io.FileNotFoundException,
                                             java.io.IOException
Parameters:
bytes -
fileName - an absolute path to a file
Returns:
the number of bytes written
Throws:
java.io.FileNotFoundException
java.io.IOException

readInputStream

public static java.lang.String readInputStream(java.io.InputStream is)
                                        throws java.io.IOException
Parameters:
is - an open InputStream
Returns:
the entire InputStream as a String
Throws:
java.io.IOException

readFromAbsoluteFile

public static java.lang.String readFromAbsoluteFile(java.lang.String fullPath)
                                             throws java.io.FileNotFoundException,
                                                    java.io.IOException
Parameters:
fullPath - an absolute file path name
Returns:
the contents of fullPath
Throws:
java.io.FileNotFoundException
java.io.IOException

moreThanLines

public static boolean moreThanLines(java.io.File file,
                                    int maxLines)
                             throws java.io.IOException
Parameters:
file -
maxLines -
Returns:
true if file is an ASCII file with more than maxLines lines of text
Throws:
java.io.IOException

readLines

public static java.util.List<java.lang.String> readLines(java.io.File file,
                                                         int maxLines)
                                                  throws java.io.IOException
The method is very much like <@link readLines(File) readLines(File)> except that it only reads maxLines> making it useful to looking at the beginnings of large files.

Parameters:
file -
maxLines -
Returns:
Throws:
java.io.IOException

copyFileToDirectory

public static void copyFileToDirectory(java.lang.String filePath,
                                       java.lang.String dirPath)
                                throws java.io.IOException
The method is a wrapper for org.apache.commons.io.copyFileToDirectory(File, File)

Parameters:
filePath -
dirPath -
Throws:
java.io.IOException
See Also:
FileUtils.copyFileToDirectory(File, File)

copyToAbsoluteFile

public static boolean copyToAbsoluteFile(java.lang.String source,
                                         java.lang.String dest)
                                  throws java.io.FileNotFoundException,
                                         java.io.IOException
Parameters:
source -
dest -
Returns:
Throws:
java.io.FileNotFoundException
java.io.IOException

isBinary

public static boolean isBinary(java.io.File file)
                        throws java.io.IOException,
                               java.io.FileNotFoundException
Parameters:
file -
Returns:
true if the file is binary, false if the first megabyte is ASCII
Throws:
java.io.IOException
java.io.FileNotFoundException
See Also:
isBinary(String)

isBinary

public static boolean isBinary(java.lang.String fileName)
                        throws java.io.IOException,
                               java.io.FileNotFoundException
Parameters:
fileName -
Returns:
true if the file is binary, false if the first megabyte is ASCII
Throws:
java.io.IOException
java.io.FileNotFoundException
See Also:
isBinary(File)

removeFolder

public static boolean removeFolder(java.lang.String folderName)
Parameters:
folderName -
Returns:
true if the folder does not exist or if it exists and it is a folder and it was deleted
See Also:
removeFolder(File)

removeFolder

public static boolean removeFolder(java.io.File folder)
Deletes a folder including all child folders and files

Parameters:
folder -
Returns:
true if all deletions succeeded

renameTo

public static boolean renameTo(java.io.File file,
                               java.lang.String newname)
This is a hack to replace File.renameTo(File) because as of Java 1.5 the File.renameTo(File) method can get hung up on the garbage collector. This method tries up to 20 times with a request to System.gc() and a short sleep between each try.

Parameters:
file -
newname -
Returns:

renameToConsistentCase

public static boolean renameToConsistentCase(java.io.File file,
                                             java.lang.String filename)
The fact that Windows files are nominally case insensitive results in an observed laziness with respect to name case consistency. This wrecks havoc when the files are shared with or transferred to a case sensitive O/S such as Linux. This method is designed to be used in utilities that want to force names to a consistent case pattern.

Parameters:
file - a file to test for case naming consistency
filename - the correct name case configuration
Returns:
true if file.getName().equalsIgnoreCase(filename)

looksLike

public static boolean looksLike(java.lang.String folder,
                                java.lang.String hitPattern,
                                java.lang.String missPattern)
Parameters:
folder - a starting folder to start a recursive search from
hitPattern - a Regex pattern that indicates a candidate folder
missPattern - a Regex pattern that indicates a folder to skip even if hitPattern matches
Returns:
true if hitPattern matches folder and missPattern does not

findFolder

public static java.lang.String findFolder(java.lang.String startFolder,
                                          java.lang.String hitPattern,
                                          java.lang.String missPattern,
                                          java.lang.String ext)
                                   throws java.io.IOException
Parameters:
startFolder - a starting folder to start a recursive search from
hitPattern - a Regex pattern that indicates a candidate folder
missPattern - a Regex pattern that indicates a folder to skip even if hitPattern matches
ext - a file extension that the found folder must contain at least one of
Returns:
Throws:
java.io.IOException

findFolders

public static void findFolders(java.util.List<java.lang.String> matchedFolders,
                               java.lang.String startFolder,
                               java.lang.String hitPattern,
                               java.lang.String missPattern,
                               java.lang.String ext)
                        throws java.io.IOException
This method is recursive which is why we have to pass the matchedFolders List as an argument

Parameters:
matchedFolders - a list of folders to add matching folders to
startFolder - a starting folder to start a recursive search from
hitPattern - a Regex pattern that indicates a candidate folder
missPattern - a Regex pattern that indicates a folder to skip even if hitPattern matches
ext - a file extension that the found folder must contain at least one of
Throws:
java.io.IOException

containsFolders

public static boolean containsFolders(java.io.File folder)
Parameters:
folder -
Returns:

containsFiles

public static boolean containsFiles(java.io.File folder)
Parameters:
folder -
Returns:

containsFileType

public static boolean containsFileType(java.io.File folder,
                                       java.lang.String ext)
Parameters:
folder - a folder to test
ext - a file extension that the found folder must contain at least one of
Returns:
true if folder contains at least one file with an extension of ext

readAsciiFile

public static java.util.List<java.lang.String> readAsciiFile(java.io.File file,
                                                             int maxLines)
The method reads an ASCII file into a List of Strings

Parameters:
file -
maxLines - if less than or equal to 0 then file will be read to EOF
Returns:
a List of Strings, one String per line

close

public static void close(java.io.FileReader inStream)
The method handles null values and ignores any IOException

Parameters:
inStream -

close

public static void close(java.io.BufferedReader inStream)
The method handles null values and ignores any IOException

Parameters:
inStream -

close

public static void close(java.io.InputStream inStream)
The method handles null values and ignores any IOException

Parameters:
inStream -

close

public static void close(java.io.OutputStream outStream)
The method handles null values and ignores any IOException

Parameters:
outStream -

getDirSize

public static long getDirSize(java.io.File dir)

getFilesInDir

public static java.util.List<java.lang.String> getFilesInDir(java.util.List<java.lang.String> list,
                                                             java.io.File dir)

getReadableFileSize

@Deprecated
public static java.lang.String getReadableFileSize(long bytes)
Deprecated. - use MathUtils.getReadableByteSize