Class IOMethods

java.lang.Object
org.klojang.util.IOMethods

public class IOMethods extends Object
I/O-related methods.
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    append(String path, String contents)
    Simple file-write method.
    static File
    Creates a new, empty directory under the file system's temp directory.
    static File
    createTempDir(Class<?> requester)
    Creates a new, empty directory under the file system's temp directory.
    static File
    createTempDir(Class<?> requester, boolean touch)
    Creates a File object with a unique file name, located file system's temp directory.
    static File
    createTempDir(Class<?> requester, String extension, boolean touch)
    Creates a File object with a unique file name, located in file system's temp directory.
    static File
    createTempDir(Class<?> requester, String relativePath, String extension, boolean touch)
    Creates a File object with a unique file name, located in file system's temp directory.
    static File
    Creates a new, empty file in the file system's temp directory.
    static File
    createTempFile(Class<?> requester)
    Creates a new, empty file in the file system's temp directory.
    static File
    createTempFile(Class<?> requester, boolean touch)
    Creates a File object with a unique file name, located file system's temp directory.
    static File
    createTempFile(Class<?> requester, String extension, boolean touch)
    Creates a File object with a unique file name, located in file system's temp directory.
    static File
    createTempFile(Class<?> requester, String relativePath, String extension, boolean touch)
    Creates a File object with a unique file name, located in file system's temp directory.
    static String
    Returns a String created from the bytes read from the specified input stream.
    static String
    getContents(InputStream in, int chunkSize)
    Returns a String created from the bytes read from the specified input stream.
    static String
    getContents(Class<?> clazz, String path)
    Returns the contents of the specified resource.
    static String
    getContents(Class<?> clazz, String path, int chunkSize)
    Returns the contents of the specified resource.
    static String
    Returns the contents of the specified file.
    static void
    Reads all bytes from the specified input stream and writes them to the specified output stream.
    static void
    pipe(InputStream in, OutputStream out, int chunkSize)
    Reads all bytes from the specified input stream and writes them to the specified output stream.
    static byte[]
    Returns a byte[] array containing the bytes read from the specified input stream.
    static byte[]
    readBytes(InputStream in, int chunkSize)
    Returns a byte[] array containing the bytes read from the specified input stream.
    static byte[]
    readBytes(Class<?> clazz, String path)
    Returns the contents of the specified resource as a byte array.
    static byte[]
    readBytes(Class<?> clazz, String path, int chunkSize)
    Returns the contents of the specified resource as a byte array.
    static void
    rm(File file)
    Deletes a file or directory.
    static void
    rm(String path)
    Deletes a file or directory.
    static void
    write(String path, String contents)
    Simple file-write method.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • getContents

      public static String getContents(String path)
      Returns the contents of the specified file.
      Parameters:
      path - the path to the file
      Returns:
      the contents of the specified file
    • getContents

      public static String getContents(Class<?> clazz, String path)
      Returns the contents of the specified resource. Bytes are read in chunks of 512 bytes.
      Parameters:
      clazz - the Class to call getResourceAsStream on
      path - the path to the resource
      Returns:
      the contents of the specified resource
    • getContents

      public static String getContents(Class<?> clazz, String path, int chunkSize)
      Returns the contents of the specified resource.
      Parameters:
      clazz - the Class to call getResourceAsStream on
      path - the path to the resource
      chunkSize - the number of bytes to read at a time
      Returns:
      the contents of the specified resource
    • getContents

      public static String getContents(InputStream in)
      Returns a String created from the bytes read from the specified input stream. Bytes are read in chunks of 512 bytes. The input stream is not closed once all bytes have been read.
      Parameters:
      in - the input stream
      Returns:
      a String from the bytes read from the specified input stream
    • getContents

      public static String getContents(InputStream in, int chunkSize)
      Returns a String created from the bytes read from the specified input stream. The input stream is not closed once all bytes have been read.
      Parameters:
      in - the input stream
      chunkSize - the number of bytes to read at a time
      Returns:
      a String from the bytes read from the specified input stream
    • write

      public static void write(String path, String contents)
      Simple file-write method. Not efficient, but easy to use. Overwrites pre-existing contents.
      Parameters:
      path - the path to the file
      contents - The contents to be written
    • append

      public static void append(String path, String contents)
      Simple file-write method. Not efficient, but easy to use. Appends the specified string to the contents of the specified file.
      Parameters:
      path - the path to the file
      contents - The contents to be written
    • readBytes

      public static byte[] readBytes(Class<?> clazz, String path)
      Returns the contents of the specified resource as a byte array. Bytes are read in chunks of 512 bytes.
      Parameters:
      clazz - the Class to call getResourceAsStream on
      path - the path to the resource
      Returns:
      the bytes contained in the specified resource
    • readBytes

      public static byte[] readBytes(Class<?> clazz, String path, int chunkSize)
      Returns the contents of the specified resource as a byte array.
      Parameters:
      clazz - the Class to call getResourceAsStream on
      path - the path to the resource
      chunkSize - the number of bytes to read at a time
      Returns:
      the contents of the specified resource
    • readBytes

      public static byte[] readBytes(InputStream in)
      Returns a byte[] array containing the bytes read from the specified input stream. Bytes are read in chunks of 512 bytes. The input stream is not closed by this method.
      Parameters:
      in - the input stream
      Returns:
      a byte[] array containing the bytes read from the specified input stream
    • readBytes

      public static byte[] readBytes(InputStream in, int chunkSize)
      Returns a byte[] array containing the bytes read from the specified input stream. Bytes are read in chunks of the specified size. The input stream is not closed by this method.
      Parameters:
      in - the input stream
      chunkSize - the number of bytes to read at a time
      Returns:
      a byte[] array containing the bytes read from the specified input stream
    • pipe

      public static void pipe(InputStream in, OutputStream out)
      Reads all bytes from the specified input stream and writes them to the specified output stream. Bytes are read and written in chunks of 512 bytes at a time. The input stream and the output stream are not closed by this method.
      Parameters:
      in - the input stream
      out - the output stream
    • pipe

      public static void pipe(InputStream in, OutputStream out, int chunkSize)
      Reads all bytes from the specified input stream and writes them to the specified output stream. Bytes are read and written in chunks of the specified size. The input stream and the output stream are not closed by this method.
      Parameters:
      in - the input stream
      out - the output stream
      chunkSize - the number of bytes read/written at a time
    • createTempFile

      public static File createTempFile() throws IOException
      Creates a new, empty file in the file system's temp directory. Equivalent to:
      
       createTempFile(IOMethods.class, ".tmp", true)
       
      Returns:
      a File object for a new, empty file in the file system's temp directory
      Throws:
      IOException - if an I/O error occurs
    • createTempFile

      public static File createTempFile(Class<?> requester) throws IOException
      Creates a new, empty file in the file system's temp directory. Equivalent to:
      
       createTempFile(requester ".tmp", true)
       
      Parameters:
      requester - the class requesting the temp file (its simple name will become part of the file name)
      Returns:
      a File object for a new, empty file in the file system's temp directory
      Throws:
      IOException - if an I/O error occurs
    • createTempFile

      public static File createTempFile(Class<?> requester, boolean touch) throws IOException
      Creates a File object with a unique file name, located file system's temp directory. Equivalent to:
      
       createTempFile(requester "tmp", touch)
       
      Parameters:
      requester - the class requesting the temp file (its simple name will become part of the file name)
      touch - whether to actually create the file on the file system
      Returns:
      a File object for a new, empty file in the file system's temp directory
      Throws:
      IOException - if an I/O error occurs
    • createTempFile

      public static File createTempFile(Class<?> requester, String extension, boolean touch) throws IOException
      Creates a File object with a unique file name, located in file system's temp directory.
      Parameters:
      requester - the class requesting the temp file (its simple name will become part of the file name)
      extension - the extension to append to the generated directory name. If empty or null, no extension will be appended to the file name. Otherwise the extension may or may not start with "." (the dot will be prepended if absent).
      touch - whether to actually create the file on the file system
      Returns:
      a File object for a new, empty file in the file system's temp directory
      Throws:
      IOException - if an I/O error occurs
    • createTempFile

      public static File createTempFile(Class<?> requester, String relativePath, String extension, boolean touch) throws IOException
      Creates a File object with a unique file name, located in file system's temp directory.
      Parameters:
      requester - the class requesting the temp directory (its simple name will become part of the file name)
      relativePath - any intermediate directories between the file system's temp directory and the file to be created. "/" means the file will be created directly underneath the file system's temp directory. Intermediate directories will be created if necessary. If the path does not start and/or end with "/" (File.separator), it will be prepended resp. appended.
      extension - the extension to append to the generated directory name. If empty or null, no extension will be appended to the directory name. Otherwise the extension may or may not start with "." (the dot will be prepended if absent).
      touch - whether to actually create the directory on the file system
      Returns:
      a File object for a new, empty file in the file system's temp directory
      Throws:
      IOException - if an I/O error occurs
    • createTempDir

      public static File createTempDir() throws IOException
      Creates a new, empty directory under the file system's temp directory. Equivalent to:
      
       createTempFile(requester ".d", true)
       
      Returns:
      a File object for a new, empty directory in the file system's temp directory
      Throws:
      IOException - if an I/O error occurs
    • createTempDir

      public static File createTempDir(Class<?> requester) throws IOException
      Creates a new, empty directory under the file system's temp directory. Equivalent to:
      
       createTempFile(requester, ".d", true)
       
      Parameters:
      requester - the class requesting the temp file (its simple name will become part of the file name)
      Returns:
      a File object for a new, empty file in the file system's temp directory
      Throws:
      IOException - if an I/O error occurs
    • createTempDir

      public static File createTempDir(Class<?> requester, boolean touch) throws IOException
      Creates a File object with a unique file name, located file system's temp directory. Equivalent to:
      
       createTempDir(requester ".d", touch)
       
      Parameters:
      requester - the class requesting the temp directory (its simple name will become part of the file name)
      touch - whether to actually create the directory on the file system
      Returns:
      a File object for a new, empty directory in the file system's temp directory
      Throws:
      IOException - if an I/O error occurs
    • createTempDir

      public static File createTempDir(Class<?> requester, String extension, boolean touch) throws IOException
      Creates a File object with a unique file name, located in file system's temp directory. If touch equals true, the File object will be used to create a directory underneath the temp directory.
      Parameters:
      requester - the class requesting the temp directory (its simple name will become part of the file name)
      extension - the extension to append to the generated directory name. If empty or null, no extension will be appended to the directory name. Otherwise the extension may or may not start with "." (the dot will be prepended if absent).
      touch - whether to actually create the directory on the file system
      Returns:
      a File object for a new, empty directory in the file system's temp directory
      Throws:
      IOException - if an I/O error occurs
    • createTempDir

      public static File createTempDir(Class<?> requester, String relativePath, String extension, boolean touch) throws IOException
      Creates a File object with a unique file name, located in file system's temp directory. If touch equals true, the File object will be used to create a directory underneath the temp directory.
      Parameters:
      requester - the class requesting the temp directory (its simple name will become part of the file name)
      relativePath - any intermediate directories between the file system's temp directory and the directory to be created. "/" means the directory will be created directly underneath the file system's temp directory. Intermediate directories will be created if necessary. If the path does not start or end with "/" (File.separator), it will be prepended resp. appended.
      extension - the extension to append to the generated directory name. If empty or null, no extension will be appended to the directory name. Otherwise the extension may or may not start with "." (the dot will be prepended if absent).
      touch - whether to actually create the directory on the file system
      Returns:
      a File object for a new, empty directory in the file system's temp directory
      Throws:
      IOException - if an I/O error occurs
    • rm

      public static void rm(String path)
      Deletes a file or directory. Directories need not be empty. If the file or directory does not exist, this method returns quietly.
      Parameters:
      path - the path of the file/directory to be deleted
    • rm

      public static void rm(File file)
      Deletes a file or directory. Directories need not be empty. If the file or directory does not exist, this method returns quietly.
      Parameters:
      file - The file or directory to delete.