org.dawb.common.util.io
Class FileUtils

java.lang.Object
  extended by org.dawb.common.util.io.FileUtils

public final class FileUtils
extends java.lang.Object

A class with a collection of file management static utility classes. Several method contents copied from code snippets available on the web.

Author:
fcp94556

Field Summary
static char BOM
           
 
Constructor Summary
FileUtils()
           
 
Method Summary
static void copy(java.io.File source_file, java.io.File destination_file)
          Overwrites destination_file if it exists, creates new if not.
static void copy(java.io.File source_file, java.io.File destination_file, byte[] buffer)
          Overwrites destination_file if it exists, creates new if not.
static void copy(java.io.File source_file, java.lang.String destination_dir)
           
static void copyNio(java.io.File source_file, java.io.File destination_file)
           
static void copyNioNoCopyOver(java.io.File source_file, java.io.File destination_file)
           
static java.io.File createNewUniqueDir(java.io.File file)
           
static java.io.File createNewUniqueDir(java.io.File file, java.lang.String templateName)
           
static void deleteContents(java.io.File parent)
           
static java.lang.String formatSize(long longSize, int decimalPos)
          Formats a file size
static byte[] getByteArrayFromStream(java.io.InputStream source)
           
static byte[] getByteArrayFromStream(java.io.InputStream source, boolean shouldClose)
           
static java.io.ByteArrayOutputStream getByteStream(java.io.InputStream source)
           
static java.io.ByteArrayOutputStream getByteStream(java.io.InputStream source, boolean shouldClose)
           
static java.lang.String getDirectory(java.lang.String filePath)
           
static long getDiskSpace(java.io.File dir)
           
static java.lang.String getFileExtension(java.io.File file)
          Get File extension (result will NOT include ".")
static java.lang.String getFileExtension(java.lang.String fileName)
          Get File extension (result will NOT include ".")
static java.lang.String getFileNameNoExtension(java.io.File file)
          Get Filename minus it's extension if present
static long getFileSizeRecursive(java.io.File file, boolean subFolders)
           
static java.lang.String getParentDirName(java.lang.String name)
           
static java.io.File getUnique(java.io.File dir, java.lang.String template, java.lang.String ext)
          Generates a unique file of the name template or template+an integer
static java.io.File getUnique(java.io.File dir, java.lang.String template, java.lang.String ext, int i)
           
static boolean isBOM(java.lang.String stringToTest)
          Returns true if the string is starting with a BOM
static boolean isDiskSpaceAvaliable(java.io.File tmp, long sizeInB)
           
static boolean isDiskSpaceAvaliableMB(java.io.File tmp, long sizeInMB)
           
static boolean isUnix(java.lang.String stringToTest)
          This method returns true if file is Unix and false if Windows line endings.
static java.lang.StringBuffer readFile(java.io.File file)
           
static java.lang.StringBuffer readFile(java.io.InputStream in)
           
static java.lang.StringBuffer readFile(java.io.InputStream in, java.lang.String charsetName)
           
static java.util.List<java.lang.String> readFileAsList(java.io.File f)
           
static java.util.List<java.lang.String> readFileAsList(java.io.File file, java.lang.String encodingOfFile, boolean removeBom, boolean trimLines)
          Reads a file using the encoding parameter passed and returns each line as an item in the result list Optionally: the BOM can be removed (first character of file if present) the line can be trimmed of whitespace
static void recursiveCopy(java.io.File source_dir, java.io.File destination_dir)
          Recursively copy one folder to another Deleting the contents of the destination folder before copying.
static void recursiveCopyNio(java.io.File source_dir, java.io.File destination_dir)
          Recursively copy one folder to another Deleting the contents of the destination folder before copying.
static void recursiveCopyNioNoDelete(java.io.File source_dir, java.io.File destination_dir)
          Recursively copy one folder to another Not deleting the contents of the destination folder before copying.
static boolean recursiveDelete(java.io.File parent)
           
static void recursiveDeleteOnExit(java.io.File parent)
          Recursively delete parent folder on exit of JVM
