moj.util
Class FileTools

java.lang.Object
  extended by moj.util.FileTools

public class FileTools
extends java.lang.Object

A small utility class that contains methods for handling files.

Version:
2009-April-21
Author:
Martin Hassel

Constructor Summary
FileTools()
           
 
Method Summary
static java.lang.String readFile2String(java.lang.String filename)
          Reads a file into a String using cpdetector for code page detection.
static java.lang.String readFile2String(java.lang.String filename, java.nio.charset.Charset cs)
          Reads a file into a String using the provided charset.
static java.lang.String readFile2StringVanilla(java.lang.String filename)
          Reads a file into a String without specifying charset.
static java.util.HashSet<java.lang.String> readVocabulary(java.lang.String vocabularyfile)
          Reads a list of words from a file into a HashSet.
static java.util.HashSet<java.lang.String> readVocabulary(java.lang.String vocabularyfile, boolean lowercase)
          Reads a list of words from a file into a HashSet.
static void traverseDirectory(java.lang.String dirItem, TraverseDirectoryHelper helper)
          Recursively traverses a directory structure and executes the supplied helper for each file it encounters.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileTools

public FileTools()
Method Detail

readFile2String

public static java.lang.String readFile2String(java.lang.String filename)
Reads a file into a String using cpdetector for code page detection.

Parameters:
filename - name of the file to read into a String
Returns:
a String containing the given file (line breaks preserved)

readFile2String

public static java.lang.String readFile2String(java.lang.String filename,
                                               java.nio.charset.Charset cs)
Reads a file into a String using the provided charset.

Parameters:
filename - name of the file to read into a String
cs - the Charset to use when reading the file
Returns:
a String containing the given file (line breaks preserved)

readFile2StringVanilla

public static java.lang.String readFile2StringVanilla(java.lang.String filename)
Reads a file into a String without specifying charset.

Parameters:
filename - name of the file to read into a String
Returns:
a String containing the given file (line breaks preserved)

traverseDirectory

public static void traverseDirectory(java.lang.String dirItem,
                                     TraverseDirectoryHelper helper)
Recursively traverses a directory structure and executes the supplied helper for each file it encounters.

Parameters:
dirItem - directory to traverse or file to process
helper - an assisting class that processes a file passed to it

readVocabulary

public static java.util.HashSet<java.lang.String> readVocabulary(java.lang.String vocabularyfile)
Reads a list of words from a file into a HashSet. All incoming words are transformed to lower case.

Parameters:
vocabularyfile - filename of the file containing the list of words
Returns:
a HashSet containing the words

readVocabulary

public static java.util.HashSet<java.lang.String> readVocabulary(java.lang.String vocabularyfile,
                                                                 boolean lowercase)
Reads a list of words from a file into a HashSet. All incoming words are optionally transformed to lower case.

Parameters:
vocabularyfile - filename of the file containing the list of words
lowercase - true if all words should be converted to lower case, false original casing should be preserved
Returns:
a HashSet containing the words