Class IO


  • public class IO
    extends java.lang.Object
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int EOF  
      static int UNSET  
    • Constructor Summary

      Constructors 
      Constructor Description
      IO()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.io.Reader asASCII​(java.io.InputStream in)
      Create a unbuffered reader that uses ASCII encoding
      static java.io.Writer asASCII​(java.io.OutputStream out)
      Create a writer that uses ASCII encoding
      static java.io.BufferedReader asBufferedUTF8​(java.io.InputStream in)
      Create an buffered reader that uses UTF-8 encoding
      static java.io.Writer asBufferedUTF8​(java.io.OutputStream out)
      Create a writer that uses UTF-8 encoding and is buffered.
      static java.io.PrintWriter asPrintWriterUTF8​(java.io.OutputStream out)
      Create a print writer that uses UTF-8 encoding
      static java.io.Reader asUTF8​(java.io.InputStream in)
      Create an unbuffered reader that uses UTF-8 encoding
      static java.io.Writer asUTF8​(java.io.OutputStream out)
      Create a writer that uses UTF-8 encoding
      static java.lang.String byteBufferToString​(java.nio.ByteBuffer bb)
      ByteBuffer to String
      static void close​(java.io.Closeable resource)  
      static void close​(AWriter resource)  
      static void close​(IndentedWriter resource)  
      static void close​(Closeable resource)  
      static void closeSilent​(java.io.Closeable resource)  
      static void closeSilent​(AWriter resource)  
      static void closeSilent​(IndentedWriter resource)  
      static void closeSilent​(Closeable resource)  
      static void deleteAll​(java.nio.file.Path start)
      Delete everything from a Path start point, including the path itself.
      static void exception​(java.io.IOException ex)
      Throw a RuntimeIOException - this function is guaranteed not to return normally
      static void exception​(java.lang.String message)
      Throw a RuntimeIOException - this function is guaranteed not to return normally
      static void exception​(java.lang.String msg, java.io.IOException ex)
      Throw a RuntimeIOException - this function is guaranteed not to return normally
      static boolean exists​(java.lang.String directory)  
      static java.lang.String filenameNoCompression​(java.lang.String filename)
      The filename without any compression extension, or the original filename.
      static void flush​(java.io.OutputStream out)  
      static void flush​(java.io.Writer out)  
      static void flush​(AWriter out)  
      static boolean isDirectory​(java.lang.String directory)  
      static boolean isEmptyDirectory​(java.lang.String directory)  
      static java.io.InputStream openFile​(java.lang.String filename)
      Open an input stream to a file.
      static java.io.Reader openFileASCII​(java.lang.String filename)
      Open an ASCII Reader for a file.
      static java.io.InputStream openFileBuffered​(java.lang.String filename)
      Open an input stream to a file and buffer it.
      static java.io.InputStream openFileEx​(java.lang.String filename)
      Open an input stream to a file; do not mask IOExceptions.
      static java.io.Reader openFileUTF8​(java.lang.String filename)
      Open a UTF8 Reader for a file.
      static java.io.OutputStream openOutputFile​(java.lang.String filename)
      Open a file for output - may include adding gzip processing.
      static java.io.OutputStream openOutputFileEx​(java.lang.String filename)
      Open an input stream to a file; do not mask IOExceptions.
      static byte[] readWholeFile​(java.io.InputStream in)  
      static java.lang.String readWholeFileAsUTF8​(java.io.InputStream in)
      Read a whole stream as UTF-8
      static java.lang.String readWholeFileAsUTF8​(java.lang.String filename)
      Read a whole file as UTF-8
      static void skipToEnd​(java.io.InputStream input)
      Skip to the end of the InputStream, discarding input.
      static java.nio.ByteBuffer stringToByteBuffer​(java.lang.String str)
      String to ByteBuffer as UTF-8 bytes
      static java.lang.String uniqueFilename​(java.lang.String directory, java.lang.String base, java.lang.String ext)  
      static AWriter wrap​(java.io.Writer w)
      Wrap in a general writer interface
      static AWriter wrapASCII​(java.io.OutputStream out)
      Wrap in a general writer interface
      static AWriter wrapUTF8​(java.io.OutputStream out)
      Wrap in a general writer interface
      static void writeStringAsUTF8​(java.io.OutputStream out, java.lang.String content)
      Write a string into an OutputStream as UTF-8.
      static void writeStringAsUTF8​(java.lang.String filename, java.lang.String content)
      Write a string to a file as UTF-8.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • IO

        public IO()
    • Method Detail

      • openFile

        public static java.io.InputStream openFile​(java.lang.String filename)
        Open an input stream to a file.

        If the filename is null or "-", return System.in If the filename ends in .gz, wrap in GZIPInputStream

        Throws RuntimeIOException on failure to open.

      • openFileBuffered

        public static java.io.InputStream openFileBuffered​(java.lang.String filename)
        Open an input stream to a file and buffer it. If the filename is null or "-", return System.in If the filename ends in .gz, wrap in GZIPInputStream. If using this InputStream with an InputStreamReader (e.g. to get UTF-8), there is no need to buffer the InputStream. Instead, buffer the Reader.

        Throws RuntimeIOException on failure to open.

      • openFileEx

        public static java.io.InputStream openFileEx​(java.lang.String filename)
                                              throws java.io.IOException,
                                                     java.io.FileNotFoundException
        Open an input stream to a file; do not mask IOExceptions. If the filename is null or "-", return System.in If the filename ends in .gz, wrap in GZIPInputStream
        Parameters:
        filename -
        Throws:
        java.io.FileNotFoundException
        java.io.IOException
      • filenameNoCompression

        public static java.lang.String filenameNoCompression​(java.lang.String filename)
        The filename without any compression extension, or the original filename. It tests for compression types handled by openFileEx(java.lang.String).
      • openFileUTF8

        public static java.io.Reader openFileUTF8​(java.lang.String filename)
        Open a UTF8 Reader for a file. If the filename is null or "-", use System.in If the filename ends in .gz, use GZIPInputStream
      • openFileASCII

        public static java.io.Reader openFileASCII​(java.lang.String filename)
        Open an ASCII Reader for a file. If the filename is null or "-", use System.in If the filename ends in .gz, use GZIPInputStream
      • asUTF8

        public static java.io.Reader asUTF8​(java.io.InputStream in)
        Create an unbuffered reader that uses UTF-8 encoding
      • asASCII

        public static java.io.Reader asASCII​(java.io.InputStream in)
        Create a unbuffered reader that uses ASCII encoding
      • asBufferedUTF8

        public static java.io.BufferedReader asBufferedUTF8​(java.io.InputStream in)
        Create an buffered reader that uses UTF-8 encoding
      • asUTF8

        public static java.io.Writer asUTF8​(java.io.OutputStream out)
        Create a writer that uses UTF-8 encoding
      • asASCII

        public static java.io.Writer asASCII​(java.io.OutputStream out)
        Create a writer that uses ASCII encoding
      • asBufferedUTF8

        public static java.io.Writer asBufferedUTF8​(java.io.OutputStream out)
        Create a writer that uses UTF-8 encoding and is buffered.
      • openOutputFile

        public static java.io.OutputStream openOutputFile​(java.lang.String filename)
        Open a file for output - may include adding gzip processing.

        Throws RuntimeIOException on failure to open.

      • openOutputFileEx

        public static java.io.OutputStream openOutputFileEx​(java.lang.String filename)
                                                     throws java.io.FileNotFoundException,
                                                            java.io.IOException
        Open an input stream to a file; do not mask IOExceptions. If the filename ends in .gz, wrap in GZIPOutputStream
        Parameters:
        filename -
        Throws:
        java.io.FileNotFoundException - If the output can't be opened.
        java.io.IOException - for bad gzip encoded data
      • wrap

        public static AWriter wrap​(java.io.Writer w)
        Wrap in a general writer interface
      • wrapUTF8

        public static AWriter wrapUTF8​(java.io.OutputStream out)
        Wrap in a general writer interface
      • wrapASCII

        public static AWriter wrapASCII​(java.io.OutputStream out)
        Wrap in a general writer interface
      • asPrintWriterUTF8

        public static java.io.PrintWriter asPrintWriterUTF8​(java.io.OutputStream out)
        Create a print writer that uses UTF-8 encoding
      • isEmptyDirectory

        public static boolean isEmptyDirectory​(java.lang.String directory)
      • exists

        public static boolean exists​(java.lang.String directory)
      • isDirectory

        public static boolean isDirectory​(java.lang.String directory)
      • close

        public static void close​(Closeable resource)
      • closeSilent

        public static void closeSilent​(Closeable resource)
      • close

        public static void close​(java.io.Closeable resource)
      • closeSilent

        public static void closeSilent​(java.io.Closeable resource)
      • close

        public static void close​(AWriter resource)
      • closeSilent

        public static void closeSilent​(AWriter resource)
      • closeSilent

        public static void closeSilent​(IndentedWriter resource)
      • exception

        public static void exception​(java.lang.String message)
        Throw a RuntimeIOException - this function is guaranteed not to return normally
      • exception

        public static void exception​(java.io.IOException ex)
        Throw a RuntimeIOException - this function is guaranteed not to return normally
      • exception

        public static void exception​(java.lang.String msg,
                                     java.io.IOException ex)
        Throw a RuntimeIOException - this function is guaranteed not to return normally
      • flush

        public static void flush​(java.io.OutputStream out)
      • flush

        public static void flush​(java.io.Writer out)
      • flush

        public static void flush​(AWriter out)
      • readWholeFile

        public static byte[] readWholeFile​(java.io.InputStream in)
      • readWholeFileAsUTF8

        public static java.lang.String readWholeFileAsUTF8​(java.lang.String filename)
        Read a whole file as UTF-8
        Parameters:
        filename -
        Returns:
        String
      • readWholeFileAsUTF8

        public static java.lang.String readWholeFileAsUTF8​(java.io.InputStream in)
        Read a whole stream as UTF-8
        Parameters:
        in - InputStream to be read
        Returns:
        String
      • writeStringAsUTF8

        public static void writeStringAsUTF8​(java.lang.String filename,
                                             java.lang.String content)
                                      throws java.io.IOException
        Write a string to a file as UTF-8. The file is closed after the operation.
        Parameters:
        filename -
        content - String to be written
        Throws:
        java.io.IOException
      • writeStringAsUTF8

        public static void writeStringAsUTF8​(java.io.OutputStream out,
                                             java.lang.String content)
                                      throws java.io.IOException
        Write a string into an OutputStream as UTF-8.
        Parameters:
        out - OutputStream destination.
        content - String to be written
        Throws:
        java.io.IOException
      • stringToByteBuffer

        public static java.nio.ByteBuffer stringToByteBuffer​(java.lang.String str)
        String to ByteBuffer as UTF-8 bytes
      • byteBufferToString

        public static java.lang.String byteBufferToString​(java.nio.ByteBuffer bb)
        ByteBuffer to String
      • uniqueFilename

        public static java.lang.String uniqueFilename​(java.lang.String directory,
                                                      java.lang.String base,
                                                      java.lang.String ext)
      • deleteAll

        public static void deleteAll​(java.nio.file.Path start)
        Delete everything from a Path start point, including the path itself. This function works on files or directories. This function does not follow symbolic links.
      • skipToEnd

        public static void skipToEnd​(java.io.InputStream input)
        Skip to the end of the InputStream, discarding input.