Class IOUtils

java.lang.Object
com.gentlyweb.utils.IOUtils

public class IOUtils extends Object
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    copyFile(File oldFile, File newFile, int bufSize)
    Copy a file from one location to a new location.
    static String
    getFile(File file)
    Get the contents of a File as a String.
    static byte[]
    Get the contents of a File as a byte array.
    static String
    Get a file length as kilobytes in the form x.y.
    static String
    Get a file length as formatted string.
    static boolean
    Ask a question of the User on stdout and wait for a response.
    static void
    gzipFile(File file, File newFile)
    GZIP a file, this will move the file from the given name to the new location.
    static void
    streamTo(InputStream in, OutputStream out, int bufSize)
    This method acts as a "join" between an input and an output stream, all it does is take the input and keep reading it in and sending it directly to the output using the specified buffer size.
    static void
    writeBytesToFile(File file, byte[] bytes)
    Write the given bytes to a file, note that this method will just overwrite any existing file.
    static void
    writeBytesToFile(File file, byte[] bytes, int bufSize)
    Write the given bytes to a file, note that this method will just overwrite any existing file.
    static void
    writeStringToFile(File file, String str, boolean compress)
    Write the given String to the File.

    Methods inherited from class java.lang.Object

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

    • IOUtils

      public IOUtils()
  • Method Details

    • copyFile

      public static void copyFile(File oldFile, File newFile, int bufSize) throws IOException
      Copy a file from one location to a new location.
      Parameters:
      oldFile - The old file name.
      newFile - The new file location.
      bufSize - The buffer size to use when performing the copy.
      Throws:
      IOException - If we can't perform the copy.
    • gzipFile

      public static void gzipFile(File file, File newFile) throws ChainException
      GZIP a file, this will move the file from the given name to the new location. This leaves the old file in place!
      Parameters:
      file - The existing file name of the file.
      newFile - The new file location for the file.
      Throws:
      ChainException - If we can't perform the transfer, the inner exception will contain an IOException that is the "real" exception.
    • getFileLengthAsFormattedKilobytes

      public static String getFileLengthAsFormattedKilobytes(long length)
      Get a file length as kilobytes in the form x.y.
      Parameters:
      length - The length of the file.
      Returns:
      A String formatted as x.y.
    • getFormattedFileLength

      public static String getFormattedFileLength(long length)
      Get a file length as formatted string.
      • If the long is > 1024 * 1024 then we return the size as X.YY MB
      • If the long is > 1024 then we return the size as X.YY KB.
      • If the long is invalid input: '<' 1024 then we return the size as XXXX B.
      Parameters:
      length - The length of the file.
      Returns:
      A String formatted as indicated above.
    • streamTo

      public static void streamTo(InputStream in, OutputStream out, int bufSize) throws IOException
      This method acts as a "join" between an input and an output stream, all it does is take the input and keep reading it in and sending it directly to the output using the specified buffer size. In this way you can join any input and output streams to pass the data between them. Note: this method does NOT flush or close either stream (this is to allow the "channel" to remain open and re-used.
      Parameters:
      in - The input stream.
      out - The output stream.
      bufSize - The buffer size.
      Throws:
      IOException - If an IO exception occurs.
    • writeBytesToFile

      public static void writeBytesToFile(File file, byte[] bytes, int bufSize) throws IOException
      Write the given bytes to a file, note that this method will just overwrite any existing file.
      Parameters:
      file - The file to write to.
      bytes - The byte array.
      bufSize - The size of output buffer to use, set to -1 to have the buffer size set to bytes.length.
      Throws:
      IOException - If the array cannot be written to the file.
    • writeBytesToFile

      public static void writeBytesToFile(File file, byte[] bytes) throws IOException
      Write the given bytes to a file, note that this method will just overwrite any existing file. For more control over the output buffer size use: writeBytesToFile(File,byte[],int).
      Parameters:
      file - The file to write to.
      bytes - The byte array.
      Throws:
      IOException - If the array cannot be written to the file.
    • writeStringToFile

      public static void writeStringToFile(File file, String str, boolean compress) throws IOException
      Write the given String to the File.
      Parameters:
      file - The file to write to.
      str - The value to write.
      Throws:
      IOException - This should never happen because PrintWriter will catch it.
    • getFile

      public static String getFile(File file) throws IOException
      Get the contents of a File as a String. Remember that this method is constrained by the underlying limits of an array size, since that can only be as big as Integer.MAX_VALUE any file bigger than that cannot be returned. Although why you would want to read a file into memory bigger than 2GB is anyone's guess.
      Parameters:
      file - The file to read in.
      Returns:
      The content of the file as a String.
      Throws:
      IOException - If there is a problem with the read.
      IllegalArgumentException - If the file length is greater than 232-1 since the maximum size of an array is (max)int - 1.
    • getFileAsArray

      public static byte[] getFileAsArray(File file) throws IOException
      Get the contents of a File as a byte array. We use a BufferedInputStream and read the entire file in one go.
      Parameters:
      file - The file to read in.
      Returns:
      The content of the file as a byte array.
      Throws:
      IOException - If there is a problem with the read.
      IllegalArgumentException - If the file length is greater than 232-1 since the maximum size of an array is (max)int - 1.
    • getYNFromUser

      public static boolean getYNFromUser(String question) throws IOException
      Ask a question of the User on stdout and wait for a response.

      Take a positive as either "", "y|Y" or "yes|YES" or any case value for yes and return true. Everything else is a no and returns false.
      Parameters:
      question - The question to ask.
      Returns:
      true if they responded with y|yes etc...return false otherwise.
      Throws:
      IOException - If there is an io problem.