Class FileTool


  • public class FileTool
    extends java.lang.Object
    File management toolbox
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  FileTool.DeleteOnCloseInputStream
      Custom FileInputFile with auto-deletion on close
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int BUFFER_SIZE  
    • Method Summary

      Modifier and Type Method Description
      static void concatFiles​(java.io.InputStream[] is, java.io.OutputStream out)
      Concat all input stream into output stream, flush and close
      static void copyFile​(java.io.File src, java.io.File dest, boolean override)
      File copy
      static void copyFile​(java.lang.String srcPath, java.lang.String destPath, boolean override)
      File copy
      static boolean deleteFileOrDir​(java.io.File file)
      Delete file or dir
      static boolean deleteFileOrDir​(java.lang.String path)
      Delete file or dir
      static java.util.List<java.lang.String> getAllFiles​(java.io.File dir)
      List all files of dir
      static java.util.List<java.lang.String> getAllFiles​(java.io.File dir, java.lang.String pattern, boolean sort)
      List all files of dir with optional pattern (returns an empty list if directory does not exists)
      static java.util.List<java.lang.String> getAllFiles​(java.lang.String dir)
      List all files of dir
      static java.util.List<java.lang.String> getAllFiles​(java.lang.String dir, java.lang.String pattern, boolean sort)
      List all files of dir with optional pattern (returns an empty list if directory does not exists)
      static java.lang.String getExtension​(java.lang.String path)
      Get file extension from path
      static java.lang.String getMIMEType​(java.io.File file)
      Get MIME type from file
      static java.lang.String getMIMEType​(java.lang.String path)
      Get MIME type from file path
      static java.lang.String getName​(java.lang.String path)
      Get file name from path
      static boolean mkdir​(java.io.File dir, boolean deleteIfExists)
      Create directory
      static boolean mkdir​(java.lang.String dir, boolean deleteIfExists)
      Create directory
      static java.lang.String readFile​(java.io.File file)
      Read file as String
      static java.lang.String readFile​(java.io.File file, java.lang.String encoding)
      Read file as String
      static java.lang.String readFile​(java.lang.String path)
      Read file as String
      static java.lang.String readFile​(java.lang.String path, java.lang.String encoding)
      Read file as String
      static java.lang.String readFileAsBase64​(java.io.File file)
      Read file as base64 string
      static java.lang.String readFileAsBase64​(java.lang.String path)
      Read file as base64 string
      static byte[] readFileAsBytes​(java.io.File file)
      Read file as byte array
      static byte[] readFileAsBytes​(java.lang.String path)
      Read file as byte array
      static java.util.List<java.lang.String> readFileByLine​(byte[] data)
      Read byte array line by line
      static java.util.List<java.lang.String> readFileByLine​(byte[] data, java.lang.String encoding)
      Read byte array line by line
      static java.util.List<java.lang.String> readFileByLine​(java.io.File file)
      Read file line by line
      static java.util.List<java.lang.String> readFileByLine​(java.io.File file, java.lang.String encoding)
      Read file line by line
      static java.util.List<java.lang.String> readFileByLine​(java.io.File file, java.lang.String regex, int maxLines, java.lang.String encoding)
      Read file line by line applying regex filter
      static java.util.List<java.lang.String> readFileByLine​(java.io.InputStream is)
      Read input stream line by line
      static java.util.List<java.lang.String> readFileByLine​(java.io.InputStream is, java.lang.String encoding)
      Read input stream line by line
      static java.util.List<java.lang.String> readFileByLine​(java.io.InputStream is, java.lang.String regex, int maxLines, java.lang.String encoding)
      Read file line by line applying regex filter
      static java.util.List<java.lang.String> readFileByLine​(java.lang.String path)
      Read file line by line
      static java.util.List<java.lang.String> readFileByLine​(java.lang.String path, java.lang.String encoding)
      Read file line by line
      static java.util.List<java.lang.String> readFileByLine​(java.lang.String path, java.lang.String regex, int maxLines, java.lang.String encoding)
      Read file line by line applying regex filter
      static long sizeOfDirectory​(java.io.File path)
      Directory total size in bytes
      static long sizeOfDirectory​(java.lang.String path)
      Directory total size in bytes
      static boolean writeFile​(java.io.File file, byte[] data)
      Write file with byte array data
      static boolean writeFile​(java.io.File file, java.lang.String data)
      Write file with string data
      static boolean writeFile​(java.io.File file, java.lang.String data, java.lang.String encoding)
      Write file with string data
      static void writeFile​(java.io.InputStream is, java.io.File f)
      Copy input stream into output stream, flush and close
      static void writeFile​(java.io.InputStream is, java.io.OutputStream out)
      Copy input stream into output stream, flush and close
      static void writeFile​(java.io.InputStream is, java.io.OutputStream out, long limit)
      Copy input stream into output stream, flush and close
      static boolean writeFile​(java.io.OutputStream os, byte[] data)
      Write file with byte array data
      static boolean writeFile​(java.lang.String path, byte[] data)
      Write file with byte array data
      static boolean writeFile​(java.lang.String path, java.lang.String data)
      Write file with string data
      static boolean writeFile​(java.lang.String path, java.lang.String data, java.lang.String encoding)
      Write file with string data
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • readFile

        public static java.lang.String readFile​(java.lang.String path)
                                         throws java.io.IOException
        Read file as String
        Parameters:
        path - File path
        Throws:
        java.io.IOException
      • readFile

        public static java.lang.String readFile​(java.lang.String path,
                                                java.lang.String encoding)
                                         throws java.io.IOException
        Read file as String
        Parameters:
        path - File path
        encoding - File encoding
        Throws:
        java.io.IOException
      • readFile

        public static java.lang.String readFile​(java.io.File file)
                                         throws java.io.IOException
        Read file as String
        Parameters:
        file - File
        Throws:
        java.io.IOException
      • readFile

        public static java.lang.String readFile​(java.io.File file,
                                                java.lang.String encoding)
                                         throws java.io.IOException
        Read file as String
        Parameters:
        file - File
        encoding - File encoding
        Throws:
        java.io.IOException
      • readFileAsBytes

        public static byte[] readFileAsBytes​(java.lang.String path)
                                      throws java.io.IOException
        Read file as byte array
        Parameters:
        path - File path
        Throws:
        java.io.IOException
      • readFileAsBytes

        public static byte[] readFileAsBytes​(java.io.File file)
                                      throws java.io.IOException
        Read file as byte array
        Parameters:
        file - File
        Throws:
        java.io.IOException
      • readFileAsBase64

        public static java.lang.String readFileAsBase64​(java.lang.String path)
                                                 throws java.io.IOException
        Read file as base64 string
        Parameters:
        path - File path
        Throws:
        java.io.IOException
      • readFileAsBase64

        public static java.lang.String readFileAsBase64​(java.io.File file)
                                                 throws java.io.IOException
        Read file as base64 string
        Parameters:
        file - File
        Throws:
        java.io.IOException
      • readFileByLine

        public static java.util.List<java.lang.String> readFileByLine​(java.lang.String path)
                                                               throws java.io.IOException
        Read file line by line
        Parameters:
        path - File path
        Throws:
        java.io.IOException
      • readFileByLine

        public static java.util.List<java.lang.String> readFileByLine​(java.lang.String path,
                                                                      java.lang.String encoding)
                                                               throws java.io.IOException
        Read file line by line
        Parameters:
        path - File path
        encoding - File encoding
        Throws:
        java.io.IOException
      • readFileByLine

        public static java.util.List<java.lang.String> readFileByLine​(java.io.File file)
                                                               throws java.io.IOException
        Read file line by line
        Parameters:
        file - File
        Throws:
        java.io.IOException
      • readFileByLine

        public static java.util.List<java.lang.String> readFileByLine​(java.io.File file,
                                                                      java.lang.String encoding)
                                                               throws java.io.IOException
        Read file line by line
        Parameters:
        file - File
        encoding - File encoding
        Throws:
        java.io.IOException
      • readFileByLine

        public static java.util.List<java.lang.String> readFileByLine​(byte[] data)
                                                               throws java.io.IOException
        Read byte array line by line
        Parameters:
        data - Byte array
        Throws:
        java.io.IOException
      • readFileByLine

        public static java.util.List<java.lang.String> readFileByLine​(byte[] data,
                                                                      java.lang.String encoding)
                                                               throws java.io.IOException
        Read byte array line by line
        Parameters:
        data - Byte array
        encoding - File encoding
        Throws:
        java.io.IOException
      • readFileByLine

        public static java.util.List<java.lang.String> readFileByLine​(java.io.InputStream is)
                                                               throws java.io.IOException
        Read input stream line by line
        Parameters:
        is - Input stream
        Throws:
        java.io.IOException
      • readFileByLine

        public static java.util.List<java.lang.String> readFileByLine​(java.io.InputStream is,
                                                                      java.lang.String encoding)
                                                               throws java.io.IOException
        Read input stream line by line
        Parameters:
        is - Input stream
        encoding - File encoding
        Throws:
        java.io.IOException
      • readFileByLine

        public static java.util.List<java.lang.String> readFileByLine​(java.lang.String path,
                                                                      java.lang.String regex,
                                                                      int maxLines,
                                                                      java.lang.String encoding)
                                                               throws java.io.IOException
        Read file line by line applying regex filter
        Parameters:
        path - File path
        regex - Regex to apply (if null or empty no filtering is done)
        maxLines - Maximum nb lines returned (>0 means first lines <0 means last lines)
        encoding - File encoding
        Throws:
        java.io.IOException
      • readFileByLine

        public static java.util.List<java.lang.String> readFileByLine​(java.io.File file,
                                                                      java.lang.String regex,
                                                                      int maxLines,
                                                                      java.lang.String encoding)
                                                               throws java.io.IOException
        Read file line by line applying regex filter
        Parameters:
        file - File
        regex - Regex to apply (if null or empty no filtering is done)
        maxLines - Maximum nb lines returned (0 means all lines, >0 means first lines, <0 means last lines)
        encoding - File encoding
        Throws:
        java.io.IOException
      • readFileByLine

        public static java.util.List<java.lang.String> readFileByLine​(java.io.InputStream is,
                                                                      java.lang.String regex,
                                                                      int maxLines,
                                                                      java.lang.String encoding)
                                                               throws java.io.IOException
        Read file line by line applying regex filter
        Parameters:
        is - Input stream
        regex - Regex to apply (if null or empty no filtering is done)
        maxLines - Maximum nb lines returned (0 means all lines, >0 means first lines, <0 means last lines)
        encoding - File encoding
        Throws:
        java.io.IOException
      • writeFile

        public static boolean writeFile​(java.lang.String path,
                                        java.lang.String data)
                                 throws java.io.IOException
        Write file with string data
        Parameters:
        path - File path
        data - String data
        Returns:
        true if file generation is OK
        Throws:
        java.io.IOException
      • writeFile

        public static boolean writeFile​(java.lang.String path,
                                        java.lang.String data,
                                        java.lang.String encoding)
                                 throws java.io.IOException
        Write file with string data
        Parameters:
        path - File path
        data - String data
        encoding - File encoding
        Returns:
        true if file generation is OK
        Throws:
        java.io.IOException
      • writeFile

        public static boolean writeFile​(java.io.File file,
                                        java.lang.String data)
                                 throws java.io.IOException
        Write file with string data
        Parameters:
        file - File
        data - String data
        Returns:
        true if file generation is OK
        Throws:
        java.io.IOException
      • writeFile

        public static boolean writeFile​(java.io.File file,
                                        java.lang.String data,
                                        java.lang.String encoding)
                                 throws java.io.IOException
        Write file with string data
        Parameters:
        file - File
        data - String data
        encoding - File encoding
        Returns:
        true if file generation is OK
        Throws:
        java.io.IOException
      • writeFile

        public static boolean writeFile​(java.lang.String path,
                                        byte[] data)
                                 throws java.io.IOException
        Write file with byte array data
        Parameters:
        path - File path
        data - Byte array data
        Returns:
        true if file generation is OK
        Throws:
        java.io.IOException
      • writeFile

        public static boolean writeFile​(java.io.File file,
                                        byte[] data)
                                 throws java.io.IOException
        Write file with byte array data
        Parameters:
        file - File
        data - Byte array data
        Returns:
        true if file generation is OK
        Throws:
        java.io.IOException
      • writeFile

        public static boolean writeFile​(java.io.OutputStream os,
                                        byte[] data)
                                 throws java.io.IOException
        Write file with byte array data
        Parameters:
        os - Output stream
        data - Byte array data
        Returns:
        true if file generation is OK
        Throws:
        java.io.IOException
      • copyFile

        public static void copyFile​(java.io.File src,
                                    java.io.File dest,
                                    boolean override)
                             throws java.io.IOException
        File copy
        Parameters:
        src - Source file
        dest - Destination file
        override - True to override if exists
        Throws:
        java.io.IOException
      • copyFile

        public static void copyFile​(java.lang.String srcPath,
                                    java.lang.String destPath,
                                    boolean override)
                             throws java.io.IOException
        File copy
        Parameters:
        srcPath - Source file path
        destPath - Destination file path
        override - True to override if exists
        Throws:
        java.io.IOException
      • writeFile

        public static void writeFile​(java.io.InputStream is,
                                     java.io.File f)
                              throws java.io.FileNotFoundException
        Copy input stream into output stream, flush and close
        Parameters:
        is - input stream
        f - output file (override)
        Throws:
        java.io.FileNotFoundException
      • writeFile

        public static void writeFile​(java.io.InputStream is,
                                     java.io.OutputStream out)
        Copy input stream into output stream, flush and close
        Parameters:
        is - input stream
        out - output stream
      • concatFiles

        public static void concatFiles​(java.io.InputStream[] is,
                                       java.io.OutputStream out)
        Concat all input stream into output stream, flush and close
        Parameters:
        is - input streams
        out - output stream
      • writeFile

        public static void writeFile​(java.io.InputStream is,
                                     java.io.OutputStream out,
                                     long limit)
                              throws FileSizeException
        Copy input stream into output stream, flush and close
        Parameters:
        is - input stream
        out - output stream
        limit - max size in Mo
        Throws:
        FileSizeException
      • deleteFileOrDir

        public static boolean deleteFileOrDir​(java.lang.String path)
        Delete file or dir
        Parameters:
        path - Path
      • deleteFileOrDir

        public static boolean deleteFileOrDir​(java.io.File file)
        Delete file or dir
        Parameters:
        file - File
      • mkdir

        public static boolean mkdir​(java.io.File dir,
                                    boolean deleteIfExists)
        Create directory
        Parameters:
        dir - Dir
        deleteIfExists - Delete content is dir exists
      • mkdir

        public static boolean mkdir​(java.lang.String dir,
                                    boolean deleteIfExists)
        Create directory
        Parameters:
        dir - Dir
        deleteIfExists - Delete content is dir exists
      • getName

        public static java.lang.String getName​(java.lang.String path)
        Get file name from path
      • getExtension

        public static java.lang.String getExtension​(java.lang.String path)
        Get file extension from path
      • sizeOfDirectory

        public static long sizeOfDirectory​(java.lang.String path)
        Directory total size in bytes
        Parameters:
        path - Start path
      • sizeOfDirectory

        public static long sizeOfDirectory​(java.io.File path)
        Directory total size in bytes
        Parameters:
        path - Start path
      • getAllFiles

        public static java.util.List<java.lang.String> getAllFiles​(java.lang.String dir)
                                                            throws java.io.IOException
        List all files of dir
        Parameters:
        dir - Directory
        Throws:
        java.io.IOException
      • getAllFiles

        public static java.util.List<java.lang.String> getAllFiles​(java.io.File dir)
                                                            throws java.io.IOException
        List all files of dir
        Parameters:
        dir - Directory
        Throws:
        java.io.IOException
      • getAllFiles

        public static java.util.List<java.lang.String> getAllFiles​(java.lang.String dir,
                                                                   java.lang.String pattern,
                                                                   boolean sort)
                                                            throws java.io.IOException
        List all files of dir with optional pattern (returns an empty list if directory does not exists)
        Parameters:
        dir - Directory
        pattern - Optional regular expression pattern to match (e.g. "^.*\\.txt$")
        sort - Sort list?
        Throws:
        java.io.IOException
      • getAllFiles

        public static java.util.List<java.lang.String> getAllFiles​(java.io.File dir,
                                                                   java.lang.String pattern,
                                                                   boolean sort)
                                                            throws java.io.IOException
        List all files of dir with optional pattern (returns an empty list if directory does not exists)
        Parameters:
        dir - Directory
        pattern - Optional regular expression pattern to match (e.g. "^.*\\.txt$")
        sort - Sort list?
        Throws:
        java.io.IOException
      • getMIMEType

        public static java.lang.String getMIMEType​(java.lang.String path)
        Get MIME type from file path
        Parameters:
        path - Path
      • getMIMEType

        public static java.lang.String getMIMEType​(java.io.File file)
        Get MIME type from file
        Parameters:
        file - File