static void recursiveIncrementalCopy(java.io.File source_dir, java.io.File destination_dir)
          Recursively copy one folder to another, but not deleting destination data.
static void recursiveIncrementalCopy(java.io.File source_dir, java.io.File destination_dir, int[] inf)
          Recursively copy one folder to another, but not deleting destination data.
static java.lang.String setExtension(java.lang.String path, java.lang.String ext)
           
static void write(java.io.BufferedInputStream source, java.io.File destination_file)
           
static void write(java.io.File file, java.util.List<java.lang.String> list)
           
static void write(java.io.File file, java.lang.String text)
          This method attempts to write a string to file in US-ASCII.
static void write(java.io.File file, java.lang.String text, java.lang.String encoding)
           
static void write(java.io.InputStream source_raw, java.io.OutputStream destination_raw)
           
static void write(java.io.OutputStream out, java.lang.String text, java.lang.String charset, boolean addBomIfNeeded)
          This method writes to a stream a potentially large String.
static void writeToFile(java.io.File file, java.io.InputStream fromNoClose)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BOM

public static final char BOM
Constructor Detail

FileUtils

public FileUtils()
Method Detail

recursiveDelete

public static final boolean recursiveDelete(java.io.File parent)
Parameters:
parent -
Returns:
boolean

deleteContents

public static final void deleteContents(java.io.File parent)
Parameters:
parent -

getUnique

public static java.io.File getUnique(java.io.File dir,
                                     java.lang.String template,
                                     java.lang.String ext)
Generates a unique file of the name template or template+an integer

Parameters:
dir -
template -
ext -
Returns:
a unique file.

getUnique

public static java.io.File getUnique(java.io.File dir,
                                     java.lang.String template,
                                     java.lang.String ext,
                                     int i)
Parameters:
dir -
template -
ext -
i -
Returns:
file

recursiveDeleteOnExit

public static final void recursiveDeleteOnExit(java.io.File parent)
Recursively delete parent folder on exit of JVM

Parameters:
parent -

recursiveCopy

public static final void recursiveCopy(java.io.File source_dir,
                                       java.io.File destination_dir)
                                throws java.io.IOException
Recursively copy one folder to another Deleting the contents of the destination folder before copying. Use at your peril!

Parameters:
source_dir -
destination_dir -
Throws:
java.io.IOException

recursiveCopyNio

public static final void recursiveCopyNio(java.io.File source_dir,
                                          java.io.File destination_dir)
                                   throws java.io.IOException
Recursively copy one folder to another Deleting the contents of the destination folder before copying. Use at your peril!

Parameters:
source_dir -
destination_dir -
Throws:
java.io.IOException

recursiveCopyNioNoDelete

public static final void recursiveCopyNioNoDelete(java.io.File source_dir,
                                                  java.io.File destination_dir)
                                           throws java.io.IOException
Recursively copy one folder to another Not deleting the contents of the destination folder before copying. Any file that already exists will not be copied.

Parameters:
source_dir -
destination_dir -
Throws:
java.io.IOException

recursiveIncrementalCopy

public static final void recursiveIncrementalCopy(java.io.File source_dir,
                                                  java.io.File destination_dir)
                                           throws java.io.IOException
Recursively copy one folder to another, but not deleting destination data. Only copies files if timestamp indicates the data is newer. inf[0] - returns files copied, can be used to track progress inf[1] - returns files skipped, can be used to track progress inf[2] - if set to -1 will halt copy

Parameters:
source_dir -
destination_dir -
Throws:
java.io.IOException

recursiveIncrementalCopy

public static final void recursiveIncrementalCopy(java.io.File source_dir,
                                                  java.io.File destination_dir,
                                                  int[] inf)
                                           throws java.io.IOException
Recursively copy one folder to another, but not deleting destination data. Only copies files if timestamp indicates the data is newer. inf[0] - returns files copied, can be used to track progress inf[1] - returns files skipped, can be used to track progress inf[2] - if set to -1 will halt copy

Parameters:
source_dir -
destination_dir -
inf -
Throws:
java.io.IOException

