calhoun.util
Class FileUtil

java.lang.Object
  extended by calhoun.util.FileUtil

public class FileUtil
extends java.lang.Object

Utility functions for file management


Method Summary
static java.lang.String appendPath(java.lang.String parent, java.lang.String child)
          Adds a file or directory name to an existing directory name.
static void appendSeparator(java.io.File in)
           
static void copyFile(java.io.File in, java.io.File out)
           
static void copyFile(java.io.File in, java.io.File out, boolean append)
           
static byte determineNewlineSize(java.io.File file)
          Determines the size of a newline character in the given text file.
static java.lang.String fileRecordCompare(java.lang.String expected, java.lang.String actual)
          Convience form of fileRecordCompare which does no masking
static java.lang.String fileRecordCompare(java.lang.String expected, java.lang.String actual, java.lang.String maskingRegEx)
          Compares records in two flat files.
static java.lang.String[] getBaseAndExtension(java.io.File file)
           
static boolean isGzipFile(java.io.File file)
           
static boolean isGzipFile(java.lang.String filename)
           
static boolean isGzipStream(java.io.InputStream s)
          Checks to see if the stream is in GZIP format, assuming that it is open to the start of a file or other resource.
static java.nio.channels.FileLock lockFile(java.io.FileOutputStream fos)
          Locks a file.
static java.nio.channels.FileLock lockFile(java.io.RandomAccessFile raf)
          Locks a file.
static java.io.File makeTempCopy(java.io.File in)
           
static double[] readDoublesFromSingleTabbedLine(java.lang.String fileName)
           
static java.lang.String readFile(java.io.File filename)
           
static java.lang.String readFile(java.lang.String filename)
          Reads a files into a String.
static byte[] readFileAsBytes(java.lang.String filename)
           
static java.lang.String[][] readFlatFile(java.lang.String fileName)
           
static java.lang.String[][] readFlatFileWithComments(java.lang.String fileName)
           
static java.lang.String readInputStream(java.io.InputStream is)
           
static java.lang.String[] readLines(java.lang.String fileName)
           
static java.lang.Object readObject(java.lang.String filename)
          Read a serialized Java object from a file.
static java.lang.String readReader(java.io.Reader reader)
           
static void renameFile(java.io.File oldFile, java.io.File newFile)
           
static void safeClose(java.io.InputStream f)
           
static void safeClose(java.io.OutputStream f)
           
static void safeClose(java.io.RandomAccessFile f)
           
static void safeClose(java.io.Reader f)
           
static void safeClose(java.io.Writer f)
           
static java.io.BufferedWriter safeOpen(java.lang.String file)
           
static void safeRelease(java.nio.channels.FileLock lock)
           
static void safeWrite(java.io.Writer w, java.lang.String s)
           
static void unzipFile(java.io.File file, java.io.File toDirectory)
           
static void writeFile(java.io.File filename, java.lang.String data)
          Writes a string out to a file.
static void writeFile(java.lang.String filename, java.lang.String data)
          Writes a string out to a file.
static void writeObject(java.lang.String filename, java.lang.Object data)
          Serializes a Java object out to a file.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

readFile

public static final java.lang.String readFile(java.io.File filename)
                                       throws java.io.IOException
Throws:
java.io.IOException

appendSeparator

public static final void appendSeparator(java.io.File in)
                                  throws java.lang.Exception
Throws:
java.lang.Exception

copyFile

public static final void copyFile(java.io.File in,
                                  java.io.File out,
                                  boolean append)
                           throws java.lang.Exception
Throws:
java.lang.Exception

copyFile

public static final void copyFile(java.io.File in,
                                  java.io.File out)
                           throws java.lang.Exception
Throws:
java.lang.Exception

renameFile

public static final void renameFile(java.io.File oldFile,
                                    java.io.File newFile)
                             throws java.lang.Exception
Throws:
java.lang.Exception

safeOpen

public static final java.io.BufferedWriter safeOpen(java.lang.String file)

safeWrite

public static final void safeWrite(java.io.Writer w,
                                   java.lang.String s)

unzipFile

public static final void unzipFile(java.io.File file,
                                   java.io.File toDirectory)
                            throws java.lang.Exception
Throws:
java.lang.Exception

isGzipStream

public static final boolean isGzipStream(java.io.InputStream s)
Checks to see if the stream is in GZIP format, assuming that it is open to the start of a file or other resource. Note that this advances the stream, caller should mark and reset around this method.


isGzipFile

public static final boolean isGzipFile(java.io.File file)

isGzipFile

public static final boolean isGzipFile(java.lang.String filename)

getBaseAndExtension

public static final java.lang.String[] getBaseAndExtension(java.io.File file)

