csli.util
Class FileUtils

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

public class FileUtils
extends Object

Author:
niekrasz, jefe Utilities for file IO.

Nested Class Summary
static interface FileUtils.LineReader
           
static interface FileUtils.ObjectReader<E>
           
static interface FileUtils.Writer<E>
           
 
Constructor Summary
FileUtils()
           
 
Method Summary
static void copy(File src, File dst)
           
static long countFileLines(String filename)
           
static Collection<String> getFileLines(File file, Collection<String> lines)
          Fill the Collection with the lines in file.
static Collection<String> getFileLines(String filename, Collection<String> lines)
          Fill the Collection with the lines in filename.
static String readFile(String filename)
          Read a file's contents into a string.
static void readLinesFromFile(String filename, FileUtils.LineReader reader)
          Open a file and read one line at a time, processing it using something derived from a LineReader.
static
<E> List<E>
readObjectsFromFile(String filename, FileUtils.ObjectReader<E> reader)
           
static
<E> void
writeObjectsToFile(String filename, Iterable<E> objects)
           
static
<E> void
writeObjectsToFile(String filename, Iterable<E> objects, boolean lineBreak)
           
static
<E> void
writeObjectsToFile(String filename, Iterable<E> objects, FileUtils.Writer<E> writer)
           
static
<E> void
writeObjectsToFile(String filename, Iterable<E> objects, FileUtils.Writer<E> writer, boolean lineBreak)
          Iterates over objects, writing each object successively to a file.
static void writeStringToFile(String filename, String string)
          Writes a string to a file.
 
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

copy

public static void copy(File src,
                        File dst)
                 throws IOException
Throws:
IOException

countFileLines

public static long countFileLines(String filename)
                           throws IOException
Throws:
IOException

getFileLines

public static Collection<String> getFileLines(String filename,
                                              Collection<String> lines)
                                       throws IOException
Fill the Collection with the lines in filename. If no collection passed in create a new arraylist and return it

Parameters:
filename -
lines -
Returns:
lines
Throws:
IOException

getFileLines

public static Collection<String> getFileLines(File file,
                                              Collection<String> lines)
                                       throws IOException
Fill the Collection with the lines in file. If no collection passed in create a new arraylist and return it

Parameters:
file -
lines -
Returns:
lines
Throws:
IOException

readFile

public static String readFile(String filename)
                       throws IOException
Read a file's contents into a string. (copied from csli.agent.manager.DisplayFile)

Parameters:
filename - The filename to read from.
Returns:
The contents of filename.
Throws:
IOException - Thrown when an I/O error occurs.

readLinesFromFile

public static void readLinesFromFile(String filename,
                                     FileUtils.LineReader reader)
                              throws IOException
Open a file and read one line at a time, processing it using something derived from a LineReader.

Parameters:
filename -
reader -
Throws:
FileNotFoundException
IOException

readObjectsFromFile

public static <E> List<E> readObjectsFromFile(String filename,
                                              FileUtils.ObjectReader<E> reader)
                                   throws IOException
Throws:
IOException

writeStringToFile

public static void writeStringToFile(String filename,
                                     String string)
                              throws FileNotFoundException,
                                     IOException
Writes a string to a file.

Parameters:
filename -
string -
Throws:
FileNotFoundException
IOException

writeObjectsToFile

public static <E> void writeObjectsToFile(String filename,
                                          Iterable<E> objects,
                                          FileUtils.Writer<E> writer,
                                          boolean lineBreak)
                               throws IOException
Iterates over objects, writing each object successively to a file.

Type Parameters:
E -
Parameters:
filename -
objects -
writer: - a Writer object. if not supplied, uses the object's natural toString() method.
lineBreak: - true if a newline should be inserted after each object. defaults to true.
Throws:
IOException

writeObjectsToFile

public static <E> void writeObjectsToFile(String filename,
                                          Iterable<E> objects,
                                          boolean lineBreak)
                               throws IOException
Throws:
IOException

writeObjectsToFile

public static <E> void writeObjectsToFile(String filename,
                                          Iterable<E> objects)
                               throws IOException
Throws:
IOException

writeObjectsToFile

public static <E> void writeObjectsToFile(String filename,
                                          Iterable<E> objects,
                                          FileUtils.Writer<E> writer)
                               throws IOException
Throws:
IOException