edu.stanford.nlp.io
Class IOUtils

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

public class IOUtils
extends java.lang.Object

Helper Class for storing serialized objects to disk.

Author:
Kayur Patel, Teg Grenager

Method Summary
static void closeIgnoringExceptions(java.io.Closeable c)
          Provides an implementation of closing a file for use in a finally block so you can correctly close a file without even more exception handling stuff.
static java.io.InputStream getInputStreamFromURLOrClasspathOrFileSystem(java.lang.String textFileOrUrl)
          Locates this file either using the given URL, or in the CLASSPATH, or in the file system The CLASSPATH takes priority over the file system! This stream is buffered and gzipped (if necessary)
static java.lang.Iterable<java.io.File> iterFilesRecursive(java.io.File dir)
          Iterate over all the files in the directory, recursively.
static java.lang.Iterable<java.io.File> iterFilesRecursive(java.io.File dir, java.util.regex.Pattern pattern)
          Iterate over all the files in the directory, recursively.
static java.lang.Iterable<java.io.File> iterFilesRecursive(java.io.File dir, java.lang.String ext)
          Iterate over all the files in the directory, recursively.
static int lineCount(java.io.File textFile)
           
static java.io.InputStream openFile(java.io.File file)
          Quietly opens a File.
static java.lang.Iterable<java.lang.String> readLines(java.io.File file)
          Returns an Iterable of the lines in the file.
static java.lang.Iterable<java.lang.String> readLines(java.io.File file, java.lang.Class<? extends java.io.InputStream> fileInputStreamWrapper)
          Returns an Iterable of the lines in the file, wrapping the generated FileInputStream with an instance of the supplied class.
static java.lang.Iterable<java.lang.String> readLines(java.lang.String path)
          Returns an Iterable of the lines in the file.
static
<T> T
readObjectFromFile(java.io.File file)
          Read an object from a stored file.
static
<T> T
readObjectFromFile(java.lang.String filename)
          Read an object from a stored file.
static
<T> T
readObjectFromFileNoExceptions(java.io.File file)
          Read an object from a stored file without throwing exceptions.
static java.io.BufferedReader readReaderFromString(java.lang.String textFileOrUrl)
           
static java.io.BufferedReader readReaderFromString(java.lang.String textFileOrUrl, java.lang.String encoding)
          Open a BufferedReader to a file or URL specified by a String name.
static java.io.ObjectInputStream readStreamFromString(java.lang.String filenameOrUrl)
           
static java.lang.String slurpFile(java.io.File file)
          Returns all the text in the given File.
static java.lang.String slurpFile(java.lang.String filename)
          Returns all the text in the given file
static java.lang.String slurpFile(java.lang.String filename, java.lang.String encoding)
          Returns all the text in the given file with the given encoding.
static java.lang.String slurpFileNoExceptions(java.io.File file)
          Returns all the text in the given File.
static java.lang.String slurpFileNoExceptions(java.lang.String filename)
          Returns all the text in the given File.
static java.lang.String slurpFileNoExceptions(java.lang.String filename, java.lang.String encoding)
          Returns all the text in the given file with the given encoding.
static java.lang.String slurpGBFile(java.lang.String filename)
           
static java.lang.String slurpGBFileNoExceptions(java.lang.String filename)
           
static java.lang.String slurpGBURL(java.net.URL u)
          Returns all the text at the given URL.
static java.lang.String slurpGBURLNoExceptions(java.net.URL u)
          Returns all the text at the given URL.
static java.lang.String slurpGZippedFile(java.io.File file)
          Returns all the text in the given File.
static java.lang.String slurpGZippedFile(java.lang.String filename)
          Returns all the text in the given File.
static java.lang.String slurpReader(java.io.Reader reader)
          Returns all the text from the given Reader.
static java.lang.String slurpURL(java.lang.String path)
          Returns all the text at the given URL.
static java.lang.String slurpURL(java.net.URL u)
          Returns all the text at the given URL.
static java.lang.String slurpURL(java.net.URL u, java.lang.String encoding)
          Returns all the text at the given URL.
static java.lang.String slurpURLNoExceptions(java.lang.String path)
          Returns all the text at the given URL.
static java.lang.String slurpURLNoExceptions(java.net.URL u)
          Returns all the text at the given URL.
static java.lang.String slurpURLNoExceptions(java.net.URL u, java.lang.String encoding)
          Returns all the text at the given URL.
static java.io.File writeObjectToFile(java.lang.Object o, java.io.File file)
          Write an object to a specified File.
static java.io.File writeObjectToFile(java.lang.Object o, java.lang.String filename)
          Write object to a file with the specified name.
