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 various I/O related things.

Author:
Kayur Patel, Teg Grenager

Field Summary
static java.lang.String defaultEncoding
           
static java.lang.String eolChar
           
 
Method Summary
static java.io.File backupFile(java.io.File file)
           
static java.lang.String backupName(java.lang.String filename)
           
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.Reader encodedInputStreamReader(java.io.InputStream stream, java.lang.String encoding)
          Create a Reader with an explicit encoding around an InputStream.
static java.io.PrintWriter encodedOutputStreamPrintWriter(java.io.OutputStream stream, java.lang.String encoding, boolean autoFlush)
          Create a Reader with an explicit encoding around an InputStream.
static java.io.Writer encodedOutputStreamWriter(java.io.OutputStream stream, java.lang.String encoding)
          Create a Reader with an explicit encoding around an InputStream.
static java.io.File ensureDir(java.io.File tgtDir)
          Given a filepath, makes sure a directory exists there.
static java.io.BufferedReader getBufferedFileReader(java.lang.String filename)
           
static java.io.BufferedReader getBufferedFileReader(java.lang.String filename, java.lang.String encoding)
           
static java.io.BufferedReader getBufferedReaderFromClasspathOrFileSystem(java.lang.String filename)
           
static java.io.BufferedReader getBufferedReaderFromClasspathOrFileSystem(java.lang.String filename, java.lang.String encoding)
           
static java.io.InputStream getBZip2PipedInputStream(java.lang.String filename)
           
static java.io.OutputStream getBZip2PipedOutputStream(java.lang.String filename)
           
static java.lang.String getExtension(java.lang.String fileName)
           
static java.io.InputStream getFileInputStream(java.lang.String filename)
          Get a input file stream (automatically gunzip/bunzip2 depending on file extension)
static java.io.OutputStream getFileOutputStream(java.lang.String filename)
          Get a output file stream (automatically gzip/bzip2 depending on file extension)
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 gunzipped (if necessary).
static java.io.File getJNLPLocalScratch()
          A JavaNLP specific convenience routine for obtaining the current scratch directory for the machine you're currently running on.
static java.io.PrintWriter getPrintWriter(java.io.File textFile)
           
static java.io.PrintWriter getPrintWriter(java.lang.String filename)
           
static java.io.PrintWriter getPrintWriter(java.lang.String filename, java.lang.String encoding)
           
static java.io.PrintWriter getPrintWriterIgnoringExceptions(java.lang.String filename)
           
static java.io.PrintWriter getPrintWriterOrDie(java.lang.String filename)
           
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.util.List<java.lang.String> linesFromFile(java.lang.String filename)
          Returns the contents of a file as a list of strings.
static java.util.List<java.lang.String> linesFromFile(java.lang.String filename, java.lang.String encoding)
          Returns the contents of a file as a list of strings.
static void main(java.lang.String[] args)
           
static java.io.InputStream openFile(java.io.File file)
          Quietly opens a File.
static java.util.Set<java.lang.String> readColumnSet(java.lang.String infile, int field)
          Read column as set
static java.util.LinkedList<java.lang.String[]> readCSVStrictly(char[] csvContents, int numColumns)
          Read a CSV file character by character.
static java.util.LinkedList<java.lang.String[]> readCSVStrictly(java.lang.String filename, int numColumns)
           
static java.util.List<java.util.Map<java.lang.String,java.lang.String>> readCSVWithHeader(java.lang.String path)
           
static java.util.List<java.util.Map<java.lang.String,java.lang.String>> readCSVWithHeader(java.lang.String path, char quoteChar, char escapeChar)
          Read in a CSV formatted file with a header row
static java.io.BufferedReader readerFromString(java.lang.String textFileOrUrl)
          Open a BufferedReader to a file or URL specified by a String name.
static java.io.BufferedReader readerFromString(java.lang.String textFileOrUrl, java.lang.String encoding)
          Open a BufferedReader to a file or URL specified by a String name.
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.
static java.lang.Iterable<java.lang.String> readLines(java.io.File file, java.lang.Class<? extends java.io.InputStream> fileInputStreamWrapper, java.lang.String encoding)
          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 java.lang.Iterable<java.lang.String> readLines(java.lang.String path, java.lang.String encoding)
          Returns an Iterable of the lines in the file.
static java.util.Map<java.lang.String,java.lang.String> readMap(java.lang.String filename)
           
static
<C> java.util.List<C>
readObjectFromColumns(java.lang.Class objClass, java.lang.String filename, java.lang.String[] fieldNames, java.lang.String delimiter)
           
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
<T> T
readObjectFromObjectStream(java.io.ObjectInputStream ois)
           
