com.aliasi.util
Class Files

java.lang.Object
  extended by com.aliasi.util.Files

public class Files
extends Object

Static utility methods for processing files.

Since:
LingPipe1.0
Version:
3.0
Author:
Bob Carpenter

Field Summary
static FileFilter FILES_ONLY_FILE_FILTER
          A file filter that accepts all normal files, as specified by File.isFile().
static FileFilter NON_CVS_DIRECTORY_FILE_FILTER
          A file filter that accepts files that are directories that are not named "CVS", ignoring case.
static File TEMP_DIRECTORY
          The temporary directory.
 
Method Summary
static String baseName(File file)
          Returns prefix of the file's name.
static void copyFile(File from, File to)
          Copies the contents of one file into another.
static File createTempFile(String fileName)
          Returns a new file with the specified name that is a daughter of the temporary directory TEMP_DIRECTORY.
static String fileToURLName(File file)
          Converts a file to the string representation of its URL, consisting of the URL prefix and a canonical path name.
static File makeCleanDir(File file)
          Creates a directory in the specified file that has no subdirectories.
static File makeCleanDir(File directory, String name)
          Creates a clean directory with the specified name as a subdirectory of the specified directory.
static String prefix(String name)
          Returns the name of a file before the final period separator.
static byte[] readBytesFromFile(File file)
          Returns the array of bytes read from the specified file.
static char[] readCharsFromFile(File file)
          Reads all of the bytes from the specified file and convert them to a character array using UTF-8 unicode.
static char[] readCharsFromFile(File file, String charset)
          Reads all of the bytes from the specified file and convert them to a character array using the specified character set.
static String readFromFile(File file)
          Reads all of the bytes from the specified file and convert them to a string using Unicode UTF-8.
static String readFromFile(File file, String charset)
          Reads all of the bytes from the specified file and convert them to a string using the specified character set.
static String[] readLinesFromFile(File file, String charset)
          Returns the lines of a file as an array of strings.
static Object readObjectFrom(File file)
          Reads a serialized object from the specified file.
static int removeDescendants(File file)
          Remove the descendants of the specified directory, but not the directory itself.
static int removeRecursive(File file)
          Removes the specified file and if it is a directory, all contained files.
static Object serializeDeserialize(Serializable in)
          Serializes the specifies object and returns its deserialized version.
static String suffix(String name)
          Returns the name of a file after the final period separator.
static void writeBytesToFile(byte[] bytes, File file)
          Writes the specified bytes to the specified file.
static void writeCharsToFile(char[] chars, File file)
          Writes the characters to the specified file, encoded using UTF-8 Unicode.
static void writeCharsToFile(char[] chars, File file, String charset)
          Writes the characters to the specified file, encoded using the specified character set.
static void writeObjectTo(Serializable object, File file)
          Writes the specified object to the specified file.
static void writeStringToFile(String s, File file)
          Writes the string to the specified file, encoded using UTF-8 Unicode.
static void writeStringToFile(String s, File file, String charset)
          Writes the string to the specified file, encoded using the specified character set.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TEMP_DIRECTORY

public static final File TEMP_DIRECTORY
The temporary directory.


NON_CVS_DIRECTORY_FILE_FILTER

public static final FileFilter NON_CVS_DIRECTORY_FILE_FILTER
A file filter that accepts files that are directories that are not named "CVS", ignoring case.


FILES_ONLY_FILE_FILTER

public static final FileFilter FILES_ONLY_FILE_FILTER
A file filter that accepts all normal files, as specified by File.isFile().

Method Detail

writeBytesToFile

public static void writeBytesToFile(byte[] bytes,
                                    File file)
                             throws IOException
Writes the specified bytes to the specified file.

Parameters:
bytes - Bytes to write to file.
file - File to which characters are written.
Throws:
IOException - If there is an underlying I/O exception.

readBytesFromFile

public static byte[] readBytesFromFile(File file)
                                throws IOException
Returns the array of bytes read from the specified file.

Parameters:
file - File from which to read bytes.
Returns:
Bytes read from the specified file.
Throws:
IOException - If there is an underlying I/O exception.

writeCharsToFile

public static void writeCharsToFile(char[] chars,
                                    File file,
                                    String charset)
                             throws IOException
Writes the characters to the specified file, encoded using the specified character set.

Parameters:
chars - Characters to write to file.
file - File to which characters are written.
charset - Character set used to encoded characters.
Throws:
IOException - If there is an underlying I/O exception.

writeCharsToFile

public static void writeCharsToFile(char[] chars,
                                    File file)
                             throws IOException
Writes the characters to the specified file, encoded using UTF-8 Unicode.

Parameters:
chars - Characters to write to file.
file - File to which characters are written.
Throws:
IOException - If there is an underlying I/O exception.

writeStringToFile

public static void writeStringToFile(String s,
                                     File file)
                              throws IOException
Writes the string to the specified file, encoded using UTF-8 Unicode.

Parameters:
s - String to write to file.
file - File to which characters are written.
Throws:
IOException - If there is an underlying I/O exception.

writeStringToFile

public static void writeStringToFile(String s,
                                     File file,
                                     String charset)
                              throws IOException
Writes the string to the specified file, encoded using the specified character set.

Parameters:
s - String to write to file.
file - File to which characters are written.
charset - Character set to use for encoding.
Throws:
IOException - If there is an underlying I/O exception.

