comirva.util
Class FileUtils

java.lang.Object
  extended by comirva.util.FileUtils

public class FileUtils
extends Object

This class implements simple utilities related to file processing.


Nested Class Summary
static class FileUtils.TextFileIterator
          Iterates over the lines of a text file without storing the whole contents of the text file in RAM (only one line at a time is kept in RAM, except for object reference / garbage collection issues)
 
Constructor Summary
FileUtils()
           
 
Method Summary
static File[] getAllFilesRecursively(File[] files, FileFilter filter)
          Creates and returns a File[] containing all files in all subdirectories of the files given as argument files.
static String getFileContent(File textFile)
          Fetch the entire content of a text file and return it in a String.
static List<String> getFilesInDir(String dirname, String filter, boolean isRecursive)
          Get the names of the files in the specified directory.
static String insertContent(String page, String anchor, String content)
           
static Object load(String filename)
          Reads the serialized object from the file.
static boolean mkdir(File dir)
           
static float[] readFloatFile(File f, int no_floats)
          Reads up to no_floats floats from a file f.
static ArrayList<String> readTextFile(File textfile)
          Read the specified text file.
static String removeUnwantedChars(String str)
          Removes all non-digit and non-letter characters from a string and returns it in lower case.
static boolean save(Serializable object, String filename)
          Serializes the given object to the given filename.
static void setFileContent(File textFile, String content)
          Change the contents of text file, overwriting any existing text.
static void writeToFile(String outfilename, Object[] data)
          Writes a text file by calling for each object in data[] the toString() method, followed by a newline character.
static
<K,V> void
writeToTextFile(String outfilename, HashMap<K,V> hm)
          Writes all key-value pairs to a text file, each "line" taking the form "k:v", where k is key.toString() and v is value.toString().
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileUtils

public FileUtils()
Method Detail

getFileContent

public static String getFileContent(File textFile)
Fetch the entire content of a text file and return it in a String.

Parameters:
textFile - is a file which already exists and can be read

setFileContent

public static void setFileContent(File textFile,
                                  String content)
Change the contents of text file, overwriting any existing text.

Parameters:
textFile - is an existing file which can be written to
content - a String representing the content that is to be written to the file

insertContent

public static String insertContent(String page,
                                   String anchor,
                                   String content)
Parameters:
page - the web page template
anchor - the comment used in the template (which is replaced)
content - the content to be inserted instead of the anchor
Returns:
the modified web page content

removeUnwantedChars

public static String removeUnwantedChars(String str)
Removes all non-digit and non-letter characters from a string and returns it in lower case.

Parameters:
str - the string to be processed
Returns:
the cleaned string

writeToTextFile

public static <K,V> void writeToTextFile(String outfilename,
                                         HashMap<K,V> hm)
Writes all key-value pairs to a text file, each "line" taking the form "k:v", where k is key.toString() and v is value.toString().

Type Parameters:
K -
V -
Parameters:
outfilename -
hm -

writeToFile

public static void writeToFile(String outfilename,
                               Object[] data)
Writes a text file by calling for each object in data[] the toString() method, followed by a newline character. TODO we could consider renaming this function to writeToTextFile (overloading this function)

Parameters:
outfilename -
data -

readTextFile

public static ArrayList<String> readTextFile(File textfile)
Read the specified text file.

Parameters:
textfile -
Returns:
Each line of the text file (as determined by BufferedReader.readLine()) is one String.

readFloatFile

public static float[] readFloatFile(File f,
                                    int no_floats)
Reads up to no_floats floats from a file f. If more lines than no_floats are contained in the file, an array index out of bounds exception is thrown. If less lines are contained, the last idcs of the returned array are zero.

Returns:
a float[], each entry parsed from one line of the file.

mkdir

public static boolean mkdir(File dir)

save

public static boolean save(Serializable object,
                           String filename)
Serializes the given object to the given filename.

Parameters:
object -
filename -
Returns:
true on success, false otherwise

load

public static Object load(String filename)
Reads the serialized object from the file.

Parameters:
filename - the name of the file containing the serialized object data.
Returns:

getFilesInDir

public static List<String> getFilesInDir(String dirname,
                                         String filter,
                                         boolean isRecursive)
Get the names of the files in the specified directory.

Parameters:
dirname - the name of the directory
filter - a regular expression, e.g., ".*?\\.txt"
isRecursive - if true, then this function also reports files in sub-directories below the specified directory, otherwise only files in the specified directory.
Returns:

getAllFilesRecursively

public static File[] getAllFilesRecursively(File[] files,
                                            FileFilter filter)
Creates and returns a File[] containing all files in all subdirectories of the files given as argument files. Furthermore, a file filter can be specified.

Parameters:
files - the files and directories which are to be searched
filter - a FileFilter to be applied to the search
Returns:
a File[] containing all files in all subdirectories of files