static
<T> T
readObjectFromURLOrClasspathOrFileSystem(java.lang.String filename)
          Read an object from a stored file.
static java.io.ObjectInputStream readStreamFromString(java.lang.String filenameOrUrl)
           
static boolean renameToBackupName(java.io.File file)
           
static java.lang.String slurpFile(java.io.File file)
          Returns all the text in the given File.
static java.lang.String slurpFile(java.io.File file, java.lang.String encoding)
          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.lang.String stringFromFile(java.lang.String filename)
          Returns the contents of a file as a single string.
static java.lang.String stringFromFile(java.lang.String filename, java.lang.String encoding)
          Returns the contents of a file as a single string.
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.io.File file, boolean append)
          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.
static void writeStringToFile(java.lang.String contents, java.lang.String path, java.lang.String encoding)
          Writes a string to a file.
static void writeStringToFileNoExceptions(java.lang.String contents, java.lang.String path, java.lang.String encoding)
          Writes a string to a file, squashing exceptions
static void writeStringToTempFile(java.lang.String contents, java.lang.String path)
          Writes a string to a temporary file, as UTF-8
static java.io.File writeStringToTempFile(java.lang.String contents, java.lang.String path, java.lang.String encoding)
          Writes a string to a temporary file
static void writeStringToTempFileNoExceptions(java.lang.String contents, java.lang.String path)
          Writes a string to a temporary file with UTF-8 encoding, squashing exceptions
static java.io.File writeStringToTempFileNoExceptions(java.lang.String contents, java.lang.String path, java.lang.String encoding)
          Writes a string to a temporary file, squashing exceptions
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

eolChar

public static final java.lang.String eolChar

defaultEncoding

public static final java.lang.String defaultEncoding
See Also:
Constant Field Values
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

writeObjectToFile

public static java.io.File writeObjectToFile(java.lang.Object o,
                                             java.io.File file,
                                             boolean append)
                                      throws java.io.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:
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

writeStringToFile

public static void writeStringToFile(java.lang.String contents,
                                     java.lang.String path,
                                     java.lang.String encoding)
                              throws java.io.IOException
Writes a string to a file.

Parameters:
contents - The string to write
path - The file path
encoding - The encoding to encode in
Throws:
java.io.IOException - In case of failure

writeStringToFileNoExceptions

public static void writeStringToFileNoExceptions(java.lang.String contents,
                                                 java.lang.String path,
                                                 java.lang.String encoding)
Writes a string to a file, squashing exceptions

Parameters:
contents - The string to write
path - The file path
encoding - The encoding to encode in

writeStringToTempFile

public static java.io.File writeStringToTempFile(java.lang.String contents,
                                                 java.lang.String path,
                                                 java.lang.String encoding)
                                          throws java.io.IOException
Writes a string to a temporary file

Parameters:
contents - The string to write
path - The file path
encoding - The encoding to encode in
Returns:
The File written to
Throws:
java.io.IOException - In case of failure

writeStringToTempFile

public static void writeStringToTempFile(java.lang.String contents,
                                         java.lang.String path)
                                  throws java.io.IOException
Writes a string to a temporary file, as UTF-8

Parameters:
contents - The string to write
path - The file path
Throws:
java.io.IOException - In case of failure

writeStringToTempFileNoExceptions

public static java.io.File writeStringToTempFileNoExceptions(java.lang.String contents,
                                                             java.lang.String path,
                                                             java.lang.String encoding)
Writes a string to a temporary file, squashing exceptions

Parameters:
contents - The string to write
path - The file path
encoding - The encoding to encode in
Returns:
The File that was written to

writeStringToTempFileNoExceptions

public static void writeStringToTempFileNoExceptions(java.lang.String contents,
                                                     java.lang.String path)
Writes a string to a temporary file with UTF-8 encoding, squashing exceptions

Parameters:
contents - The string to write
path - The file path

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 retrieved
Returns:
The object read from the file.
Throws:
java.io.IOException - If file cannot be read
java.lang.ClassNotFoundException - If reading serialized object fails

readObjectFromURLOrClasspathOrFileSystem

public static <T> T readObjectFromURLOrClasspathOrFileSystem(java.lang.String filename)
                                                  throws java.io.IOException,
                                                         java.lang.ClassNotFoundException
Read an object from a stored file. The file can be anything obtained via a URL, the filesystem, or the classpath (eg in a jar file).

Parameters:
filename - The file pointing to the object to be retrieved
Returns:
The object read from the file.
Throws:
java.io.IOException - If file cannot be read
java.lang.ClassNotFoundException - If reading serialized object fails

readObjectFromObjectStream