readCharsFromFile

public static char[] readCharsFromFile(File file,
                                       String charset)
                                throws IOException
Reads all of the bytes from the specified file and convert them to a character array using the specified character set.

Parameters:
file - File from which to read input.
charset - Charset to decode bytes in file.
Returns:
Characters in the file.
Throws:
IOException - If there is an underlying I/O exception.
UnsupportedEncodingException - If the charset is not supported.

readCharsFromFile

public static char[] readCharsFromFile(File file)
                                throws IOException
Reads all of the bytes from the specified file and convert them to a character array using UTF-8 unicode.

Parameters:
file - File from which to read input.
Returns:
Characters in the file.
Throws:
IOException - If there is an underlying I/O exception.

readFromFile

public static String readFromFile(File file,
                                  String charset)
                           throws IOException
Reads all of the bytes from the specified file and convert them to a string using the specified character set.

Parameters:
file - File from which to read input.
charset - Charset to decode bytes in file.
Returns:
Characters in the file.
Throws:
IOException - If there is an underlying I/O exception.
UnsupportedEncodingException - If the charset is not supported.

readFromFile

public static String readFromFile(File file)
                           throws IOException
Reads all of the bytes from the specified file and convert them to a string using Unicode UTF-8.

Parameters:
file - File from which to read input.
Returns:
Characters in the file.
Throws:
IOException - If there is an underlying I/O exception.

readObjectFrom

public static Object readObjectFrom(File file)
                             throws ClassNotFoundException,
                                    IOException
Reads a serialized object from the specified file.

Parameters:
file - File from which to read object.
Returns:
Object read from file.
Throws:
IOException - If there is an I/O error reading.
ClassNotFoundException - If the serialized object's class is not on the classpath.

writeObjectTo

public static void writeObjectTo(Serializable object,
                                 File file)
                          throws IOException
Writes the specified object to the specified file.

Parameters:
object - Object to write.
file - File to which object is serialized.
Throws:
IOException - If there is an underlying I/O error writing.

prefix

public static String prefix(String name)
Returns the name of a file before the final period separator.

Parameters:
name - Name of file.
Returns:
File name's prefix.

suffix

public static String suffix(String name)
Returns the name of a file after the final period separator.

Parameters:
name - Name of file.
Returns:
File name's suffix.

baseName

public static String baseName(File file)
Returns prefix of the file's name.

Parameters:
file - File whose name's prefix is returned.
Returns:
Prefix of file.

removeRecursive

public static int removeRecursive(File file)
Removes the specified file and if it is a directory, all contained files. Returns number of files removed, including specified one.

Parameters:
file - File or directory to remove.
Returns:
Number of files and directories removed.

removeDescendants

public static int removeDescendants(File file)
Remove the descendants of the specified directory, but not the directory itself. Returns number of files removed.

Parameters:
file - File whose descendants are removed.
Returns:
Number of files or directories removed.

makeCleanDir

public static File makeCleanDir(File directory,
                                String name)
Creates a clean directory with the specified name as a subdirectory of the specified directory.

Parameters:
directory - Parent directory of directory to create.
name - Name of directory to create.
Returns:
Directory with specified name that is daughter of specified parent directory.

makeCleanDir

public static File makeCleanDir(File file)
Creates a directory in the specified file that has no subdirectories. If the specified file exists as a file, it is removed first. If the specified file is a directory with files or subdireictories, they are removed.

Parameters:
file - File to create as a clean directory.
Returns:
Directory with specified name that is daughter of specified parent directory.

createTempFile

public static File createTempFile(String fileName)
Returns a new file with the specified name that is a daughter of the temporary directory TEMP_DIRECTORY. The file will be deleted automicatically when the virtual machine exits.

Parameters:
fileName - Name of file to create.
Returns:
Temporary file with specified name.

fileToURLName

public static String fileToURLName(File file)
                            throws IOException
Converts a file to the string representation of its URL, consisting of the URL prefix and a canonical path name.

Parameters:
file - File to convert to string URL.
Returns:
The string URL corresponding to the specified file.
Throws:
IOException - If there is an exception retrieving the file's canonical path name.

readLinesFromFile

public static String[] readLinesFromFile(File file,
                                         String charset)
                                  throws IOException
Returns the lines of a file as an array of strings. The newline characters are removed.

Parameters:
file - Name of file from which to read lines.
charset - Character set to use to read lines.
Returns:
Array of lines read from file.
Throws:
IOException - If there is an I/O exception opening, closing, or reading from the file.

serializeDeserialize

public static Object serializeDeserialize(Serializable in)
                                   throws ClassNotFoundException,
                                          IOException
Serializes the specifies object and returns its deserialized version. This operates in memory by writing to a byte-array backed object output stream and reading from a byte-array backed object input stream.

Parameters:
in - Object to serialize and then deserialize.
Returns:
Deserialized version of serialized version of specified object.
Throws:
IOException - If there is an I/O error while reading or writing.
ClassNotFoundException - If the class for the object being restored cannot be found.

copyFile

public static void copyFile(File from,
                            File to)
                     throws IOException
Copies the contents of one file into another. Even if there is an exception, both file handles will be closed on exit.

Parameters:
from - File from which to copy.
to - File to which to copy.
Throws:
IOException - If there is a read or write error.