|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectedu.stanford.nlp.io.IOUtils
public class IOUtils
Helper Class for storing serialized objects to disk.
Field Summary | |
---|---|
static String |
eolChar
|
Method Summary | ||
---|---|---|
static File |
backupFile(File file)
|
|
static String |
backupName(String filename)
|
|
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 Reader |
encodedInputStreamReader(InputStream stream,
String encoding)
Create a Reader with an explicit encoding around an InputStream. |
|
static PrintWriter |
encodedOutputStreamPrintWriter(OutputStream stream,
String encoding,
boolean autoFlush)
Create a Reader with an explicit encoding around an InputStream. |
|
static Writer |
encodedOutputStreamWriter(OutputStream stream,
String encoding)
Create a Reader with an explicit encoding around an InputStream. |
|
static File |
ensureDir(File tgtDir)
Given a filepath, makes sure a directory exists there. |
|
static BufferedReader |
getBufferedFileReader(String filename)
|
|
static BufferedReader |
getBufferedFileReader(String filename,
String encoding)
|
|
static InputStream |
getBZip2PipedInputStream(String filename)
|
|
static OutputStream |
getBZip2PipedOutputStream(String filename)
|
|
static String |
getExtension(String fileName)
|
|
static InputStream |
getFileInputStream(String filename)
Get a input file stream (automatically gunzip/bunzip2 depending on file extension) |
|
static OutputStream |
getFileOutputStream(String filename)
Get a output file stream (automatically gzip/bzip2 depending on file extension) |
|
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 File |
getJNLPLocalScratch()
A JavaNLP specific convenience routine for obtaining the current scratch directory for the machine you're currently running on. |
|
static PrintWriter |
getPrintWriter(File textFile)
|
|
static PrintWriter |
getPrintWriter(String filename)
|
|
static PrintWriter |
getPrintWriter(String filename,
String encoding)
|
|
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 List<String> |
linesFromFile(String filename)
Returns the contents of a file as a list of strings. |
|
static List<String> |
linesFromFile(String filename,
String encoding)
Returns the contents of a file as a list of strings. |
|
static void |
main(String[] args)
|
|
static InputStream |
openFile(File file)
Quietly opens a File. |
|
static Set<String> |
readColumnSet(String infile,
int field)
Read column as set |
|
static LinkedList<String[]> |
readCSVStrictly(char[] csvContents,
int numColumns)
Read a CSV file character by character. |
|
static LinkedList<String[]> |
readCSVStrictly(String filename,
int numColumns)
|
|
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 Map<String,String> |
readMap(String filename)
|
|
static
|
readObjectFromColumns(Class objClass,
String filename,
String[] fieldNames,
String delimiter)
|
|
static
|
readObjectFromFile(File file)
Read an object from a stored file. |
|
static
|
readObjectFromFile(String filename)
Read an object from a stored file. |
|
static
|
readObjectFromFileNoExceptions(File file)
Read an object from a stored file without throwing exceptions. |
|
static
|
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 boolean |
renameToBackupName(File file)
|
|
static String |
slurpFile(File file)
Returns all the text in the given File. |
|
static String |
slurpFile(File file,
String encoding)
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 String |
stringFromFile(String filename)
Returns the contents of a file as a single string. |
|
static String |
stringFromFile(String filename,
String encoding)
Returns the contents of a file as a single string. |
|
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. |
|
static void |
writeStringToFile(String contents,
String path)
Writes a string to a file, as UTF-8 |
|
static void |
writeStringToFile(String contents,
String path,
String encoding)
Writes a string to a file |
|
static void |
writeStringToFileNoExceptions(String contents,
String path)
Writes a string to a file with UTF-8 encoding, squashing exceptions |
|
static void |
writeStringToFileNoExceptions(String contents,
String path,
String encoding)
Writes a string to a file, squashing exceptions |
|
static void |
writeStringToTempFile(String contents,
String path)
Writes a string to a temporary file, as UTF-8 |
|
static File |
writeStringToTempFile(String contents,
String path,
String encoding)
Writes a string to a temporary file |
|
static void |
writeStringToTempFileNoExceptions(String contents,
String path)
Writes a string to a temporary file with UTF-8 encoding, squashing exceptions |
|
static File |
writeStringToTempFileNoExceptions(String contents,
String path,
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 |
---|
public static final String eolChar
Method Detail |
---|
public static File writeObjectToFile(Object o, String filename) throws IOException
o
- object to be written to filefilename
- name of the temp file
IOException
- If can't write file.public static File writeObjectToFile(Object o, File file) throws IOException
o
- object to be written to filefile
- The temp File
IOException
- If File cannot be writtenpublic static File writeObjectToFile(Object o, File file, boolean append) throws IOException
o
- object to be written to filefile
- The temp Fileappend
- If true, append to this file instead of overwriting it
IOException
- If File cannot be writtenpublic static File writeObjectToFileNoExceptions(Object o, String filename)
o
- object to be written to filefilename
- name of the temp file
public static File writeObjectToTempFile(Object o, String filename) throws IOException
o
- object to be written to filefilename
- name of the temp file
IOException
- If file cannot be writtenpublic static File writeObjectToTempFileNoExceptions(Object o, String filename)
o
- object to be written to filefilename
- name of the temp file
public static void writeStringToFile(String contents, String path, String encoding) throws IOException
contents
- The string to writepath
- The file pathencoding
- The encoding to encode in
IOException
- In case of failurepublic static void writeStringToFile(String contents, String path) throws IOException
contents
- The string to writepath
- The file path
IOException
- In case of failurepublic static void writeStringToFileNoExceptions(String contents, String path, String encoding)
contents
- The string to writepath
- The file pathencoding
- The encoding to encode inpublic static void writeStringToFileNoExceptions(String contents, String path)
contents
- The string to writepath
- The file pathpublic static File writeStringToTempFile(String contents, String path, String encoding) throws IOException
contents
- The string to writepath
- The file pathencoding
- The encoding to encode in
IOException
- In case of failurepublic static void writeStringToTempFile(String contents, String path) throws IOException
contents
- The string to writepath
- The file path
IOException
- In case of failurepublic static File writeStringToTempFileNoExceptions(String contents, String path, String encoding)
contents
- The string to writepath
- The file pathencoding
- The encoding to encode inpublic static void writeStringToTempFileNoExceptions(String contents, String path)
contents
- The string to writepath
- The file pathpublic static <T> T readObjectFromFile(File file) throws IOException, ClassNotFoundException
file
- the file pointing to the object to be retrived
IOException
- If file cannot be read
ClassNotFoundException
- If reading serialized object failspublic static <T> T readObjectFromObjectStream(ObjectInputStream ois) throws IOException, ClassNotFoundException
IOException
ClassNotFoundException
public static <T> T readObjectFromFile(String filename) throws IOException, ClassNotFoundException
filename
- The filename of the object to be retrieved
IOException
- If file cannot be read
ClassNotFoundException
- If reading serialized object failspublic static <T> T readObjectFromFileNoExceptions(File file)
file
- the file pointing to the object to be retrieved
public static int lineCount(File textFile) throws IOException
IOException
public static ObjectOutputStream writeStreamFromString(String serializePath) throws IOException
IOException
public static ObjectInputStream readStreamFromString(String filenameOrUrl) throws IOException
IOException
public static InputStream getInputStreamFromURLOrClasspathOrFileSystem(String textFileOrUrl) throws IOException
textFileOrUrl
-
IOException
public static BufferedReader readReaderFromString(String textFileOrUrl) throws IOException
IOException
public static BufferedReader readReaderFromString(String textFileOrUrl, String encoding) throws IOException
textFileOrUrl
- What to read fromencoding
- CharSet encoding
IOException
- If there is an I/O problempublic static Iterable<String> readLines(String path)
path
- The file whose lines are to be read.
public static Iterable<String> readLines(File file)
file
- The file whose lines are to be read.
public static Iterable<String> readLines(File file, Class<? extends InputStream> fileInputStreamWrapper)
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.
public static InputStream openFile(File file) throws RuntimeIOException
RuntimeIOException
public static void closeIgnoringExceptions(Closeable c)
c
- The IO resource to close (e.g., a Stream/Reader)public static Iterable<File> iterFilesRecursive(File dir)
dir
- The root directory.
public static Iterable<File> iterFilesRecursive(File dir, String ext)
dir
- The root directory.ext
- A string that must be at the end of all files (e.g. ".txt")
public static Iterable<File> iterFilesRecursive(File dir, Pattern pattern)
dir
- The root directory.pattern
- A regular expression that the file path must match. This uses
Matcher.find(), so use ^ and $ to specify endpoints.
public static String slurpFile(File file) throws IOException
IOException
public static String slurpFile(File file, String encoding) throws IOException
file
- The file to read fromencoding
- The character encoding to assume. This may be null, and
the platform default character encoding is used.
IOException
public static String slurpGZippedFile(String filename) throws IOException
IOException
public static String slurpGZippedFile(File file) throws IOException
IOException
public static String slurpGBFileNoExceptions(String filename)
public static String slurpFile(String filename, String encoding) throws IOException
IOException
public static String slurpFileNoExceptions(String filename, String encoding)
null
.
public static String slurpGBFile(String filename) throws IOException
IOException
public static String slurpFile(String filename) throws IOException
IOException
public static String slurpGBURL(URL u) throws IOException
IOException
public static String slurpGBURLNoExceptions(URL u)
public static String slurpURLNoExceptions(URL u, String encoding)
public static String slurpURL(URL u, String encoding) throws IOException
IOException
public static String slurpURL(URL u) throws IOException
IOException
public static String slurpURLNoExceptions(URL u)
public static String slurpURL(String path) throws Exception
Exception
public static String slurpURLNoExceptions(String path)
null
.
public static String slurpFileNoExceptions(File file)
null
.public static String slurpFileNoExceptions(String filename)
null
.public static String slurpReader(Reader reader)
public static void writeStreamToStream(InputStream input, OutputStream output) throws IOException
input
- The input bytes.output
- Where the bytes should be written.
IOException
public static List<Map<String,String>> readCSVWithHeader(String path, char quoteChar, char escapeChar) throws IOException
path
- - path to CSV filequoteChar
- - 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)
IOException
public static List<Map<String,String>> readCSVWithHeader(String path) throws IOException
IOException
public static LinkedList<String[]> readCSVStrictly(char[] csvContents, int numColumns)
csvContents
- The char[] array corresponding to the contents of the filenumColumns
- The number of columns in the file (for verification, primarily)
public static LinkedList<String[]> readCSVStrictly(String filename, int numColumns) throws IOException
IOException
public static InputStream getFileInputStream(String filename) throws IOException
filename
- Name of file to open
IOException
- if there are exceptions opening the filepublic static OutputStream getFileOutputStream(String filename) throws IOException
filename
- Name of file to open
IOException
- if there are exceptions opening the filepublic static BufferedReader getBufferedFileReader(String filename) throws IOException
IOException
public static BufferedReader getBufferedFileReader(String filename, String encoding) throws IOException
IOException
public static PrintWriter getPrintWriter(File textFile) throws IOException
IOException
public static PrintWriter getPrintWriter(String filename) throws IOException
IOException
public static PrintWriter getPrintWriter(String filename, String encoding) throws IOException
IOException
public static InputStream getBZip2PipedInputStream(String filename) throws IOException
IOException
public static OutputStream getBZip2PipedOutputStream(String filename) throws IOException
IOException
public static Set<String> readColumnSet(String infile, int field) throws IOException
infile
- - filenamefield
- index of field to read
IOException
public static <C> List<C> readObjectFromColumns(Class objClass, String filename, String[] fieldNames, String delimiter) throws IOException, InstantiationException, IllegalAccessException, NoSuchFieldException, NoSuchMethodException, InvocationTargetException
IOException
InstantiationException
IllegalAccessException
NoSuchFieldException
NoSuchMethodException
InvocationTargetException
public static Map<String,String> readMap(String filename) throws IOException
IOException
public static String stringFromFile(String filename)
public static String stringFromFile(String filename, String encoding)
public static List<String> linesFromFile(String filename)
public static List<String> linesFromFile(String filename, String encoding)
public static String backupName(String filename)
public static File backupFile(File file)
public static boolean renameToBackupName(File file)
public static File getJNLPLocalScratch()
public static File ensureDir(File tgtDir) throws Exception
Exception
public static void main(String[] args)
public static String getExtension(String fileName)
public static Reader encodedInputStreamReader(InputStream stream, String encoding) throws IOException
stream
- An InputStreamencoding
- A charset encoding
IOException
- If any IO problempublic static Writer encodedOutputStreamWriter(OutputStream stream, String encoding) throws IOException
stream
- An InputStreamencoding
- A charset encoding
IOException
- If any IO problempublic static PrintWriter encodedOutputStreamPrintWriter(OutputStream stream, String encoding, boolean autoFlush) throws IOException
stream
- An InputStreamencoding
- A charset encoding
IOException
- If any IO problem
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |