## edu.stanford.nlp.io
Class IOUtils

    java.lang.Object
      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` `closeIgnoringExceptions(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 InputStream` `getInputStreamFromURLOrClasspathOrFileSystem(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 Iterable<File>` `iterFilesRecursive(File dir)` Iterate over all the files in the directory, recursively.
`static Iterable<File>` `iterFilesRecursive(File dir, Pattern pattern)` Iterate over all the files in the directory, recursively.
`static Iterable<File>` `iterFilesRecursive(File dir, String ext)` Iterate over all the files in the directory, recursively.
`static int` `lineCount(File textFile)`
`static InputStream` `openFile(File file)` Quietly opens a File.
`static List<Map<String,String>>` `readCSVWithHeader(String path)`
`static List<Map<String,String>>` `readCSVWithHeader(String path, char quoteChar, char escapeChar)` Read in a CSV formatted file with a header row
`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

`<T> T`

`

`readObjectFromObjectStream(ObjectInputStream ois)`

`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 String` `slurpFile(File file)` Returns all the text in the given File.
`static String` `slurpFile(String filename)` Returns all the text in the given file
`static String` `slurpFile(String filename, String encoding)` Returns all the text in the given file with the given encoding.
`static String` `slurpFileNoExceptions(File file)` Returns all the text in the given File.
`static String` `slurpFileNoExceptions(String filename)` Returns all the text in the given File.
`static String` `slurpFileNoExceptions(String filename, String encoding)` Returns all the text in the given file with the given encoding.
`static String` `slurpGBFile(String filename)`
`static String` `slurpGBFileNoExceptions(String filename)`
`static String` `slurpGBURL(URL u)` Returns all the text at the given URL.
`static String` `slurpGBURLNoExceptions(URL u)` Returns all the text at the given URL.
`static String` `slurpGZippedFile(File file)` Returns all the text in the given File.
`static String` `slurpGZippedFile(String filename)` Returns all the text in the given File.
`static String` `slurpReader(Reader reader)` Returns all the text from the given Reader.
`static String` `slurpURL(String path)` Returns all the text at the given URL.
`static String` `slurpURL(URL u)` Returns all the text at the given URL.
`static String` `slurpURL(URL u, String encoding)` Returns all the text at the given URL.
`static String` `slurpURLNoExceptions(String path)` Returns all the text at the given URL.
`static String` `slurpURLNoExceptions(URL u)` Returns all the text at the given URL.
`static String` `slurpURLNoExceptions(URL u, String encoding)` Returns all the text at the given URL.
`static File` `writeObjectToFile(Object o, File file)` Write an object to a specified File.
`static File` `writeObjectToFile(Object o, File file, boolean append)` 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)`
`static void` `writeStreamToStream(InputStream input, 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 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

### writeObjectToFile

    public static File writeObjectToFile(Object o,
                                         File file,
                                         boolean append)
                                  throws IOException

  Write an object to a specified File.

  Parameters:
    `o` - object to be written to file
    `file` - The temp File
    `append` - If true, append to this file instead of overwriting it
  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

### readObjectFromObjectStream

    public static <T> T readObjectFromObjectStream(ObjectInputStream ois)
                                        throws IOException,
                                               ClassNotFoundException

  Throws:
    `IOException`
    `ClassNotFoundException`

### 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 retrieved
  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 retrieved
  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`

### getInputStreamFromURLOrClasspathOrFileSystem

    public static InputStream getInputStreamFromURLOrClasspathOrFileSystem(String textFileOrUrl)
                                                                    throws 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:
    `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. 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 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.

### 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. 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 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`

### closeIgnoringExceptions

    public static void closeIgnoringExceptions(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 Iterable<File> iterFilesRecursive(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 Iterable<File> iterFilesRecursive(File dir,
                                                    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 Iterable<File> iterFilesRecursive(File dir,
                                                    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 String slurpFile(File file)
                            throws IOException

  Returns all the text in the given File.

  Throws:
    `IOException`

### slurpGZippedFile

    public static String slurpGZippedFile(String filename)
                                   throws IOException

  Returns all the text in the given File.

  Throws:
    `IOException`

### slurpGZippedFile

    public static String slurpGZippedFile(File file)
                                   throws IOException

  Returns all the text in the given File.

  Throws:
    `IOException`

### slurpGBFileNoExceptions

    public static String slurpGBFileNoExceptions(String filename)

### slurpFile

    public static String slurpFile(String filename,
                                   String encoding)
                            throws IOException

  Returns all the text in the given file with the given encoding.

  Throws:
    `IOException`

### slurpFileNoExceptions

    public static String slurpFileNoExceptions(String filename,
                                               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 String slurpGBFile(String filename)
                              throws IOException

  Throws:
    `IOException`

### slurpFile

    public static String slurpFile(String filename)
                            throws IOException

  Returns all the text in the given file

  Returns:
    The text in the file.
  Throws:
    `IOException`

### slurpGBURL

    public static String slurpGBURL(URL u)
                             throws IOException

  Returns all the text at the given URL.

  Throws:
    `IOException`

### slurpGBURLNoExceptions

    public static String slurpGBURLNoExceptions(URL u)

  Returns all the text at the given URL.

### slurpURLNoExceptions

    public static String slurpURLNoExceptions(URL u,
                                              String encoding)

  Returns all the text at the given URL.

### slurpURL

    public static String slurpURL(URL u,
                                  String encoding)
                           throws IOException

  Returns all the text at the given URL.

  Throws:
    `IOException`

### slurpURL

    public static String slurpURL(URL u)
                           throws IOException

  Returns all the text at the given URL.

  Throws:
    `IOException`

### slurpURLNoExceptions

    public static String slurpURLNoExceptions(URL u)

  Returns all the text at the given URL.

### slurpURL

    public static String slurpURL(String path)
                           throws Exception

  Returns all the text at the given URL.

  Throws:
    `Exception`

### slurpURLNoExceptions

    public static String slurpURLNoExceptions(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 String slurpFileNoExceptions(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 String slurpFileNoExceptions(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 String slurpReader(Reader reader)

  Returns all the text from the given Reader.

  Returns:
    The text in the file.

### writeStreamToStream

    public static void writeStreamToStream(InputStream input,
                                           OutputStream output)
                                    throws 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:
    `IOException`

### readCSVWithHeader

    public static List<Map<String,String>> readCSVWithHeader(String path,
                                                             char quoteChar,
                                                             char escapeChar)
                                                      throws IOException

  Read in a CSV formatted file with a header row

  Parameters:
    `path` - - path to CSV file
    `quoteChar` - - character for enclosing strings, defaults to "
    `escapeChar` - - character for escaping quotes appearing in quoted strings; defaults to " (i.e. "" is used for " inside quotes, consistent with Excel)
  Returns:
    a list of maps representing the rows of the csv. The maps' keys are the header strings and their values are the row contents
  Throws:
    `IOException`

### readCSVWithHeader

    public static List<Map<String,String>> readCSVWithHeader(String path)
                                                      throws IOException

  Throws:
    `IOException`

Stanford NLP Group