Class FileUtils


  • public class FileUtils
    extends java.lang.Object
    Utils for dealing with files.
    • Constructor Summary

      Constructors 
      Constructor Description
      FileUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void appendUtf8​(java.io.File file, java.lang.CharSequence text)  
      static void copyFile​(java.io.File from, java.io.File to)
      Copies a file to another location.
      static void copyFile​(java.lang.String fromFilename, java.lang.String toFilename)
      Copies a file to another location.
      static void deleteDirRecursive​(java.io.File dir)
      Deletes all files within the given directory recursively (including subdirectories).
      static boolean deleteDirRecursiveBestEffort​(java.io.File dir)
      Deletes all files within the given directory recursively (including subdirectories).
      private static java.io.File deleteDirRecursiveInternal​(java.io.File dir, boolean failFast)  
      static java.lang.String getMd5​(java.io.File file)  
      static java.lang.String getSha1​(java.io.File file)  
      static java.lang.String getSha256​(java.io.File file)  
      static byte[] readBytes​(java.io.File file)  
      static java.lang.String readChars​(java.io.File file, java.lang.String charset)  
      static <T> T readObject​(java.io.File file)
      To read an object in a quick & dirty way.
      static java.lang.String readUtf8​(java.io.File file)  
      static void updateChecksum​(java.io.File file, java.util.zip.Checksum checksum)  
      static void writeBytes​(java.io.File file, byte[] content)  
      static void writeChars​(java.io.File file, java.lang.String charset, java.lang.CharSequence text, boolean apppend)  
      static void writeObject​(java.io.File file, java.lang.Object object)
      To store an object in a quick & dirty way.
      static void writeUtf8​(java.io.File file, java.lang.CharSequence text)  
      • Methods inherited from class java.lang.Object

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

      • FileUtils

        public FileUtils()
    • Method Detail

      • readBytes

        public static byte[] readBytes​(java.io.File file)
                                throws java.io.IOException
        Throws:
        java.io.IOException
      • writeBytes

        public static void writeBytes​(java.io.File file,
                                      byte[] content)
                               throws java.io.IOException
        Throws:
        java.io.IOException
      • readUtf8

        public static java.lang.String readUtf8​(java.io.File file)
                                         throws java.io.IOException
        Throws:
        java.io.IOException
      • readChars

        public static java.lang.String readChars​(java.io.File file,
                                                 java.lang.String charset)
                                          throws java.io.IOException
        Throws:
        java.io.IOException
      • writeUtf8

        public static void writeUtf8​(java.io.File file,
                                     java.lang.CharSequence text)
                              throws java.io.IOException
        Throws:
        java.io.IOException
      • appendUtf8

        public static void appendUtf8​(java.io.File file,
                                      java.lang.CharSequence text)
                               throws java.io.IOException
        Throws:
        java.io.IOException
      • writeChars

        public static void writeChars​(java.io.File file,
                                      java.lang.String charset,
                                      java.lang.CharSequence text,
                                      boolean apppend)
                               throws java.io.IOException
        Throws:
        java.io.IOException
      • copyFile

        public static void copyFile​(java.io.File from,
                                    java.io.File to)
                             throws java.io.IOException
        Copies a file to another location.
        Throws:
        java.io.IOException
      • copyFile

        public static void copyFile​(java.lang.String fromFilename,
                                    java.lang.String toFilename)
                             throws java.io.IOException
        Copies a file to another location.
        Throws:
        java.io.IOException
      • readObject

        public static <T> T readObject​(java.io.File file)
                                throws java.io.IOException,
                                       java.lang.ClassNotFoundException
        To read an object in a quick & dirty way. Prepare to handle failures when object serialization changes!
        Throws:
        java.io.IOException
        java.lang.ClassNotFoundException
      • writeObject

        public static void writeObject​(java.io.File file,
                                       java.lang.Object object)
                                throws java.io.IOException
        To store an object in a quick & dirty way.
        Throws:
        java.io.IOException
      • getMd5

        public static java.lang.String getMd5​(java.io.File file)
                                       throws java.io.IOException
        Returns:
        MD5 digest (32 hex characters).
        Throws:
        java.io.IOException
      • getSha1

        public static java.lang.String getSha1​(java.io.File file)
                                        throws java.io.IOException
        Returns:
        SHA-1 digest (40 hex characters).
        Throws:
        java.io.IOException
      • getSha256

        public static java.lang.String getSha256​(java.io.File file)
                                          throws java.io.IOException
        Returns:
        SHA-256 digest (64 hex characters).
        Throws:
        java.io.IOException
      • updateChecksum

        public static void updateChecksum​(java.io.File file,
                                          java.util.zip.Checksum checksum)
                                   throws java.io.IOException
        Throws:
        java.io.IOException
      • deleteDirRecursive

        public static void deleteDirRecursive​(java.io.File dir)
                                       throws java.io.IOException
        Deletes all files within the given directory recursively (including subdirectories). This method fails fast with an IOException on the first file that could not be deleted.
        Throws:
        java.io.IOException
      • deleteDirRecursiveBestEffort

        public static boolean deleteDirRecursiveBestEffort​(java.io.File dir)
        Deletes all files within the given directory recursively (including subdirectories). It will try to delete as many files as possible (best effort) and will not fail on files that couldn't be deleted.
        Returns:
        true if the given dir does not exist anymore on completion of this method
      • deleteDirRecursiveInternal

        private static java.io.File deleteDirRecursiveInternal​(java.io.File dir,
                                                               boolean failFast)
        Returns:
        File that could not be deleted (fail fast mode only)