static java.io.File writeObjectToFileNoExceptions(java.lang.Object o, java.lang.String filename)
          Write object to a file with the specified name.
static java.io.File writeObjectToTempFile(java.lang.Object o, java.lang.String filename)
          Write object to temp file which is destroyed when the program exits.
static java.io.File writeObjectToTempFileNoExceptions(java.lang.Object o, java.lang.String filename)
          Write object to a temp file and ignore exceptions.
static java.io.ObjectOutputStream writeStreamFromString(java.lang.String serializePath)
           
static void writeStreamToStream(java.io.InputStream input, java.io.OutputStream output)
          Send all bytes from the input stream to the output stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

writeObjectToFile

public static java.io.File writeObjectToFile(java.lang.Object o,
                                             java.lang.String filename)
                                      throws java.io.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:
java.io.IOException - If can't write file.

writeObjectToFile

public static java.io.File writeObjectToFile(java.lang.Object o,
                                             java.io.File file)
                                      throws java.io.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:
java.io.IOException - If File cannot be written

writeObjectToFileNoExceptions

public static java.io.File writeObjectToFileNoExceptions(java.lang.Object o,
                                                         java.lang.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 java.io.File writeObjectToTempFile(java.lang.Object o,
                                                 java.lang.String filename)
                                          throws java.io.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:
java.io.IOException - If file cannot be written

writeObjectToTempFileNoExceptions

public static java.io.File writeObjectToTempFileNoExceptions(java.lang.Object o,
                                                             java.lang.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(java.io.File file)
                            throws java.io.IOException,
                                   java.lang.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:
java.io.IOException - If file cannot be read
java.lang.ClassNotFoundException - If reading serialized object fails

readObjectFromFile

public static <T> T readObjectFromFile(java.lang.String filename)
                            throws java.io.IOException,
                                   java.lang.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:
java.io.IOException - If file cannot be read
java.lang.ClassNotFoundException - If reading serialized object fails

readObjectFromFileNoExceptions

public static <T> T readObjectFromFileNoExceptions(java.io.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(java.io.File textFile)
                     throws java.io.IOException
Throws:
java.io.IOException

writeStreamFromString

public static java.io.ObjectOutputStream writeStreamFromString(java.lang.String serializePath)
                                                        throws java.io.IOException
Throws:
java.io.IOException

readStreamFromString

public static java.io.ObjectInputStream readStreamFromString(java.lang.String filenameOrUrl)
                                                      throws java.io.IOException
Throws:
java.io.IOException

getInputStreamFromURLOrClasspathOrFileSystem

public static java.io.InputStream getInputStreamFromURLOrClasspathOrFileSystem(java.lang.String textFileOrUrl)
                                                                        throws java.io.IOException
Locates this file either using the given URL, or in the CLASSPATH, or in the file system The CLASSPATH takes priority over the file system! This stream is buffered and gzipped (if necessary)

Parameters:
textFileOrUrl -
Returns:
An InputStream for loading a resource
Throws:
java.io.IOException

readReaderFromString

public static java.io.BufferedReader readReaderFromString(java.lang.String textFileOrUrl)
                                                   throws java.io.IOException
Throws:
java.io.IOException

readReaderFromString

public static java.io.BufferedReader readReaderFromString(java.lang.String textFileOrUrl,
                                                          java.lang.String encoding)
                                                   throws java.io.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:
java.io.IOException - If there is an I/O problem

readLines

public static java.lang.Iterable<java.lang.String> readLines(java.lang.String path)
Returns an Iterable of the lines in the file. The file reader will be closed when the iterator is exhausted. IO errors will throw an (unchecked) RuntimeIOException

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

readLines

public static java.lang.Iterable<java.lang.String> readLines(java.io.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.

readLines

public static java.lang.Iterable<java.lang.String> readLines(java.io.File file,
                                                             java.lang.Class<? extends java.io.InputStream> fileInputStreamWrapper)
Returns an Iterable of the lines in the file, wrapping the generated FileInputStream with an instance of the supplied class. IO errors will throw an (unchecked) RuntimeIOException

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.

openFile

public static java.io.InputStream openFile(java.io.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

closeIgnoringExceptions

public static void closeIgnoringExceptions(java.io.Closeable c)
Provides an implementation of closing a file for use in a finally block so you can correctly close a file without even more exception handling stuff. From a suggestion in a talk by Josh Bloch.

Parameters:
c - The IO resource to close (e.g., a Stream/Reader)

iterFilesRecursive

public static java.lang.Iterable<java.io.File> iterFilesRecursive(java.io.File dir)
Iterate over all the files in the directory, recursively.

Parameters:
dir - The root directory.
Returns:
All files within the directory.

iterFilesRecursive

public static java.lang.Iterable<java.io.File> iterFilesRecursive(java.io.File dir,
                                                                  java.lang.String ext)
Iterate over all the files in the directory, recursively.

Parameters:
dir - The root directory.
ext - A string that must be at the end of all files (e.g. ".txt")
Returns:
All files within the directory ending in the given extension.

iterFilesRecursive

public static java.lang.Iterable<java.io.File> iterFilesRecursive(java.io.File dir,
                                                                  java.util.regex.Pattern pattern)
Iterate over all the files in the directory, recursively.

Parameters:
dir - The root directory.
pattern - A regular expression that the file path must match. This uses Matcher.find(), so use ^ and $ to specify endpoints.
Returns:
All files within the directory.

slurpFile

public static java.lang.String slurpFile(java.io.File file)
                                  throws java.io.IOException
Returns all the text in the given File.

Throws:
java.io.IOException

slurpGZippedFile

public static java.lang.String slurpGZippedFile(java.lang.String filename)
                                         throws java.io.IOException
Returns all the text in the given File.

Throws:
java.io.IOException

slurpGZippedFile

public static java.lang.String slurpGZippedFile(java.io.File file)
                                         throws java.io.IOException
Returns all the text in the given File.

Throws:
java.io.IOException

slurpGBFileNoExceptions

public static java.lang.String slurpGBFileNoExceptions(java.lang.String filename)

slurpFile

public static java.lang.String slurpFile(java.lang.String filename,
                                         java.lang.String encoding)
                                  throws java.io.IOException
Returns all the text in the given file with the given encoding.

Throws:
java.io.IOException

slurpFileNoExceptions

public static java.lang.String slurpFileNoExceptions(java.lang.String filename,
                                                     java.lang.String encoding)
Returns all the text in the given file with the given encoding. If the file cannot be read (non-existent, etc.), then and only then the method returns null.


slurpGBFile

public static java.lang.String slurpGBFile(java.lang.String filename)
                                    throws java.io.IOException
Throws:
java.io.IOException

slurpFile

public static java.lang.String slurpFile(java.lang.String filename)
                                  throws java.io.IOException
Returns all the text in the given file

Returns:
The text in the file.
Throws:
java.io.IOException

slurpGBURL

public static java.lang.String slurpGBURL(java.net.URL u)
                                   throws java.io.IOException
Returns all the text at the given URL.

Throws:
java.io.IOException

slurpGBURLNoExceptions

public static java.lang.String slurpGBURLNoExceptions(java.net.URL u)
Returns all the text at the given URL.


slurpURLNoExceptions

public static java.lang.String slurpURLNoExceptions(java.net.URL u,
                                                    java.lang.String encoding)
Returns all the text at the given URL.


slurpURL

public static java.lang.String slurpURL(java.net.URL u,
                                        java.lang.String encoding)
                                 throws java.io.IOException
Returns all the text at the given URL.

Throws:
java.io.IOException

slurpURL

public static java.lang.String slurpURL(java.net.URL u)
                                 throws java.io.IOException
Returns all the text at the given URL.

Throws:
java.io.IOException

slurpURLNoExceptions

public static java.lang.String slurpURLNoExceptions(java.net.URL u)
Returns all the text at the given URL.


slurpURL

public static java.lang.String slurpURL(java.lang.String path)
                                 throws java.lang.Exception
Returns all the text at the given URL.

Throws:
java.lang.Exception

slurpURLNoExceptions

public static java.lang.String slurpURLNoExceptions(java.lang.String path)
Returns all the text at the given URL. If the file cannot be read (non-existent, etc.), then and only then the method returns null.


slurpFileNoExceptions

public static java.lang.String slurpFileNoExceptions(java.io.File file)
Returns all the text in the given File.

Returns:
The text in the file. May be an empty string if the file is empty. If the file cannot be read (non-existent, etc.), then and only then the method returns null.

slurpFileNoExceptions

public static java.lang.String slurpFileNoExceptions(java.lang.String filename)
Returns all the text in the given File.

Returns:
The text in the file. May be an empty string if the file is empty. If the file cannot be read (non-existent, etc.), then and only then the method returns null.

slurpReader

public static java.lang.String slurpReader(java.io.Reader reader)
Returns all the text from the given Reader.

Returns:
The text in the file.

writeStreamToStream

public static void writeStreamToStream(java.io.InputStream input,
                                       java.io.OutputStream output)
                                throws java.io.IOException
Send all bytes from the input stream to the output stream.

Parameters:
input - The input bytes.
output - Where the bytes should be written.
Throws:
java.io.IOException


Stanford NLP Group