copy

public static final void copy(java.io.File source_file,
                              java.lang.String destination_dir)
                       throws java.io.IOException
Parameters:
source_file -
destination_dir -
Throws:
java.io.IOException

copy

public static final void copy(java.io.File source_file,
                              java.io.File destination_file)
                       throws java.io.IOException
Overwrites destination_file if it exists, creates new if not.

Parameters:
source_file -
destination_file -
Throws:
java.io.IOException

copy

public static final void copy(java.io.File source_file,
                              java.io.File destination_file,
                              byte[] buffer)
                       throws java.io.IOException
Overwrites destination_file if it exists, creates new if not.

Parameters:
source_file -
destination_file -
buffer -
Throws:
java.io.IOException

copyNio

public static final void copyNio(java.io.File source_file,
                                 java.io.File destination_file)
                          throws java.io.IOException
Parameters:
source_file -
destination_file -
Throws:
java.io.IOException

copyNioNoCopyOver

public static final void copyNioNoCopyOver(java.io.File source_file,
                                           java.io.File destination_file)
                                    throws java.io.IOException
Parameters:
source_file -
destination_file -
Throws:
java.io.IOException

write

public static final void write(java.io.BufferedInputStream source,
                               java.io.File destination_file)
                        throws java.io.IOException
Parameters:
source -
destination_file -
Throws:
java.io.IOException

write

public static final void write(java.io.InputStream source_raw,
                               java.io.OutputStream destination_raw)
                        throws java.io.IOException
Parameters:
source_raw -
destination_raw -
Throws:
java.io.IOException

getByteArrayFromStream

public static final byte[] getByteArrayFromStream(java.io.InputStream source)
                                           throws java.io.IOException
Parameters:
source -
Returns:
byte[]
Throws:
java.io.IOException

getByteArrayFromStream

public static final byte[] getByteArrayFromStream(java.io.InputStream source,
                                                  boolean shouldClose)
                                           throws java.io.IOException
Parameters:
source -
shouldClose -
Returns:
byte[]
Throws:
java.io.IOException

getByteStream

public static final java.io.ByteArrayOutputStream getByteStream(java.io.InputStream source)
                                                         throws java.io.IOException
Parameters:
source -
Returns:
ByteArrayOutputStream
Throws:
java.io.IOException

getByteStream

public static final java.io.ByteArrayOutputStream getByteStream(java.io.InputStream source,
                                                                boolean shouldClose)
                                                         throws java.io.IOException
Parameters:
source -
shouldClose -
Returns:
ByteArrayOutputStream
Throws:
java.io.IOException

readFile

public static final java.lang.StringBuffer readFile(java.io.File file)
                                             throws java.lang.Exception
Parameters:
file -
Returns:
StringBuffer
Throws:
java.lang.Exception

readFile

public static final java.lang.StringBuffer readFile(java.io.InputStream in)
                                             throws java.lang.Exception
Parameters:
in -
Returns:
StringBuffer
Throws:
java.lang.Exception

readFile

public static final java.lang.StringBuffer readFile(java.io.InputStream in,
                                                    java.lang.String charsetName)
                                             throws java.lang.Exception
Parameters:
in -
charsetName -
Returns:
StringBuffer
Throws:
java.lang.Exception

readFileAsList

public static final java.util.List<java.lang.String> readFileAsList(java.io.File f)
                                                             throws java.lang.Exception
Parameters:
f -
Returns:
List
Throws:
java.lang.Exception

readFileAsList

public static final java.util.List<java.lang.String> readFileAsList(java.io.File file,
                                                                    java.lang.String encodingOfFile,
                                                                    boolean removeBom,
                                                                    boolean trimLines)
                                                             throws java.io.IOException
Reads a file using the encoding parameter passed and returns each line as an item in the result list Optionally: the BOM can be removed (first character of file if present) the line can be trimmed of whitespace

Parameters:
file - The file to read
encodingOfFile - The encoding format the file should be read as
removeBom - Whether the BOM should be removed, if present.
trimLines - If each line should have leading and trailing whitespace removed
Returns:
List lines of the file optionally trimmed
Throws:
java.io.IOException - If the file can not be read

