public class IOUtils extends Object
Modifier and Type | Field and Description |
---|---|
static String |
defaultEncoding |
static String |
eolChar |
Modifier and Type | Method and Description |
---|---|
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 void |
cp(File source,
File target) |
static void |
cp(File source,
File target,
boolean recursive)
An implementation of cp, as close to the Unix command as possible.
|
static boolean |
deleteDirRecursively(File dir)
Given a filepath, delete all files in the directory recursively
|
static void |
deleteRecursively(File file)
Delete this file; or, if it is a directory, delete this directory and all its contents.
|
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 boolean |
existsInClasspathOrFileSystem(String name)
Check if this path exists either in the classpath or on the filesystem.
|
static BufferedReader |
getBufferedFileReader(String filename) |
static BufferedReader |
getBufferedFileReader(String filename,
String encoding) |
static BufferedReader |
getBufferedReaderFromClasspathOrFileSystem(String filename) |
static BufferedReader |
getBufferedReaderFromClasspathOrFileSystem(String filename,
String encoding) |
static InputStream |
getBZip2PipedInputStream(String filename) |
static OutputStream |
getBZip2PipedOutputStream(String filename) |
static DataInputStream |
getDataInputStream(String filenameUrlOrClassPath) |
static DataOutputStream |
getDataOutputStream(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 gunzipped (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 Iterable<String> |
getLineIterable(Reader r,
boolean includeEol)
Given a reader, returns the lines from the reader as an Iterable.
|
static Iterable<String> |
getLineIterable(Reader r,
int bufferSize,
boolean includeEol) |
static PrintWriter |
getPrintWriter(File textFile) |
static PrintWriter |
getPrintWriter(File textFile,
String encoding) |
static PrintWriter |
getPrintWriter(String filename) |
static PrintWriter |
getPrintWriter(String filename,
String encoding) |
static PrintWriter |
getPrintWriterIgnoringExceptions(String filename) |
static PrintWriter |
getPrintWriterOrDie(String filename) |
static String |
getUrlEncoding(URLConnection connection) |
static InputStream |
inputStreamFromFile(File file)
Quietly opens a File.
|
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(String textFileOrUrl) |
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 List<String> |
linesFromFile(String filename,
String encoding,
boolean ignoreHeader) |
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 BufferedReader |
readerFromFile(File file)
Open a BufferedReader to a File.
|
static BufferedReader |
readerFromFile(File file,
String encoding)
Open a BufferedReader to a File.
|
static BufferedReader |
readerFromStdin()
Open a BufferedReader on stdin.
|
static BufferedReader |
readerFromStdin(String encoding)
Open a BufferedReader on stdin.
|
static BufferedReader |
readerFromString(String textFileOrUrl)
Open a BufferedReader to a file, class path entry or URL specified by a String name.
|
static BufferedReader |
readerFromString(String textFileOrUrl,
String encoding)
Open a BufferedReader to a file or URL specified by a String name.
|
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.
|
static Iterable<String> |
readLines(File file,
Class<? extends InputStream> fileInputStreamWrapper,
String encoding)
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 Iterable<String> |
readLines(String path,
String encoding)
Returns an Iterable of the lines in the file.
|
static Map<String,String> |
readMap(String filename) |
static <C> List<C> |
readObjectFromColumns(Class objClass,
String filename,
String[] fieldNames,
String delimiter) |
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 <T> T |
readObjectFromURLOrClasspathOrFileSystem(String filename)
Read an object from a stored file.
|
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 with the given
encoding.
|
static String |
slurpFileNoExceptions(String filename)
Returns all the text in the given file with the given
encoding.
|
static String |
slurpFileNoExceptions(String filename,
String encoding)
Returns all the text in the given file with the given
encoding.
|
static String |
slurpGBURL(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 String[] |
tail(File f,
int n) |
static String[] |
tail(File f,
int n,
String encoding)
A Java implementation of the Unix tail functionality.
|
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,
String encoding)
Writes a string to a file.
|
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
|
public static final String eolChar
public static final String defaultEncoding
public static File writeObjectToFile(Object o, String filename) throws IOException
o
- Object to be written to filefilename
- Name of the temp fileIOException
- If can't write file.public static File writeObjectToFile(Object o, File file) throws IOException
o
- Object to be written to filefile
- The temp FileIOException
- 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 itIOException
- If File cannot be writtenpublic static File writeObjectToFileNoExceptions(Object o, String filename)
o
- Object to be written to filefilename
- Name of the temp filepublic static File writeObjectToTempFile(Object o, String filename) throws IOException
o
- Object to be written to filefilename
- Name of the temp fileIOException
- If file cannot be writtenpublic static File writeObjectToTempFileNoExceptions(Object o, String filename)
o
- Object to be written to filefilename
- Name of the temp filepublic static void writeStringToFile(String contents, String path, String encoding) throws IOException
contents
- The string to writepath
- The file pathencoding
- The encoding to encode inIOException
- 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 File writeStringToTempFile(String contents, String path, String encoding) throws IOException
contents
- The string to writepath
- The file pathencoding
- The encoding to encode inIOException
- In case of failurepublic static void writeStringToTempFile(String contents, String path) throws IOException
contents
- The string to writepath
- The file pathIOException
- 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 retrievedIOException
- If file cannot be readClassNotFoundException
- If reading serialized object failspublic static DataInputStream getDataInputStream(String filenameUrlOrClassPath) throws IOException
IOException
public static DataOutputStream getDataOutputStream(String filename) throws IOException
IOException
public static <T> T readObjectFromURLOrClasspathOrFileSystem(String filename) throws IOException, ClassNotFoundException
filename
- The file pointing to the object to be retrievedIOException
- If file cannot be readClassNotFoundException
- 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 retrievedIOException
- If file cannot be readClassNotFoundException
- If reading serialized object failspublic static <T> T readObjectFromFileNoExceptions(File file)
file
- The file pointing to the object to be retrievedpublic static int lineCount(String textFileOrUrl) throws IOException
IOException
public static ObjectOutputStream writeStreamFromString(String serializePath) throws IOException
IOException
public static ObjectInputStream readStreamFromString(String filenameOrUrl) throws IOException
IOException
public static boolean existsInClasspathOrFileSystem(String name)
name
- The file or resource name.getBufferedReaderFromClasspathOrFileSystem(String)
would return a valid stream.public static InputStream getInputStreamFromURLOrClasspathOrFileSystem(String textFileOrUrl) throws IOException
textFileOrUrl
- IOException
public static InputStream inputStreamFromFile(File file) throws RuntimeIOException
RuntimeIOException
public static BufferedReader readerFromFile(File file)
file
- What to read fromRuntimeIOException
- If there is an I/O problempublic static BufferedReader readerFromFile(File file, String encoding)
file
- What to read fromencoding
- What charset to use. A null String is interpreted as platform default encodingRuntimeIOException
- If there is an I/O problempublic static BufferedReader readerFromStdin() throws IOException
IOException
- If there is an I/O problempublic static BufferedReader readerFromStdin(String encoding) throws IOException
encoding
- CharSet encoding. Maybe be null, in which case the
platform default encoding is usedIOException
- If there is an I/O problempublic static BufferedReader readerFromString(String textFileOrUrl) throws IOException
textFileOrUrl
- What to read fromIOException
- If there is an I/O problempublic static BufferedReader readerFromString(String textFileOrUrl, String encoding) throws IOException
textFileOrUrl
- What to read fromencoding
- CharSet encoding. Maybe be null, in which case the
platform default encoding is usedIOException
- 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(String path, String encoding)
path
- The file whose lines are to be read.encoding
- The encoding to use when reading lines.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 Iterable<String> readLines(File file, Class<? extends InputStream> fileInputStreamWrapper, String encoding)
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.public static Iterable<String> getLineIterable(Reader r, boolean includeEol)
r
- input readerincludeEol
- whether to keep eol-characters in the returned stringspublic static Iterable<String> getLineIterable(Reader r, int bufferSize, boolean includeEol)
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 slurpFile(String filename, String encoding) throws IOException
IOException
public static String slurpFileNoExceptions(String filename, String encoding)
public static String slurpFile(String filename) throws IOException
IOException
public static String slurpGBURL(URL u) throws IOException
IOException
public static String slurpURLNoExceptions(URL u, String encoding)
public static String slurpURL(URL u, String encoding) throws IOException
IOException
public static String getUrlEncoding(URLConnection connection)
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)
public static String slurpFileNoExceptions(String filename)
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 openIOException
- if there are exceptions opening the filepublic static OutputStream getFileOutputStream(String filename) throws IOException
filename
- Name of file to openIOException
- 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 BufferedReader getBufferedReaderFromClasspathOrFileSystem(String filename) throws IOException
IOException
public static BufferedReader getBufferedReaderFromClasspathOrFileSystem(String filename, String encoding) throws IOException
IOException
public static PrintWriter getPrintWriter(File textFile) throws IOException
IOException
public static PrintWriter getPrintWriter(File textFile, String encoding) throws IOException
IOException
public static PrintWriter getPrintWriter(String filename) throws IOException
IOException
public static PrintWriter getPrintWriterIgnoringExceptions(String filename)
public static PrintWriter getPrintWriterOrDie(String filename)
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 readIOException
public static <C> List<C> readObjectFromColumns(Class objClass, String filename, String[] fieldNames, String delimiter) throws 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 List<String> linesFromFile(String filename, String encoding, boolean ignoreHeader)
public static boolean renameToBackupName(File file)
public static File getJNLPLocalScratch()
public static File ensureDir(File tgtDir) throws IOException
tgtDir
- The directory that you wish to ensure existsIOException
- If directory can't be created, is an existing file, or for other reasonspublic static boolean deleteDirRecursively(File dir)
dir
- public static Reader encodedInputStreamReader(InputStream stream, String encoding) throws IOException
stream
- An InputStreamencoding
- A charset encodingIOException
- If any IO problempublic static Writer encodedOutputStreamWriter(OutputStream stream, String encoding) throws IOException
stream
- An InputStreamencoding
- A charset encodingIOException
- If any IO problempublic static PrintWriter encodedOutputStreamPrintWriter(OutputStream stream, String encoding, boolean autoFlush) throws IOException
stream
- An InputStreamencoding
- A charset encodingautoFlush
- Whether to make an autoflushing WriterIOException
- If any IO problempublic static void cp(File source, File target, boolean recursive) throws IOException
An implementation of cp, as close to the Unix command as possible. Both directories and files are valid for either the source or the target; if the target exists, the semantics of Unix cp are [intended to be] obeyed.
source
- The source file or directory.target
- The target to write this file or directory to.recursive
- If true, recursively copy directory contentsIOException
- If either the copy fails (standard IO Exception), or the command is invalid
(e.g., copying a directory without the recursive flag)public static void cp(File source, File target) throws IOException
IOException
cp(java.io.File, java.io.File, boolean)
public static String[] tail(File f, int n, String encoding) throws IOException
f
- The file to read the last n lines fromn
- The number of lines to read from the end of the file.encoding
- The encoding to read the file in.IOException
- if the file could not be read.public static String[] tail(File f, int n) throws IOException
IOException
tail(java.io.File, int, String)
public static void deleteRecursively(File file)
file
- The file or directory to delete.