public static <T> T readObjectFromObjectStream(java.io.ObjectInputStream ois)
                                    throws java.io.IOException,
                                           java.lang.ClassNotFoundException
Throws:
java.io.IOException
java.lang.ClassNotFoundException

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 retrieved
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 retrieved
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 gunzipped (if necessary).

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

readerFromString

public static java.io.BufferedReader readerFromString(java.lang.String textFileOrUrl)
                                               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 first tried as a URL, otherwise it is next tried as a resource on the CLASSPATH, and then finally it is tried as a local file or other network-available file . If the String ends in .gz, it is interpreted as a gzipped file (and uncompressed). The file is then interpreted as a utf-8 text file.

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

readerFromString

public static java.io.BufferedReader readerFromString(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 first tried as a URL, otherwise it is next tried as a resource on the CLASSPATH, and then finally it is tried as a local file or other network-available 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. Maybe be null, in which case the platform default encoding is used
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.lang.String path,
                                                             java.lang.String encoding)
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.
encoding - The encoding to use when reading lines.
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. The file reader will be closed when the iterator is exhausted.

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.

readLines

public static java.lang.Iterable<java.lang.String> readLines(java.io.File file,
                                                             java.lang.Class<? extends java.io.InputStream> fileInputStreamWrapper,
                                                             java.lang.String encoding)
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.
encoding - The encoding to use when reading lines.
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

slurpFile

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

Parameters:
file - The file to read from
encoding - The character encoding to assume. This may be null, and the platform default character encoding is used.
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. Closes the Reader when done.

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

readCSVWithHeader

public static java.util.List<java.util.Map<java.lang.String,java.lang.String>> readCSVWithHeader(java.lang.String path,
                                                                                                 char quoteChar,
                                                                                                 char escapeChar)
                                                                                          throws java.io.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:
java.io.IOException

readCSVWithHeader

public static java.util.List<java.util.Map<java.lang.String,java.lang.String>> readCSVWithHeader(java.lang.String path)
                                                                                          throws java.io.IOException
Throws:
java.io.IOException

readCSVStrictly

public static java.util.LinkedList<java.lang.String[]> readCSVStrictly(char[] csvContents,
                                                                       int numColumns)
Read a CSV file character by character. Allows for multi-line CSV files (in quotes), but is less flexible and likely slower than readCSVWithHeader()

Parameters:
csvContents - The char[] array corresponding to the contents of the file
numColumns - The number of columns in the file (for verification, primarily)
Returns:
A list of lines in the file

readCSVStrictly

public static java.util.LinkedList<java.lang.String[]> readCSVStrictly(java.lang.String filename,
                                                                       int numColumns)
                                                                throws java.io.IOException
Throws:
java.io.IOException

getFileInputStream

public static java.io.InputStream getFileInputStream(java.lang.String filename)
                                              throws java.io.IOException
Get a input file stream (automatically gunzip/bunzip2 depending on file extension)

Parameters:
filename - Name of file to open
Returns:
Input stream that can be used to read from the file
Throws:
java.io.IOException - if there are exceptions opening the file

getFileOutputStream

public static java.io.OutputStream getFileOutputStream(java.lang.String filename)
                                                throws java.io.IOException
Get a output file stream (automatically gzip/bzip2 depending on file extension)

Parameters:
filename - Name of file to open
Returns:
Output stream that can be used to write to the file
Throws:
java.io.IOException - if there are exceptions opening the file

getBufferedFileReader

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

getBufferedFileReader

public static java.io.BufferedReader getBufferedFileReader(java.lang.String filename,
                                                           java.lang.String encoding)
                                                    throws java.io.IOException
Throws:
java.io.IOException

getBufferedReaderFromClasspathOrFileSystem

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

getBufferedReaderFromClasspathOrFileSystem

public static java.io.BufferedReader getBufferedReaderFromClasspathOrFileSystem(java.lang.String filename,
                                                                                java.lang.String encoding)
                                                                         throws java.io.IOException
Throws:
java.io.IOException

getPrintWriter

public static java.io.PrintWriter getPrintWriter(java.io.File textFile)
                                          throws java.io.IOException
Throws:
java.io.IOException

getPrintWriter

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

getPrintWriterIgnoringExceptions

public static java.io.PrintWriter getPrintWriterIgnoringExceptions(java.lang.String filename)

getPrintWriterOrDie

public static java.io.PrintWriter getPrintWriterOrDie(java.lang.String filename)

getPrintWriter

public static java.io.PrintWriter getPrintWriter(java.lang.String filename,
                                                 java.lang.String encoding)
                                          throws java.io.IOException
Throws:
java.io.IOException

getBZip2PipedInputStream

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

getBZip2PipedOutputStream

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

readColumnSet

public static java.util.Set<java.lang.String> readColumnSet(java.lang.String infile,
                                                            int field)
                                                     throws java.io.IOException
Read column as set

Parameters:
infile - - filename
field - index of field to read
Returns:
a set of the entries in column field
Throws:
java.io.IOException

readObjectFromColumns

public static <C> java.util.List<C> readObjectFromColumns(java.lang.Class objClass,
                                                          java.lang.String filename,
                                                          java.lang.String[] fieldNames,
                                                          java.lang.String delimiter)
                                               throws java.io.IOException,
                                                      java.lang.InstantiationException,
                                                      java.lang.IllegalAccessException,
                                                      java.lang.NoSuchFieldException,
                                                      java.lang.NoSuchMethodException,
                                                      java.lang.reflect.InvocationTargetException
Throws:
java.io.IOException
java.lang.InstantiationException
java.lang.IllegalAccessException
java.lang.NoSuchFieldException
java.lang.NoSuchMethodException
java.lang.reflect.InvocationTargetException

readMap

public static java.util.Map<java.lang.String,java.lang.String> readMap(java.lang.String filename)
                                                                throws java.io.IOException
Throws:
java.io.IOException

stringFromFile

public static java.lang.String stringFromFile(java.lang.String filename)
Returns the contents of a file as a single string. The string may be empty, if the file is empty. If there is an IOException, it is caught and null is returned.


stringFromFile

public static java.lang.String stringFromFile(java.lang.String filename,
                                              java.lang.String encoding)
Returns the contents of a file as a single string. The string may be empty, if the file is empty. If there is an IOException, it is caught and null is returned. Encoding can also be specified.


linesFromFile

public static java.util.List<java.lang.String> linesFromFile(java.lang.String filename)
Returns the contents of a file as a list of strings. The list may be empty, if the file is empty. If there is an IOException, it is caught and null is returned.


linesFromFile

public static java.util.List<java.lang.String> linesFromFile(java.lang.String filename,
                                                             java.lang.String encoding)
Returns the contents of a file as a list of strings. The list may be empty, if the file is empty. If there is an IOException, it is caught and null is returned. Encoding can also be specified


backupName

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

backupFile

public static java.io.File backupFile(java.io.File file)

renameToBackupName

public static boolean renameToBackupName(java.io.File file)

getJNLPLocalScratch

public static java.io.File getJNLPLocalScratch()
A JavaNLP specific convenience routine for obtaining the current scratch directory for the machine you're currently running on.


ensureDir

public static java.io.File ensureDir(java.io.File tgtDir)
                              throws java.io.IOException
Given a filepath, makes sure a directory exists there. If not, creates and returns it. Same as ENSURE-DIRECTORY in CL.

Parameters:
tgtDir - The directory that you wish to ensure exists
Throws:
java.io.IOException - If directory can't be created, is an existing file, or for other reasons

main

public static void main(java.lang.String[] args)

getExtension

public static java.lang.String getExtension(java.lang.String fileName)

encodedInputStreamReader

public static java.io.Reader encodedInputStreamReader(java.io.InputStream stream,
                                                      java.lang.String encoding)
                                               throws java.io.IOException
Create a Reader with an explicit encoding around an InputStream. This static method will treat null as meaning to use the platform default, unlike the Java library methods that disallow a null encoding.

Parameters:
stream - An InputStream
encoding - A charset encoding
Returns:
A Reader
Throws:
java.io.IOException - If any IO problem

encodedOutputStreamWriter

public static java.io.Writer encodedOutputStreamWriter(java.io.OutputStream stream,
                                                       java.lang.String encoding)
                                                throws java.io.IOException
Create a Reader with an explicit encoding around an InputStream. This static method will treat null as meaning to use the platform default, unlike the Java library methods that disallow a null encoding.

Parameters:
stream - An InputStream
encoding - A charset encoding
Returns:
A Reader
Throws:
java.io.IOException - If any IO problem

encodedOutputStreamPrintWriter

public static java.io.PrintWriter encodedOutputStreamPrintWriter(java.io.OutputStream stream,
                                                                 java.lang.String encoding,
                                                                 boolean autoFlush)
                                                          throws java.io.IOException
Create a Reader with an explicit encoding around an InputStream. This static method will treat null as meaning to use the platform default, unlike the Java library methods that disallow a null encoding.

Parameters:
stream - An InputStream
encoding - A charset encoding
autoFlush - Whether to make an autoflushing Writer
Returns:
A Reader
Throws:
java.io.IOException - If any IO problem


Stanford NLP Group