isUnix

public static boolean isUnix(java.lang.String stringToTest)
This method returns true if file is Unix and false if Windows line endings.

Parameters:
stringToTest - The text of the file
Returns:
boolean true if the file is Unix.

isBOM

public static boolean isBOM(java.lang.String stringToTest)
Returns true if the string is starting with a BOM

Parameters:
stringToTest -
Returns:
boolean

write

public static void write(java.io.File file,
                         java.lang.String text,
                         java.lang.String encoding)
                  throws java.lang.Exception
Parameters:
file -
text -
encoding -
Throws:
java.lang.Exception

write

public static void write(java.io.File file,
                         java.lang.String text)
                  throws java.lang.Exception
This method attempts to write a string to file in US-ASCII. The code was moved directly from atos.SaveTextFile and no check has been made to how efficient it is.

Parameters:
file -
text -
Throws:
java.lang.Exception

write

public static void write(java.io.OutputStream out,
                         java.lang.String text,
                         java.lang.String charset,
                         boolean addBomIfNeeded)
                  throws java.lang.Exception
This method writes to a stream a potentially large String. The current limit for Java servlets in 90Mb with this method there is a workaround in Java 1.5 for this problem: http://forum.java.sun.com/thread.jspa?threadID=418441&messageID=2816084 http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5026745

Parameters:
out -
text -
charset -
addBomIfNeeded -
Throws:
java.lang.Exception

write

public static void write(java.io.File file,
                         java.util.List<java.lang.String> list)
                  throws java.lang.Exception
Parameters:
file -
list -
Throws:
java.lang.Exception

createNewUniqueDir

public static java.io.File createNewUniqueDir(java.io.File file)
Parameters:
file -
Returns:
File

createNewUniqueDir

public static java.io.File createNewUniqueDir(java.io.File file,
                                              java.lang.String templateName)
Parameters:
file -
templateName -
Returns:
File

getParentDirName

public static final java.lang.String getParentDirName(java.lang.String name)
Parameters:
name -
Returns:
String

writeToFile

public static final void writeToFile(java.io.File file,
                                     java.io.InputStream fromNoClose)
                              throws java.lang.Exception
Parameters:
file -
fromNoClose -
Throws:
java.lang.Exception

getDiskSpace

public static long getDiskSpace(java.io.File dir)
Parameters:
dir -
Returns:
long

isDiskSpaceAvaliableMB

public static boolean isDiskSpaceAvaliableMB(java.io.File tmp,
                                             long sizeInMB)
                                      throws java.io.IOException
Parameters:
tmp -
sizeInMB -
Returns:
boolean
Throws:
java.io.IOException

isDiskSpaceAvaliable

public static boolean isDiskSpaceAvaliable(java.io.File tmp,
                                           long sizeInB)
                                    throws java.io.IOException
Parameters:
tmp -
sizeInB -
Returns:
boolean
Throws:
java.io.IOException

getFileSizeRecursive

public static long getFileSizeRecursive(java.io.File file,
                                        boolean subFolders)
Parameters:
file -
subFolders -
Returns:
long

getFileExtension

public static java.lang.String getFileExtension(java.lang.String fileName)
Get File extension (result will NOT include ".")

Parameters:
fileName -
Returns:
String file extension value, or "" is no extension

getFileExtension

public static java.lang.String getFileExtension(java.io.File file)
Get File extension (result will NOT include ".")

Parameters:
file -
Returns:
String file extension value, or "" is no extension

getFileNameNoExtension

public static java.lang.String getFileNameNoExtension(java.io.File file)
Get Filename minus it's extension if present

Parameters:
file - File to get filename from
Returns:
String filename minus its extension

formatSize

public static java.lang.String formatSize(long longSize,
                                          int decimalPos)
Formats a file size

Parameters:
longSize -
decimalPos -
Returns:
formatted string for size.

setExtension

public static java.lang.String setExtension(java.lang.String path,
                                            java.lang.String ext)

getDirectory

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