edu.stanford.nlp.io
Class IOUtils

java.lang.Object
  extended by edu.stanford.nlp.io.IOUtils

public class IOUtils
extends Object

Helper Class for storing serialized objects to disk.

Author:
Kayur Patel, Teg Grenager

Method Summary
static void close(InputStream stream)
          Quietly close a stream.
static int lineCount(File textFile)
           
static InputStream openFile(File file)
          Quietly opens a File.
static Iterable<String> readLines(File file)
          Returns an Iterable of the lines in the file.
static Iterable<String> readLines(File file, Class<? extends InputStream> fileInputStreamWrapper)
          Returns an Iterable of the lines in the file, wrapping the generated FileInputStream with an instance of the supplied class.
static Iterable<String> readLines(String path)
          Returns an Iterable of the lines in the file.
static
<T> T
readObjectFromFile(File file)
          Read an object from a stored file.
static
<T> T
readObjectFromFile(String filename)
          Read an object from a stored file.
static
<T> T
readObjectFromFileNoExceptions(File file)
          Read an object from a stored file without throwing exceptions.
static BufferedReader readReaderFromString(String textFileOrUrl)
           
static BufferedReader readReaderFromString(String textFileOrUrl, String encoding)
          Open a BufferedReader to a file or URL specified by a String name.
static ObjectInputStream readStreamFromString(String filenameOrUrl)
           
static File writeObjectToFile(Object o, File file)
          Write an object to a specified File.
static File writeObjectToFile(Object o, String filename)
          Write object to a file with the specified name.
static File writeObjectToFileNoExceptions(Object o, String filename)
          Write object to a file with the specified name.
static File writeObjectToTempFile(Object o, String filename)
          Write object to temp file which is destroyed when the program exits.
static File writeObjectToTempFileNoExceptions(Object o, String filename)
          Write object to a temp file and ignore exceptions.
static ObjectOutputStream writeStreamFromString(String serializePath)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

writeObjectToFile

public static File writeObjectToFile(Object o,
                                     String filename)
                              throws IOException
Write object to a file with the specified name.

Parameters:
o - object to be written to file
filename - name of the temp file
Returns:
File containing the object
Throws:
IOException - If can't write file.

writeObjectToFile

public static File writeObjectToFile(Object o,
                                     File file)
                              throws IOException
Write an object to a specified File.

Parameters:
o - object to be written to file
file - The temp File
Returns:
File containing the object
Throws:
IOException - If File cannot be written

writeObjectToFileNoExceptions

public static File writeObjectToFileNoExceptions(Object o,
                                                 String filename)
Write object to a file with the specified name.

Parameters:
o - object to be written to file
filename - name of the temp file
Returns:
File containing the object, or null if an exception was caught

writeObjectToTempFile

public static File writeObjectToTempFile(Object o,
                                         String filename)
                                  throws IOException
Write object to temp file which is destroyed when the program exits.

Parameters:
o - object to be written to file
filename - name of the temp file
Returns:
File containing the object
Throws:
IOException - If file cannot be written

writeObjectToTempFileNoExceptions

public static File writeObjectToTempFileNoExceptions(Object o,
                                                     String filename)
Write object to a temp file and ignore exceptions.

Parameters:
o - object to be written to file
filename - name of the temp file
Returns:
File containing the object

readObjectFromFile

public static <T> T readObjectFromFile(File file)
                            throws IOException,
                                   ClassNotFoundException
Read an object from a stored file.

Parameters:
file - the file pointing to the object to be retrived
Returns:
the object read from the file.
Throws:
IOException - If file cannot be read
ClassNotFoundException - If reading serialized object fails

readObjectFromFile

public static <T> T readObjectFromFile(String filename)
                            throws IOException,
                                   ClassNotFoundException
Read an object from a stored file.

Parameters:
filename - The filename of the object to be retrived
Returns:
The object read from the file.
Throws:
IOException - If file cannot be read
ClassNotFoundException - If reading serialized object fails

readObjectFromFileNoExceptions

public static <T> T readObjectFromFileNoExceptions(File file)
Read an object from a stored file without throwing exceptions.

Parameters:
file - the file pointing to the object to be retrived
Returns:
the object read from the file, or null if an exception occurred.

lineCount

public static int lineCount(File textFile)
                     throws IOException
Throws:
IOException

writeStreamFromString

public static ObjectOutputStream writeStreamFromString(String serializePath)
                                                throws IOException
Throws:
IOException

readStreamFromString

public static ObjectInputStream readStreamFromString(String filenameOrUrl)
                                              throws IOException
Throws:
IOException

readReaderFromString

public static BufferedReader readReaderFromString(String textFileOrUrl)
                                           throws IOException
Throws:
IOException

readReaderFromString

public static BufferedReader readReaderFromString(String textFileOrUrl,
                                                  String encoding)
                                           throws IOException
Open a BufferedReader to a file or URL specified by a String name. If the String starts with https?://, then it is interpreted as a URL, otherwise it is interpreted as a local file. If the String ends in .gz, it is interpreted as a gzipped file (and uncompressed), else it is interpreted as a regular text file in the given encoding.

Parameters:
textFileOrUrl - What to read from
encoding - CharSet encoding
Returns:
The BufferedReader
Throws:
IOException - If there is an I/O problem

readLines

public static Iterable<String> readLines(String path)
Returns an Iterable of the lines in the file. The file reader will be closed when the iterator is exhausted.

Parameters:
path - The file whose lines are to be read.
Returns:
An Iterable containing the lines from the file.
Throws:
IOException

readLines

public static Iterable<String> readLines(File file)
Returns an Iterable of the lines in the file. The file reader will be closed when the iterator is exhausted.

Parameters:
file - The file whose lines are to be read.
Returns:
An Iterable containing the lines from the file.
Throws:
IOException

readLines

public static Iterable<String> readLines(File file,
                                         Class<? extends InputStream> fileInputStreamWrapper)
Returns an Iterable of the lines in the file, wrapping the generated FileInputStream with an instance of the supplied class.

Parameters:
file - The file whose lines are to be read.
fileInputStreamWrapper - The class to wrap the InputStream with, e.g. GZIPInputStream. Note that the class must have a constructor that accepts an InputStream.
Returns:
An Iterable containing the lines from the file.

close

public static void close(InputStream stream)
                  throws RuntimeIOException
Quietly close a stream.

Parameters:
stream - The stream to close
Throws:
RuntimeIOException - If there is an IO problem. This is an unchecked exception

openFile

public static InputStream openFile(File file)
                            throws RuntimeIOException
Quietly opens a File. If the file ends with a ".gz" extension, automatically opens a GZIPInputStream to wrap the constructed FileInputStream.

Throws:
RuntimeIOException


Stanford NLP Group