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:
4.0.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.
 
Method Summary
static String baseName(File file)
          Returns prefix of the file's name, defined as the part of the name before the final period, or the whole name if there is no final period.
static void copyFile(File from, File to)
          Copies the contents of one file into another.
static String extension(File file)
          Returns the suffix of the file's name, defined as the part of the name after the final period, or null if there is no period in the name.
static byte[] readBytesFromFile(File file)
          Returns the array of bytes read from the specified file.
static char[] readCharsFromFile(File file, String encoding)
          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, String encoding)
          Reads all of the bytes from the specified file and convert them to a string using the specified character set.
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 void writeBytesToFile(byte[] bytes, File file)
          Writes the specified bytes to the specified file.
static void writeCharsToFile(char[] chars, File file, String encoding)
          Writes the characters to the specified file, encoded using the specified character set.
static void writeStringToFile(String s, File file, String encoding)
          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

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 encoding)
                             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.
encoding - Character encoding used by file.
Throws:
IOException - If there is an underlying I/O exception.

writeStringToFile

public static void writeStringToFile(String s,
                                     File file,
                                     String encoding)
                              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.
encoding - Character set to use for encoding.
Throws:
IOException - If there is an underlying I/O exception.

readCharsFromFile

public static char[] readCharsFromFile(File file,
                                       String encoding)
                                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.
encoding - Encoding to decode bytes in file.
Returns:
Characters in the file.
Throws:
IOException - If there is an underlying I/O exception.
UnsupportedEncodingException - If the encoding is not supported.
IllegalArgumentException - If the file is longer than the maximum integer value.

readFromFile

public static String readFromFile(File file,
                                  String encoding)
                           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.
encoding - Encoding to decode bytes in file.
Returns:
Characters in the file.
Throws:
IOException - If there is an underlying I/O exception.
UnsupportedEncodingException - If the encoding is not supported.

baseName

public static String baseName(File file)
Returns prefix of the file's name, defined as the part of the name before the final period, or the whole name if there is no final period.

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

extension

public static String extension(File file)
Returns the suffix of the file's name, defined as the part of the name after the final period, or null if there is no period in the name.

Returns:
The file name's extension.

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.

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.