Package com.yahoo.io
Class IOUtils
java.lang.Object
com.yahoo.io.IOUtils
Some static io convenience methods.
- Author:
- bratseth, Bjorn Borud
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
Convenience method for closing a list of readers.static void
closeInputStream
(InputStream stream) Closes an input stream, or does nothing if the stream is nullstatic void
closeOutputStream
(OutputStream stream) Closes an output stream, or does nothing if the stream is nullstatic void
closeReader
(Reader reader) Closes a reader, or does nothing if the reader is nullstatic void
closeWriter
(Writer writer) Closes a writer, or does nothing if the writer is nullstatic void
Copies a file to another file.static void
Copies a file to another file.static void
Copies the n first lines of a file to another file.static void
copyDirectory
(File sourceLocation, File targetLocation) Copies all files and subdirectories (infinitely recursively) in a directory to another.static void
copyDirectory
(File sourceLocation, File targetLocation, int maxRecurseLevel) Copies all files and subdirectories in a directory to another.static void
copyDirectory
(File sourceLocation, File targetLocation, int maxRecurseLevel, FilenameFilter filter) Copies all files and subdirectories in a directory to another.static void
copyDirectoryInto
(File sourceLocation, File targetLocation) Copies the whole source directory (infinitely recursively) into the target directory.static int
countLines
(String file) Returns the number of lines in a file.static void
createDirectory
(String filename) Creates the directory path of this file if it does not existstatic BufferedReader
createReader
(File filename, String encoding) Creates a buffered readerstatic BufferedReader
createReader
(String filename) Creates a buffered reader in the default encodingstatic BufferedReader
createReader
(String filename, String encoding) Creates a buffered readerstatic BufferedWriter
createWriter
(File file, boolean append) Creates a buffered writer in the default encodingstatic BufferedWriter
createWriter
(File file, String encoding, boolean append) Creates a buffered writer, and the directories to contain it if they do not existstatic BufferedWriter
createWriter
(String filename, boolean append) Creates a buffered writer in the default encodingstatic BufferedWriter
createWriter
(String filename, String encoding, boolean append) Creates a buffered writer, and the directories to contain it if they do not existReturns a list containing the lines in the given file as stringsstatic String
readAll
(InputStream stream, Charset charset) Read an input stream completely into a stringstatic String
Reads all data from a reader into a string.static byte[]
readBytes
(InputStream stream, int chunkSize) Reads all the content of the given input stream, in chunks of at max chunkSizestatic String
Reads the contents of a UTF-8 text file into a String.static byte[]
readFileBytes
(File file) Reads the content of a file into a byte arraystatic boolean
recursiveDeleteDir
(File dir) Recursive deletion of directoriesstatic ByteBuffer
Encodes string as UTF-8 into ByteBufferstatic void
Writes the given content to the file (replacing any existing content)static void
Writes the given string to the filestatic void
Writes the given string to the file
-
Constructor Details
-
IOUtils
public IOUtils()
-
-
Method Details
-
closeWriter
Closes a writer, or does nothing if the writer is null -
closeReader
Closes a reader, or does nothing if the reader is null -
closeInputStream
Closes an input stream, or does nothing if the stream is null -
closeOutputStream
Closes an output stream, or does nothing if the stream is null -
createReader
Creates a buffered reader- Parameters:
filename
- the name or path of the fileencoding
- the encoding of the file, for instance "UTF-8"- Throws:
IOException
-
createReader
Creates a buffered reader- Parameters:
filename
- the name or path of the fileencoding
- the encoding of the file, for instance "UTF-8"- Throws:
IOException
-
createReader
Creates a buffered reader in the default encoding- Throws:
IOException
-
createWriter
public static BufferedWriter createWriter(String filename, String encoding, boolean append) throws IOException Creates a buffered writer, and the directories to contain it if they do not exist- Parameters:
filename
- the name or path of the fileencoding
- the encoding to use, for instance "UTF-8"append
- whether to append to the files if it exists- Throws:
IOException
-
createWriter
public static BufferedWriter createWriter(File file, String encoding, boolean append) throws IOException Creates a buffered writer, and the directories to contain it if they do not exist- Parameters:
file
- the file to write toencoding
- the encoding to use, for instance "UTF-8"append
- whether to append to the files if it exists- Throws:
IOException
-
createWriter
Creates a buffered writer in the default encoding- Parameters:
filename
- the name or path of the fileappend
- whether to append to the files if it exists- Throws:
IOException
-
createWriter
Creates a buffered writer in the default encoding- Parameters:
file
- the file to write toappend
- whether to append to the files if it exists- Throws:
IOException
-
createDirectory
Creates the directory path of this file if it does not exist -
copy
Copies the n first lines of a file to another file. If the out file exists it will be overwritten- Throws:
IOException
- if copying fails
-
copy
Copies a file to another file. If the out file exists it will be overwritten.- Throws:
IOException
- if copying fails
-
copy
Copies a file to another file. If the out file exists it will be overwritten.- Throws:
IOException
-
copyDirectory
public static void copyDirectory(File sourceLocation, File targetLocation, int maxRecurseLevel) throws IOException Copies all files and subdirectories in a directory to another. Any existing files are overwritten.- Parameters:
sourceLocation
- the source directorytargetLocation
- the target directorymaxRecurseLevel
- if this is 1, only files immediately in sourceLocation are copied, if it is 2, then files contained in immediate subdirectories are copied, etc. If it is 0, sourceLocation will only be copied if it is a file, not a directory. If it is negative, recursion is infinite.- Throws:
IOException
- if copying any file fails. This will typically result in some files being copied and others not, i.e this method is not exception safe
-
copyDirectory
public static void copyDirectory(File sourceLocation, File targetLocation, int maxRecurseLevel, FilenameFilter filter) throws IOException Copies all files and subdirectories in a directory to another. Any existing files are overwritten.- Parameters:
sourceLocation
- the source directorytargetLocation
- the target directorymaxRecurseLevel
- if this is 1, only files immediately in sourceLocation are copied, if it is 2, then files contained in immediate subdirectories are copied, etc. If it is 0, sourceLocation will only be copied if it is a file, not a directory. If it is negative, recursion is infinite.filter
- Only copy files passing through filter.- Throws:
IOException
- if copying any file fails. This will typically result in some files being copied and others not, i.e this method is not exception safe
-
copyDirectory
Copies all files and subdirectories (infinitely recursively) in a directory to another. Any existing files are overwritten.- Parameters:
sourceLocation
- the source directorytargetLocation
- the target directory- Throws:
IOException
- if copying any file fails. This will typically result in some files being copied and others not, i.e this method is not exception safe
-
copyDirectoryInto
Copies the whole source directory (infinitely recursively) into the target directory.- Throws:
IOException
- if copying any file fails. This will typically result in some files being copied and others not, i.e this method is not exception safe
-
countLines
Returns the number of lines in a file. If the file does not exists, 0 is returned -
getLines
Returns a list containing the lines in the given file as strings- Returns:
- a list of Strings for the lines of the file, in order
- Throws:
IOException
- if the file could not be read
-
recursiveDeleteDir
Recursive deletion of directories -
utf8ByteBuffer
Encodes string as UTF-8 into ByteBuffer -
readFile
Reads the contents of a UTF-8 text file into a String.- Parameters:
file
- the file to read, or null- Returns:
- the file content as a string, or null if the input file was null
- Throws:
IOException
-
readBytes
Reads all the content of the given input stream, in chunks of at max chunkSize- Throws:
IOException
-
readFileBytes
Reads the content of a file into a byte array- Throws:
IOException
-
readAll
Reads all data from a reader into a string. Uses a buffer to speed up reading.- Throws:
IOException
-
readAll
Read an input stream completely into a string- Throws:
IOException
-
closeAll
Convenience method for closing a list of readers. Does nothing if the given reader list is null. -
writeFile
Writes the given string to the file- Throws:
IOException
-
writeFile
Writes the given content to the file (replacing any existing content)- Throws:
UncheckedIOException
-
writeFile
Writes the given string to the file- Throws:
IOException
-