Package com.yahoo.io

Class IOUtils


  • public abstract class IOUtils
    extends Object
    Some static io convenience methods.
    Author:
    bratseth, Bjorn Borud
    • Constructor Detail

      • IOUtils

        public IOUtils()
    • Method Detail

      • closeWriter

        public static void closeWriter​(Writer writer)
        Closes a writer, or does nothing if the writer is null
      • closeReader

        public static void closeReader​(Reader reader)
        Closes a reader, or does nothing if the reader is null
      • closeInputStream

        public static void closeInputStream​(InputStream stream)
        Closes an input stream, or does nothing if the stream is null
      • closeOutputStream

        public static void closeOutputStream​(OutputStream stream)
        Closes an output stream, or does nothing if the stream is null
      • createReader

        public static BufferedReader createReader​(File filename,
                                                  String encoding)
                                           throws IOException
        Creates a buffered reader
        Parameters:
        filename - the name or path of the file
        encoding - the encoding of the file, for instance "UTF-8"
        Throws:
        IOException
      • createReader

        public static BufferedReader createReader​(String filename,
                                                  String encoding)
                                           throws IOException
        Creates a buffered reader
        Parameters:
        filename - the name or path of the file
        encoding - the encoding of the file, for instance "UTF-8"
        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 file
        encoding - 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 to
        encoding - 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​(String filename,
                                                  boolean append)
                                           throws IOException
        Creates a buffered writer in the default encoding
        Parameters:
        filename - the name or path of the file
        append - whether to append to the files if it exists
        Throws:
        IOException
      • createWriter

        public static BufferedWriter createWriter​(File file,
                                                  boolean append)
                                           throws IOException
        Creates a buffered writer in the default encoding
        Parameters:
        file - the file to write to
        append - whether to append to the files if it exists
        Throws:
        IOException
      • createDirectory

        public static void createDirectory​(String filename)
        Creates the directory path of this file if it does not exist
      • copy

        public static void copy​(String inFile,
                                String outFile,
                                int lineCount)
                         throws IOException
        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

        public static void copy​(String inFile,
                                String outFile)
                         throws IOException
        Copies a file to another file. If the out file exists it will be overwritten.
        Throws:
        IOException - if copying fails
      • copy

        public static void copy​(File inFile,
                                File outFile)
                         throws IOException
        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 directory
        targetLocation - the target directory
        maxRecurseLevel - 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 directory
        targetLocation - the target directory
        maxRecurseLevel - 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

        public static void copyDirectory​(File sourceLocation,
                                         File targetLocation)
                                  throws IOException
        Copies all files and subdirectories (infinitely recursively) in a directory to another. Any existing files are overwritten.
        Parameters:
        sourceLocation - the source directory
        targetLocation - 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

        public static void copyDirectoryInto​(File sourceLocation,
                                             File targetLocation)
                                      throws IOException
        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

        public static int countLines​(String file)
        Returns the number of lines in a file. If the file does not exists, 0 is returned
      • getLines

        public static List<String> getLines​(String fileName)
                                     throws 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:
        IOException - if the file could not be read
      • recursiveDeleteDir

        public static boolean recursiveDeleteDir​(File dir)
        Recursive deletion of directories
      • utf8ByteBuffer

        public static ByteBuffer utf8ByteBuffer​(String s)
        Encodes string as UTF-8 into ByteBuffer
      • readFile

        public static String readFile​(File file)
                               throws 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:
        IOException
      • readBytes

        public static byte[] readBytes​(InputStream stream,
                                       int chunkSize)
                                throws IOException
        Reads all the content of the given input stream, in chunks of at max chunkSize
        Throws:
        IOException
      • readFileBytes

        public static byte[] readFileBytes​(File file)
                                    throws IOException
        Reads the content of a file into a byte array
        Throws:
        IOException
      • readAll

        public static String readAll​(Reader reader)
                              throws IOException
        Reads all data from a reader into a string. Uses a buffer to speed up reading.
        Throws:
        IOException
      • closeAll

        public static void closeAll​(List<Reader> readers)
        Convenience method for closing a list of readers. Does nothing if the given reader list is null.
      • writeFile

        public static void writeFile​(File file,
                                     String text,
                                     boolean append)
                              throws IOException
        Writes the given string to the file
        Throws:
        IOException