Package com.yahoo.io
Class IOUtils
- java.lang.Object
-
- com.yahoo.io.IOUtils
-
public abstract class IOUtils extends java.lang.Object
Some static io convenience methods.- Author:
- bratseth, Bjorn Borud
-
-
Constructor Summary
Constructors Constructor Description IOUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
closeAll(java.util.List<java.io.Reader> readers)
Convenience method for closing a list of readers.static void
closeInputStream(java.io.InputStream stream)
Closes an input stream, or does nothing if the stream is nullstatic void
closeOutputStream(java.io.OutputStream stream)
Closes an output stream, or does nothing if the stream is nullstatic void
closeReader(java.io.Reader reader)
Closes a reader, or does nothing if the reader is nullstatic void
closeWriter(java.io.Writer writer)
Closes a writer, or does nothing if the writer is nullstatic void
copy(java.io.File inFile, java.io.File outFile)
Copies a file to another file.static void
copy(java.lang.String inFile, java.lang.String outFile)
Copies a file to another file.static void
copy(java.lang.String inFile, java.lang.String outFile, int lineCount)
Copies the n first lines of a file to another file.static void
copyDirectory(java.io.File sourceLocation, java.io.File targetLocation)
Copies all files and subdirectories (infinitely recursively) in a directory to another.static void
copyDirectory(java.io.File sourceLocation, java.io.File targetLocation, int maxRecurseLevel)
Copies all files and subdirectories in a directory to another.static void
copyDirectory(java.io.File sourceLocation, java.io.File targetLocation, int maxRecurseLevel, java.io.FilenameFilter filter)
Copies all files and subdirectories in a directory to another.static void
copyDirectoryInto(java.io.File sourceLocation, java.io.File targetLocation)
Copies the whole source directory (infinitely recursively) into the target directory.static int
countLines(java.lang.String file)
Returns the number of lines in a file.static void
createDirectory(java.lang.String filename)
Creates the directory path of this file if it does not existstatic java.io.BufferedReader
createReader(java.io.File filename, java.lang.String encoding)
Creates a buffered readerstatic java.io.BufferedReader
createReader(java.lang.String filename)
Creates a buffered reader in the default encodingstatic java.io.BufferedReader
createReader(java.lang.String filename, java.lang.String encoding)
Creates a buffered readerstatic java.io.BufferedWriter
createWriter(java.io.File file, boolean append)
Creates a buffered writer in the default encodingstatic java.io.BufferedWriter
createWriter(java.io.File file, java.lang.String encoding, boolean append)
Creates a buffered writer, and the directories to contain it if they do not existstatic java.io.BufferedWriter
createWriter(java.lang.String filename, boolean append)
Creates a buffered writer in the default encodingstatic java.io.BufferedWriter
createWriter(java.lang.String filename, java.lang.String encoding, boolean append)
Creates a buffered writer, and the directories to contain it if they do not existstatic java.util.List<java.lang.String>
getLines(java.lang.String fileName)
Returns a list containing the lines in the given file as stringsstatic java.lang.String
readAll(java.io.InputStream stream, java.nio.charset.Charset charset)
Read an input stream completely into a stringstatic java.lang.String
readAll(java.io.Reader reader)
Reads all data from a reader into a string.static byte[]
readBytes(java.io.InputStream stream, int chunkSize)
Reads all the content of the given input stream, in chunks of at max chunkSizestatic java.lang.String
readFile(java.io.File file)
Reads the contents of a UTF-8 text file into a String.static byte[]
readFileBytes(java.io.File file)
Reads the content of a file into a byte arraystatic boolean
recursiveDeleteDir(java.io.File dir)
Recursive deletion of directoriesstatic java.nio.ByteBuffer
utf8ByteBuffer(java.lang.String s)
Encodes string as UTF-8 into ByteBufferstatic void
writeFile(java.io.File file, byte[] content)
Writes the given content to the file (replacing any existing content)static void
writeFile(java.io.File file, java.lang.String text, boolean append)
Writes the given string to the filestatic void
writeFile(java.lang.String file, java.lang.String text, boolean append)
Writes the given string to the file
-
-
-
Method Detail
-
closeWriter
public static void closeWriter(java.io.Writer writer)
Closes a writer, or does nothing if the writer is null
-
closeReader
public static void closeReader(java.io.Reader reader)
Closes a reader, or does nothing if the reader is null
-
closeInputStream
public static void closeInputStream(java.io.InputStream stream)
Closes an input stream, or does nothing if the stream is null
-
closeOutputStream
public static void closeOutputStream(java.io.OutputStream stream)
Closes an output stream, or does nothing if the stream is null
-
createReader
public static java.io.BufferedReader createReader(java.io.File filename, java.lang.String encoding) throws java.io.IOException
Creates a buffered reader- Parameters:
filename
- the name or path of the fileencoding
- the encoding of the file, for instance "UTF-8"- Throws:
java.io.IOException
-
createReader
public static java.io.BufferedReader createReader(java.lang.String filename, java.lang.String encoding) throws java.io.IOException
Creates a buffered reader- Parameters:
filename
- the name or path of the fileencoding
- the encoding of the file, for instance "UTF-8"- Throws:
java.io.IOException
-
createReader
public static java.io.BufferedReader createReader(java.lang.String filename) throws java.io.IOException
Creates a buffered reader in the default encoding- Throws:
java.io.IOException
-
createWriter
public static java.io.BufferedWriter createWriter(java.lang.String filename, java.lang.String encoding, boolean append) throws java.io.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:
java.io.IOException
-
createWriter
public static java.io.BufferedWriter createWriter(java.io.File file, java.lang.String encoding, boolean append) throws java.io.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:
java.io.IOException
-
createWriter
public static java.io.BufferedWriter createWriter(java.lang.String filename, boolean append) throws java.io.IOException
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:
java.io.IOException
-
createWriter
public static java.io.BufferedWriter createWriter(java.io.File file, boolean append) throws java.io.IOException
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:
java.io.IOException
-
createDirectory
public static void createDirectory(java.lang.String filename)
Creates the directory path of this file if it does not exist
-
copy
public static void copy(java.lang.String inFile, java.lang.String outFile, int lineCount) throws java.io.IOException
Copies the n first lines of a file to another file. If the out file exists it will be overwritten- Throws:
java.io.IOException
- if copying fails
-
copy
public static void copy(java.lang.String inFile, java.lang.String outFile) throws java.io.IOException
Copies a file to another file. If the out file exists it will be overwritten.- Throws:
java.io.IOException
- if copying fails
-
copy
public static void copy(java.io.File inFile, java.io.File outFile) throws java.io.IOException
Copies a file to another file. If the out file exists it will be overwritten.- Throws:
java.io.IOException
-
copyDirectory
public static void copyDirectory(java.io.File sourceLocation, java.io.File targetLocation, int maxRecurseLevel) throws java.io.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:
java.io.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(java.io.File sourceLocation, java.io.File targetLocation, int maxRecurseLevel, java.io.FilenameFilter filter) throws java.io.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:
java.io.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(java.io.File sourceLocation, java.io.File targetLocation) throws java.io.IOException
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:
java.io.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
public static void copyDirectoryInto(java.io.File sourceLocation, java.io.File targetLocation) throws java.io.IOException
Copies the whole source directory (infinitely recursively) into the target directory.- Throws:
java.io.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
public static int countLines(java.lang.String file)
Returns the number of lines in a file. If the file does not exists, 0 is returned
-
getLines
public static java.util.List<java.lang.String> getLines(java.lang.String fileName) throws java.io.IOException
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:
java.io.IOException
- if the file could not be read
-
recursiveDeleteDir
public static boolean recursiveDeleteDir(java.io.File dir)
Recursive deletion of directories
-
utf8ByteBuffer
public static java.nio.ByteBuffer utf8ByteBuffer(java.lang.String s)
Encodes string as UTF-8 into ByteBuffer
-
readFile
public static java.lang.String readFile(java.io.File file) throws java.io.IOException
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:
java.io.IOException
-
readBytes
public static byte[] readBytes(java.io.InputStream stream, int chunkSize) throws java.io.IOException
Reads all the content of the given input stream, in chunks of at max chunkSize- Throws:
java.io.IOException
-
readFileBytes
public static byte[] readFileBytes(java.io.File file) throws java.io.IOException
Reads the content of a file into a byte array- Throws:
java.io.IOException
-
readAll
public static java.lang.String readAll(java.io.Reader reader) throws java.io.IOException
Reads all data from a reader into a string. Uses a buffer to speed up reading.- Throws:
java.io.IOException
-
readAll
public static java.lang.String readAll(java.io.InputStream stream, java.nio.charset.Charset charset) throws java.io.IOException
Read an input stream completely into a string- Throws:
java.io.IOException
-
closeAll
public static void closeAll(java.util.List<java.io.Reader> readers)
Convenience method for closing a list of readers. Does nothing if the given reader list is null.
-
writeFile
public static void writeFile(java.io.File file, java.lang.String text, boolean append) throws java.io.IOException
Writes the given string to the file- Throws:
java.io.IOException
-
writeFile
public static void writeFile(java.io.File file, byte[] content) throws java.io.UncheckedIOException
Writes the given content to the file (replacing any existing content)- Throws:
java.io.UncheckedIOException
-
writeFile
public static void writeFile(java.lang.String file, java.lang.String text, boolean append) throws java.io.IOException
Writes the given string to the file- Throws:
java.io.IOException
-
-