iglu.util
Class IOTools

java.lang.Object
  |
  +--iglu.util.IOTools

public class IOTools
extends java.lang.Object

This class provides some helper procedures to make IO a bit simpler.

Version:
1.0
Author:
Ryan Scherle rscherle@acm.org

Field Summary
static java.lang.String GENERIC_WORD_DELIMITERS
           
private static java.io.BufferedReader standardInput
           
static java.lang.String WHITE_SPACE_CHARACTERS
           
 
Constructor Summary
private IOTools()
          Prevent instantiation.
 
Method Summary
static java.lang.String changeExtension(java.lang.String filename, java.lang.String extension)
          Changes the extension of a filename.
static boolean fileExists(java.lang.String name)
          Check to see if the specified file exists
static java.io.BufferedReader getFileReader(java.io.File theFile)
          Returns a BufferedReader that can read the contents of a File.
static java.io.BufferedReader getFileReader(java.lang.String filename)
          Returns a BufferedReader that can read the contents of the file filename.
static java.io.PrintWriter getFileWriter(java.io.File theFile)
          Returns a PrintWriter that can write to the given file.
static java.io.PrintWriter getFileWriter(java.lang.String filename)
          Returns a PrintWriter that can write to the given file.
static java.io.File getIndependentFile(java.lang.String filename)
          Creates a File that will work on any operating system, assuming the directory structures are the same.
static java.io.BufferedReader getProcessReader(java.lang.String command)
          Returns a BufferedReader that can read the results of running the process command.
static java.io.BufferedReader getURLReader(java.lang.String url)
          Returns a BufferedReader that contains the contents of a URL.
static java.io.BufferedReader getWashingFileReader(java.io.File theFile)
          Returns a BufferedReader that can read the sanitized contents of the File.
static java.io.BufferedReader getWashingFileReader(java.lang.String filename)
          Returns a BufferedReader that can read the sanitized contents of the file filename.
static void main(java.lang.String[] args)
          Runs some test examples on the methods of this class.
static java.lang.Object objectFromBytes(byte[] bytes)
           
static java.io.Serializable objectFromFile(java.lang.String filename)
          A convienence class which reads serializable object from the disk.
static byte[] objectToBytes(java.io.Serializable o)
           
static boolean objectToFile(java.io.Serializable o, java.lang.String filename)
          A convienence class which writes serializable objects out to disk.
static java.lang.String readLine()
          Reads a line of text from stdin and returns it as a string.
static java.lang.String readLines(java.io.BufferedReader rdr, int numLines)
          Reads numLines lines from the reader, concatenates them into a String, and returns the String.
static java.lang.Object retrieveObject(java.io.File theFile)
          Retrieves an object from the specified file.
static void saveObject(java.lang.Object item, java.io.File theFile)
          Saves a single object to the specified file.
static java.lang.String slurp(java.io.BufferedReader rdr)
          Reads the entire contents of a reader and returns them as a String.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

GENERIC_WORD_DELIMITERS

public static final java.lang.String GENERIC_WORD_DELIMITERS

WHITE_SPACE_CHARACTERS

public static final java.lang.String WHITE_SPACE_CHARACTERS

standardInput

private static java.io.BufferedReader standardInput
Constructor Detail

IOTools

private IOTools()
Prevent instantiation.

Method Detail

readLine

public static java.lang.String readLine()
Reads a line of text from stdin and returns it as a string.


readLines

public static java.lang.String readLines(java.io.BufferedReader rdr,
                                         int numLines)
Reads numLines lines from the reader, concatenates them into a String, and returns the String. If the reader doesn't contain enough lines, all lines that exist are returned.


slurp

public static java.lang.String slurp(java.io.BufferedReader rdr)
Reads the entire contents of a reader and returns them as a String. The reader is also closed, since there is nothing more to read from it.


getIndependentFile

public static java.io.File getIndependentFile(java.lang.String filename)
Creates a File that will work on any operating system, assuming the directory structures are the same. This method should only be used for hardcoded filenames. Filenames input by the user should be left as-is, so they may be interpreted in an OS-dependent manner.

If the first character of the string is a forward slash or tilde, or the second character is a colon, then the string is assumed to be an absolute filename, and the file is created directly from the string. Otherwise, the user's home directory is used as a base, and the file is created relative to this base.


getFileWriter

public static java.io.PrintWriter getFileWriter(java.lang.String filename)
Returns a PrintWriter that can write to the given file.


getFileWriter

public static java.io.PrintWriter getFileWriter(java.io.File theFile)
Returns a PrintWriter that can write to the given file.


getFileReader

public static java.io.BufferedReader getFileReader(java.lang.String filename)
                                            throws java.io.FileNotFoundException
Returns a BufferedReader that can read the contents of the file filename.

java.io.FileNotFoundException

getFileReader

public static java.io.BufferedReader getFileReader(java.io.File theFile)
                                            throws java.io.FileNotFoundException
Returns a BufferedReader that can read the contents of a File.

java.io.FileNotFoundException

getWashingFileReader

public static java.io.BufferedReader getWashingFileReader(java.lang.String filename)
                                                   throws java.io.FileNotFoundException
Returns a BufferedReader that can read the sanitized contents of the file filename. For sanitizing, contents are passed through the WashingFilter, which throws away non-ASCII characters.

java.io.FileNotFoundException

getWashingFileReader

public static java.io.BufferedReader getWashingFileReader(java.io.File theFile)
                                                   throws java.io.FileNotFoundException
Returns a BufferedReader that can read the sanitized contents of the File. For sanitizing, contents are passed through the WashingFilter, which throws away non-ASCII characters.

java.io.FileNotFoundException

getProcessReader

public static java.io.BufferedReader getProcessReader(java.lang.String command)
                                               throws java.io.IOException
Returns a BufferedReader that can read the results of running the process command.

java.io.IOException

getURLReader

public static java.io.BufferedReader getURLReader(java.lang.String url)
                                           throws java.io.IOException
Returns a BufferedReader that contains the contents of a URL.

java.io.IOException

saveObject

public static void saveObject(java.lang.Object item,
                              java.io.File theFile)
                       throws java.io.IOException
Saves a single object to the specified file. The object must be Serializable. If the file exists, it is overwritten. If it doesn't exist, it is created.

java.io.IOException

retrieveObject

public static java.lang.Object retrieveObject(java.io.File theFile)
                                       throws java.io.IOException
Retrieves an object from the specified file. The object must be Serializable. Assumes that there is only one object (or object graph) in the file.

Returns:
The object if it is found and readable, null if it is not.
java.io.IOException

changeExtension

public static java.lang.String changeExtension(java.lang.String filename,
                                               java.lang.String extension)
Changes the extension of a filename.

Returns:
The filename with the new extension.

objectToBytes

public static byte[] objectToBytes(java.io.Serializable o)

objectFromBytes

public static java.lang.Object objectFromBytes(byte[] bytes)

objectToFile

public static boolean objectToFile(java.io.Serializable o,
                                   java.lang.String filename)
A convienence class which writes serializable objects out to disk. Deletes the file if it already exists.


fileExists

public static boolean fileExists(java.lang.String name)
Check to see if the specified file exists


objectFromFile

public static java.io.Serializable objectFromFile(java.lang.String filename)
A convienence class which reads serializable object from the disk. For use with files written by objectToFile


main

public static void main(java.lang.String[] args)
                 throws java.io.IOException
Runs some test examples on the methods of this class.

java.io.IOException