Class IOUtils

java.lang.Object
com.infinitekind.util.IOUtils

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

    Fields 
    Modifier and Type Field Description
    static java.lang.Class ATOMIC_RENAME_CLASS  
    static java.lang.String ATOMIC_RENAME_METHOD_NAME  
  • Constructor Summary

    Constructors 
    Constructor Description
    IOUtils()  
  • Method Summary

    Modifier and Type Method Description
    static void copy​(java.io.File src, java.io.File dest)  
    static void copyDirectoryContents​(java.io.File src, java.io.File dst)
    Copy everything in the source folder to the destination directory.
    static void copyFile​(java.io.File fromFile, java.io.File toFile)
    Copy the contents of fromFile to toFile.
    static void copyFolder​(java.io.File src, java.io.File dest)
    Copy the contents of a folder into another.
    static long copyStream​(java.io.InputStream in, java.io.OutputStream out)
    Copy all of the bytes from one inputstream to another, returning the number of bytes copied.
    static java.io.File createTempFolder()  
    static void delete​(java.io.File file)  
    static void deleteFile​(java.io.File file)
    Delete a file immediately.
    static boolean deleteFolder​(java.io.File folder)
    Delete recursively a directory and all its content.
    static java.io.File getUnusedFileNameWithBaseAndExtension​(java.io.File containerFolder, java.lang.String baseName, java.lang.String extension)
    Return a unique, non-existent file with the given name and name and file extension.
    static boolean isDirectoryEmpty​(java.io.File dir)
    Returns whether a directory is empty or not.
    static java.lang.String openZip​(java.io.File fileToOpen, java.lang.String path)
    Unzip a file in given path.
    static byte[] readFully​(java.io.InputStream in)
    Read the entire contents of the given stream into a byte array.
    static java.lang.String readline​(java.io.InputStream in)
    Read a single line from the given InputStream, assuming the text is UTF-8 encoded.
    static java.util.List<java.lang.String> readlines​(java.io.InputStream in)
    Return a list containing the lines read from the given UTF8-encoded InputStream.
    static void renameFilesMatching​(java.lang.String oldName, java.lang.String newName, java.io.File dir)
    This method will rename files matching oldName to newName in the given dir
    static void renameMostlyAtomicallyWithoutNIO​(java.io.File oldFilePath, java.io.File destFilePath)  
    static boolean unzip​(java.io.InputStream in, java.io.File destinationFolder)
    Unzip a zip file from the given input stream and into the given folder.
    static void writeAtomically​(byte[] dataToWrite, java.io.File outputFile)
    Atomically write the given data to the outputFile.
    static void writeAtomically​(java.lang.String stringToWrite, java.io.File outputFile)
    Atomically write the given string to the outputFile in UTF8.
    static void zipRecursively​(java.util.zip.ZipOutputStream zipOut, java.io.File fileToZip, java.io.FilenameFilter filter)
    Recursively zip the given file or directory, only adding files matching the filter (if any).

    Methods inherited from class java.lang.Object

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

  • Constructor Details

  • Method Details

    • getUnusedFileNameWithBaseAndExtension

      public static java.io.File getUnusedFileNameWithBaseAndExtension​(java.io.File containerFolder, java.lang.String baseName, java.lang.String extension)
      Return a unique, non-existent file with the given name and name and file extension. This will name files filename.extension, filename 2.extension, filename 3.extension, etc.
    • copy

      public static void copy​(java.io.File src, java.io.File dest) throws java.io.IOException
      Throws:
      java.io.IOException
    • delete

      public static void delete​(java.io.File file)
    • copyStream

      public static long copyStream​(java.io.InputStream in, java.io.OutputStream out) throws java.io.IOException
      Copy all of the bytes from one inputstream to another, returning the number of bytes copied. This does not close or flush the streams when finished. As of build 1859 if the output stream is null then the bytes read from the input stream are discarded (/dev/null)
      Throws:
      java.io.IOException
      Since:
      build 817
    • readlines

      public static java.util.List<java.lang.String> readlines​(java.io.InputStream in) throws java.io.IOException
      Return a list containing the lines read from the given UTF8-encoded InputStream. This will close the inputstream when finished, so no need to clean up after yourself.
      Parameters:
      in - the input stream from which to read the lines
      Returns:
      the list of strings read from the input stream
      Throws:
      java.io.IOException
      Since:
      Moneydance 2017 build 1523
    • readFully

      public static byte[] readFully​(java.io.InputStream in) throws java.io.IOException
      Read the entire contents of the given stream into a byte array. This will close the inputstream when finished, so no need to clean up after yourself.
      Parameters:
      in - the input stream from which to read the bytes
      Returns:
      the list of strings read from the input stream
      Throws:
      java.io.IOException
      Since:
      Moneydance 2017 build 1527
    • createTempFolder

      public static java.io.File createTempFolder()
    • copyFile

      public static void copyFile​(java.io.File fromFile, java.io.File toFile) throws java.io.IOException
      Copy the contents of fromFile to toFile.
      Throws:
      java.io.IOException
    • copyFolder

      public static void copyFolder​(java.io.File src, java.io.File dest) throws java.io.IOException
      Copy the contents of a folder into another.
      Parameters:
      src - the folder where to copy the files from
      dest - the folder where to copy the file to
      Throws:
      java.io.IOException
    • deleteFolder

      public static boolean deleteFolder​(java.io.File folder)
      Delete recursively a directory and all its content.
      Parameters:
      folder - the folder to delete
    • deleteFile

      public static void deleteFile​(java.io.File file)
      Delete a file immediately. If the operation doesn't succeed, try to delete it on exit.
      Parameters:
      file -
    • renameFilesMatching

      public static void renameFilesMatching​(java.lang.String oldName, java.lang.String newName, java.io.File dir)
      This method will rename files matching oldName to newName in the given dir
      Parameters:
      oldName -
      newName -
    • copyDirectoryContents

      public static void copyDirectoryContents​(java.io.File src, java.io.File dst) throws java.io.IOException
      Copy everything in the source folder to the destination directory. Does not copy the src directory itself, only the content.
      Parameters:
      src -
      dst -
      Throws:
      java.io.IOException
    • openZip

      public static java.lang.String openZip​(java.io.File fileToOpen, java.lang.String path)
      Unzip a file in given path. Returns file parent, which is useful in dealing with backup files.
      Since:
      Moneydance 2015 (build 1231)
    • unzip

      public static boolean unzip​(java.io.InputStream in, java.io.File destinationFolder)
      Unzip a zip file from the given input stream and into the given folder. Returns true if the unzipping process was successful.
      Since:
      Moneydance 2019.4 (build 1887)
    • zipRecursively

      public static void zipRecursively​(java.util.zip.ZipOutputStream zipOut, java.io.File fileToZip, java.io.FilenameFilter filter) throws java.io.IOException, java.util.zip.ZipException
      Recursively zip the given file or directory, only adding files matching the filter (if any).
      Throws:
      java.io.IOException
      java.util.zip.ZipException
      Since:
      Moneydance 2014 (build 915)
    • isDirectoryEmpty

      public static boolean isDirectoryEmpty​(java.io.File dir)
      Returns whether a directory is empty or not.
      Parameters:
      dir -
      Returns:
    • writeAtomically

      public static void writeAtomically​(java.lang.String stringToWrite, java.io.File outputFile) throws java.io.IOException
      Atomically write the given string to the outputFile in UTF8. This works by first writing the UTF-8 converted bytes to a temporary file and then using the File.rename() method to atomically move that file into place. It is a good idea to put a synchronized block around any code that calls this for the same file.
      Throws:
      java.io.IOException
    • renameMostlyAtomicallyWithoutNIO

      public static void renameMostlyAtomicallyWithoutNIO​(java.io.File oldFilePath, java.io.File destFilePath) throws java.io.IOException
      Throws:
      java.io.IOException
    • writeAtomically

      public static void writeAtomically​(byte[] dataToWrite, java.io.File outputFile) throws java.io.IOException
      Atomically write the given data to the outputFile. This works by first writing to a temporary file and then using the File.rename() method to atomically move that file into place. It is a good idea to put a synchronized block around any code that calls this for the same file.
      Throws:
      java.io.IOException
    • readline

      public static java.lang.String readline​(java.io.InputStream in) throws java.io.IOException
      Read a single line from the given InputStream, assuming the text is UTF-8 encoded. Returns null if an EOF is found before a newline character.
      Throws:
      java.io.IOException
      Since:
      Moneydance 2017 build 1696