edu.neu.ccs.util
Class TextTools

java.lang.Object
  extended byedu.neu.ccs.util.TextTools

public class TextTools
extends Object

Class TextTools is a special purpose class for reading and writing data on local disk files.

See also:

Version:
2.3.3

Constructor Summary
private TextTools()
          Prevent instantiation.
 
Method Summary
static void copyTextFile(String oldPath, String newPath)
          Copies a text file from one place to another.
static String[] extractNonEmptyLines(String data)
          Finds and trims the lines in the given data and returns the trimmed lines that are non-empty into a string array.
static String makePath(String directory, String filename)
          Makes a file path from the directory name and file name adding a path separator character if necessary.
static String[] readFileNames(String directory, String filelist)
          Returns the file names in the filelist text file located in the given directory.
static String[] readNonEmptyLines(String path)
          Reads and trims the lines in the given file and returns the trimmed lines that are non-empty into a string array.
static String readTextFile(String path)
          Reads a text file and returns all of its data as a string.
static void writeTextFile(String path, String data)
          Writes a text file whose contents will be the given data.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TextTools

private TextTools()
Prevent instantiation.

Method Detail

readTextFile

public static String readTextFile(String path)

Reads a text file and returns all of its data as a string.

Assumes that the entire file may be read into memory at once.

Throws RuntimeException if an error occurs.

Parameters:
path - the text file path on the local file system
Returns:
the text file contents as a string

writeTextFile

public static void writeTextFile(String path,
                                 String data)

Writes a text file whose contents will be the given data.

Throws RuntimeException if an error occurs.

Parameters:
path - the text file path on the local file system
data - the text file contents as a string

copyTextFile

public static void copyTextFile(String oldPath,
                                String newPath)

Copies a text file from one place to another.

Assumes that the entire file can be read into memory at once.

Parameters:
oldPath - the source text file path on the local file system
newPath - the target text file path on the local file system

readNonEmptyLines

public static String[] readNonEmptyLines(String path)

Reads and trims the lines in the given file and returns the trimmed lines that are non-empty into a string array.

Throws RuntimeException if an error occurs.

Assumes that the entire file can be read into memory at once.

Parameters:
path - the text file path on the local file system
Returns:
the array with the non-empty trimmed lines from the text file

extractNonEmptyLines

public static String[] extractNonEmptyLines(String data)

Finds and trims the lines in the given data and returns the trimmed lines that are non-empty into a string array.

Parameters:
data - the string data to convert into an array of lines
Returns:
the array with the non-empty trimmed lines from the data

readFileNames

public static String[] readFileNames(String directory,
                                     String filelist)

Returns the file names in the filelist text file located in the given directory.

Assumes that filelist is the name of a text file in the same directory as the files listed within it.

The filelist text file should contain one name per line. Blank lines are permitted as separator lines.

Appends the file separatory character to the given directory string if necessary.

If an error occurs, returns a zero-length string array rather than throw an exception.

Parameters:
directory - the directory with the files and the file list
filelist - the text file with the list of file names
Returns:
the array with the list of file names

makePath

public static String makePath(String directory,
                              String filename)

Makes a file path from the directory name and file name adding a path separator character if necessary.

Throws RuntimeException if an error occurs.

Parameters:
directory - the directory string
filename - the file name string
Returns:
the path that combines the directory and file name