Class FileUtil


  • public class FileUtil
    extends Object
    This class manages I/O operations with files.
    Version:
    4.0
    Author:
    Marco Meschieri - LogicalDOC
    • Constructor Detail

      • FileUtil

        public FileUtil()
    • Method Detail

      • writeFile

        public static void writeFile​(InputStream in,
                                     String filepath)
                              throws Exception
        Writes a stream to a file and then closes the same stream.
        Parameters:
        in - the input stream
        filepath - the target file path
        Throws:
        Exception - raised in case of I/O error
      • writeFile

        public static void writeFile​(String text,
                                     String filepath)
      • appendFile

        public static void appendFile​(String text,
                                      String filepath)
      • computeDigest

        public static String computeDigest​(File file)
        This method calculates the digest of a file using the algorithm SHA-1.
        Parameters:
        file - The file for which will be computed the digest
        Returns:
        digest
      • computeDigest

        public static String computeDigest​(String src)
        This method calculates the digest of a string using the algorithm SHA-1.
        Parameters:
        src - The string for which will be computed the digest
        Returns:
        digest
      • computeSha1Hash

        public static byte[] computeSha1Hash​(File file)
        This method calculates the digest of a file using the algorithm SHA-1.
        Parameters:
        file - The file for which will be computed the digest
        Returns:
        digest
      • computeSha1Hash

        public static byte[] computeSha1Hash​(InputStream is)
        This method calculates the digest of a inputStram content using the algorithm SHA-1.
        Parameters:
        is - The content of which will be computed the digest
        Returns:
        digest
      • copyResource

        public static void copyResource​(String resourceName,
                                        File out)
                                 throws IOException
        Writes the specified classpath resource into a file
        Parameters:
        resourceName - Fully qualified resource name
        out - The output file
        Throws:
        IOException - if the copy caused an error
      • getFolderSize

        public static long getFolderSize​(File folder)
        Computes the folder size as the sum of all files directly and indirectly contained.
        Parameters:
        folder - the folder to calculate
        Returns:
        the sum of the sizes of all contained files expressed in bytes
      • getDisplaySize

        public static String getDisplaySize​(long size,
                                            String language)
        Renders a file size in a more readable behaviour taking into account the user locale. Depending on the size, the result will be presented in the following measure units: GB, MB, KB or Bytes
        Parameters:
        size - Size to be rendered
        language - The language for the format symbols
        Returns:
        the size as human readable text
      • getDisplaySizeKB

        public static String getDisplaySizeKB​(long size,
                                              String language)
        Renders a file size in a more readable behaviour taking into account the user locale. The size is always rendered in the KB(kilobyte) measure unit.
        Parameters:
        size - Size to be rendered
        language - The language for the format symbols
        Returns:
        the size in KB as human readable text
      • matches

        public static boolean matches​(String filename,
                                      String[] includes,
                                      String[] excludes)
        Check if a given filename matches the includes and not the excludes
        Parameters:
        filename - The filename to consider
        includes - list of includes expressions (eg. *.doc,*dummy*)
        excludes - list of excludes expressions (eg. *.doc,*dummy*)
        Returns:
        true only if the passed filename matches the includes and not the excludes
      • matches

        public static boolean matches​(String filename,
                                      String includes,
                                      String excludes)
        Check if a given filename matches the includes and not the excludes
        Parameters:
        filename - The filename to consider
        includes - comma-separated list of includes expressions (eg. *.doc,*dummy*)
        excludes - comma-separated list of excludes expressions (eg. *.doc,*dummy*)
        Returns:
        true only if the passed filename matches the includes and not the excludes
      • writeUTF8

        public static void writeUTF8​(String content,
                                     File file,
                                     boolean append)
      • toByteArray

        public static byte[] toByteArray​(File file)
      • copy

        public static void copy​(File input,
                                File output,
                                long offset)
                         throws IOException
        Copies the input file into the output at the given offset
        Parameters:
        input - the file to copy
        output - the target file to copy to
        offset - an offset to use in copying from the input expressed in number of bytes
        Throws:
        IOException - raised in case of error
      • copyFile

        public static void copyFile​(File source,
                                    File target)
                             throws IOException
        Quickest way to copy a file in Java, makes use of NIO buffer.
        Parameters:
        source - the source file to copy
        target - the target file
        Throws:
        IOException - if the copy resulted in an error
      • strongDelete

        public static void strongDelete​(File file)
      • isInsideFolder

        public static boolean isInsideFolder​(File folder,
                                             File file)
        Checks if file is in side folder at any level.
        Parameters:
        folder - the folder to inspect
        file - the file to check
        Returns:
        true if file is in side folder