makeTempCopy

public static java.io.File makeTempCopy(java.io.File in)
                                 throws java.lang.Exception
Throws:
java.lang.Exception

readInputStream

public static final java.lang.String readInputStream(java.io.InputStream is)
                                              throws java.io.IOException
Throws:
java.io.IOException

readReader

public static final java.lang.String readReader(java.io.Reader reader)
                                         throws java.io.IOException
Throws:
java.io.IOException

readFileAsBytes

public static final byte[] readFileAsBytes(java.lang.String filename)
                                    throws java.io.IOException
Throws:
java.io.IOException

readFile

public static final java.lang.String readFile(java.lang.String filename)
                                       throws java.io.IOException
Reads a files into a String.

Parameters:
filename - Name of the file to read in
Returns:
String containing the contents of the file
Throws:
java.io.IOException

readFlatFile

public static java.lang.String[][] readFlatFile(java.lang.String fileName)
                                         throws java.io.IOException
Throws:
java.io.IOException

readFlatFileWithComments

public static java.lang.String[][] readFlatFileWithComments(java.lang.String fileName)
                                                     throws java.io.IOException
Throws:
java.io.IOException

readDoublesFromSingleTabbedLine

public static double[] readDoublesFromSingleTabbedLine(java.lang.String fileName)
                                                throws java.io.IOException
Throws:
java.io.IOException

readLines

public static java.lang.String[] readLines(java.lang.String fileName)
                                    throws java.io.IOException
Throws:
java.io.IOException

writeFile

public static final void writeFile(java.io.File filename,
                                   java.lang.String data)
                            throws java.io.IOException
Writes a string out to a file.

Parameters:
filename - Name of the file to read in
data - The string to write to the file
Throws:
java.io.IOException

writeObject

public static final void writeObject(java.lang.String filename,
                                     java.lang.Object data)
                              throws java.io.IOException
Serializes a Java object out to a file.

Parameters:
filename - Name of the file to write
data - The object to serialize. Must be Serializable
Throws:
java.io.IOException

readObject

public static final java.lang.Object readObject(java.lang.String filename)
                                         throws java.io.IOException,
                                                java.lang.ClassNotFoundException
Read a serialized Java object from a file.

Parameters:
filename - Name of the file to read in
Returns:
The object which was deserialized.
Throws:
java.io.IOException
java.lang.ClassNotFoundException

fileRecordCompare

public static final java.lang.String fileRecordCompare(java.lang.String expected,
                                                       java.lang.String actual,
                                                       java.lang.String maskingRegEx)
                                                throws java.io.IOException
Compares records in two flat files. Just checks that every line in one file occurs in the other. Reports differences. Like fileDiff, except that it is insensitive to differences in ordering.

Throws:
java.io.IOException

fileRecordCompare

public static final java.lang.String fileRecordCompare(java.lang.String expected,
                                                       java.lang.String actual)
                                                throws java.io.IOException
Convience form of fileRecordCompare which does no masking

Throws:
java.io.IOException

writeFile

public static final void writeFile(java.lang.String filename,
                                   java.lang.String data)
                            throws java.io.IOException
Writes a string out to a file.

Parameters:
filename - Name of the file to read in
data - The string ot write to the file
Throws:
java.io.IOException

appendPath

public static final java.lang.String appendPath(java.lang.String parent,
                                                java.lang.String child)
Adds a file or directory name to an existing directory name. Handles duplicate or missing slashes

Parameters:
parent - Name of the parent directory
child - Name of the child directory
Returns:
The concatenated path.

safeClose

public static final void safeClose(java.io.Writer f)

safeClose

public static final void safeClose(java.io.Reader f)

safeClose

public static final void safeClose(java.io.OutputStream f)

safeClose

public static final void safeClose(java.io.InputStream f)

safeClose

public static final void safeClose(java.io.RandomAccessFile f)

lockFile

public static final java.nio.channels.FileLock lockFile(java.io.RandomAccessFile raf)
Locks a file. This lock will work across JVMs but not necessarily across threads within a single JVM. You must have write permission to lock a file.


lockFile

public static final java.nio.channels.FileLock lockFile(java.io.FileOutputStream fos)
Locks a file. This lock will work across JVMs but not necessarily across threads within a single JVM. You must have write permission to lock a file.


safeRelease

public static final void safeRelease(java.nio.channels.FileLock lock)

determineNewlineSize

public static byte determineNewlineSize(java.io.File file)
                                 throws java.io.IOException
Determines the size of a newline character in the given text file. Will return 1 or 2 depending on the platform the file was created on.

Parameters:
file - the file object to examine.
Returns:
the size of a newline in this file
Throws:
java.io.IOException