Class FileUtils


  • public class FileUtils
    extends Object
    A set of utility methods for manipulating files
    Author:
    Eyal Schneider
    • Constructor Detail

      • FileUtils

        public FileUtils()
    • Method Detail

      • getLinesFromClasspath

        public static List<String> getLinesFromClasspath​(String filename)
                                                  throws IOException
        Utility for loading the lines of a classpath file (UTF8).
        Parameters:
        filename - A filename (in classpath). Must refer to a textual file in UTF8.
        Returns:
        The list of strings, each representing a line
        Throws:
        IOException
      • getLinesFromFile

        public static List<String> getLinesFromFile​(File file,
                                                    Charset charset,
                                                    FileReadOptions options)
                                             throws IOException
        Utility for loading the lines of a local text file
        Parameters:
        file - The file to read the lines from
        charset - The charset the text file is encoded with
        options - the file reading options
        Returns:
        The list of strings, each representing a line
        Throws:
        IOException
      • getLinesFromFile

        public static List<String> getLinesFromFile​(File file)
                                             throws IOException
        Utility for loading the lines of a local text file (UTF8).
        Parameters:
        file - The file to read the lines from (UTF8)
        Returns:
        The list of strings, each representing a line
        Throws:
        IOException
      • getLinesFromFileAsSet

        public static Set<String> getLinesFromFileAsSet​(File file,
                                                        Charset charset,
                                                        FileReadOptions options)
                                                 throws IOException
        Utility for loading the lines of a local text file into a set
        Parameters:
        file - The file to read the lines from
        charset - The charset the text file is encoded with
        options - the file reading options
        Returns:
        The set of strings, each representing a unique line value
        Throws:
        IOException
      • getLinesFromFileAsSet

        public static Set<String> getLinesFromFileAsSet​(File file)
                                                 throws IOException
        Utility for loading the lines of a local text file (UTF8) into a set
        Parameters:
        file - The file to read the lines from (UTF8)
        Returns:
        The set of strings, each representing a unique line value
        Throws:
        IOException
      • getListFromReader

        public static List<String> getListFromReader​(Reader reader)
                                              throws IOException
        Utility for loading the lines from a reader
        Parameters:
        reader - The reader to read from. No need to be a buffered reader. Closed at the end of this call, regardless of IO exceptions.
        Returns:
        The list of strings, each representing a line
        Throws:
        IOException
      • getSetFromReader

        public static Set<String> getSetFromReader​(Reader reader)
                                            throws IOException
        Utility for loading the lines from a reader, into a deduped set
        Parameters:
        reader - The reader to read from. No need to be a buffered reader. Closed at the end of this call, regardless of IO exceptions.
        Returns:
        The set of strings, each representing a line
        Throws:
        IOException
      • getJSONFromClasspath

        public static net.minidev.json.JSONObject getJSONFromClasspath​(String filename)
                                                                throws IOException,
                                                                       net.minidev.json.parser.ParseException
        Utility for loading JSON objects from classpath files
        Parameters:
        filename - A filename (in classpath)
        Returns:
        The json object as read from the given file
        Throws:
        IOException
        net.minidev.json.parser.ParseException
      • getJSONListFromClasspath

        public static List<net.minidev.json.JSONObject> getJSONListFromClasspath​(String filename)
                                                                          throws IOException,
                                                                                 net.minidev.json.parser.ParseException
        Utility for loading a list of JSON objects from classpath files, assuming that every line has one JSON object.
        Parameters:
        filename - A filename (in classpath)
        Returns:
        The list of json objects, one per line in the file
        Throws:
        IOException
        net.minidev.json.parser.ParseException
      • getStringFromClasspath

        public static String getStringFromClasspath​(String filename)
                                             throws IOException
        Utility for loading string from classpath files
        Parameters:
        filename - A filename (in classpath)
        Returns:
        The string as read from the given file
        Throws:
        IOException
      • getJSONFromInpuStream

        public static net.minidev.json.JSONObject getJSONFromInpuStream​(InputStream is)
                                                                 throws IOException,
                                                                        net.minidev.json.parser.ParseException
        Parameters:
        is - An input stream to read the json from. Expected to have a single valid json object. The stream is closed by this method.
        Returns:
        The json object as read from the given stream
        Throws:
        IOException
        net.minidev.json.parser.ParseException
      • getJSONFromFile

        public static net.minidev.json.JSONObject getJSONFromFile​(File file)
                                                           throws IOException,
                                                                  net.minidev.json.parser.ParseException
        Parameters:
        file - A file to read as json. Expected to have one valid json object.
        Returns:
        The json object as read from the given file
        Throws:
        IOException
        net.minidev.json.parser.ParseException
      • getJSONListFromFile

        public static List<net.minidev.json.JSONObject> getJSONListFromFile​(File file)
                                                                     throws IOException,
                                                                            net.minidev.json.parser.ParseException
        Utility for loading a list of JSON objects from a file, assuming that every line has one JSON object.
        Parameters:
        file - The file to read from
        Returns:
        The list of json objects, each one read from one line in the file
        Throws:
        IOException
        net.minidev.json.parser.ParseException
      • writeJSONToFile

        public static void writeJSONToFile​(net.minidev.json.JSONObject json,
                                           File file)
                                    throws IOException
        Parameters:
        json - The json object to write
        file - A file to write the json to. The file is being overridden.
        Throws:
        IOException
      • writeJSONListToFile

        public static void writeJSONListToFile​(List<net.minidev.json.JSONObject> jsonList,
                                               File file)
                                        throws IOException,
                                               net.minidev.json.parser.ParseException
        Parameters:
        jsonList - The list of json objects to write, one per line
        file - A file to write the json objects to
        Throws:
        IOException
        net.minidev.json.parser.ParseException
      • readCSV

        public static List<List<String>> readCSV​(File file)
                                          throws IOException
        Reads and parses a CSV file with no headers. Supports null values.
        Parameters:
        file - The CSV file to read from
        Returns:
        A list of lines, each composed of a list of parsed columns
        Throws:
        IOException
      • readCSV

        public static List<List<String>> readCSV​(File file,
                                                 Charset charset,
                                                 FileReadOptions options)
                                          throws IOException
        Reads and parses a CSV file with no headers. Supports null values.
        Parameters:
        file - The CSV file to read from
        options - (allows reading compressed CSVs for example)
        Returns:
        A list of lines, each composed of a list of parsed columns
        Throws:
        IOException
      • readCSV

        public static List<List<String>> readCSV​(Reader csvReader)
                                          throws IOException
        Reads and parses a CSV file with no headers. Supports null values.
        Parameters:
        csvReader - The CSV file reader
        Returns:
        A list of lines, each composed of a list of parsed columns
        Throws:
        IOException
      • readCSV

        public static List<List<String>> readCSV​(File file,
                                                 FileReadOptions options)
                                          throws IOException
        Reads and parses a CSV file with no headers. Supports null values.
        Parameters:
        file - The CSV file to read from
        options - (allows reading compressed CSVs for example)
        Returns:
        A list of lines, each composed of a list of parsed columns
        Throws:
        IOException
      • writeCSV

        public static void writeCSV​(List<List<String>> lines,
                                    File f)
                             throws IOException
        Writes CSV file with no headers. Supports null values.
        Parameters:
        lines - the lines to write. The iteration order defines the line write order.
        f - The file to write to
        Throws:
        IOException
      • writeCSV

        public static <T> void writeCSV​(Collection<T> lines,
                                        File f,
                                        Function<T,​Object[]> toLineTransformer,
                                        FileWriteOptions options)
                                 throws IOException
        Writes CSV file with no headers. Supports null values. receives the toLineTransformer and applies it on each line.
        Parameters:
        lines - the lines to write. The iteration order defines the line write order.
        toLineTransformer - transformer of each object into csv columns
        f - The file to write to
        options - The file writing options
        Throws:
        IOException
      • writeCSV

        public static <T> void writeCSV​(Iterator<T> iterator,
                                        File file,
                                        Function<T,​Object[]> toLineTransformer,
                                        FileWriteOptions options,
                                        String... headers)
                                 throws IOException
        Writes items from iterator into the given csv file. Including headers.
        Type Parameters:
        T - the entity type
        Parameters:
        iterator - the objects to write in each row
        file - the file to write
        toLineTransformer - transformer of each object into csv columns
        options - file write options
        headers - the csv file headers. optional - if null/empty - ignored
        Throws:
        IOException - on failure
      • writeCSV

        public static <T> void writeCSV​(Collection<T> lines,
                                        File f,
                                        Function<T,​Object[]> toLineTransformer)
                                 throws IOException
        Writes CSV file with no headers. Supports null values. receives the toLineTransformer and applies it on each line.
        Parameters:
        lines - the lines to write. The iteration order defines the line write order.
        f - The file to write to
        Throws:
        IOException
      • writeLines

        public static void writeLines​(Collection<String> lines,
                                      File f,
                                      FileWriteOptions options)
                               throws IOException
        Writes the given lines to a file.
        Parameters:
        lines - the lines to write. The iteration order defines the line write order.
        f - The file to write to
        options - The file writing options
        Throws:
        IOException
      • writeLines

        public static void writeLines​(Collection<String> lines,
                                      File f)
                               throws IOException
        Writes the given lines to a file, using default file write options
        Parameters:
        lines - the lines to write. The iteration order defines the line write order.
        f - The file to write to
        Throws:
        IOException
      • createFolder

        public static File createFolder​(String folderPath)
                                 throws IOException
        creates a folder, or keeps it if exists
        Parameters:
        folderPath - - path (including the name) of the folder
        Returns:
        The new created folder
        Throws:
        IOException
      • createFolder

        public static File createFolder​(File parentFolder,
                                        String folderPath)
                                 throws IOException
        creates a folder
        Parameters:
        parentFolder - the parent folder that should contain the folder
        folderPath - path (including the name) of the folder
        Returns:
        The new requested folder (existing or created), after verifying its existence
        Throws:
        IOException
      • sort

        public static void sort​(File srcDir,
                                FilenameFilter fnameFilter,
                                File trgt,
                                File tmpDir,
                                int memLimit)
                         throws IOException,
                                InterruptedException

        Same as {@link this#sort(File, FilenameFilter, File, File, int)}, but enables to supply a FilenameFilter to exclude files from the provided directory from being sorted.

        Parameters:
        srcDir - The file/folder to sort. The file/s are assumed to be CSV, and lines are sorted lexicographically. If a folder is used, all files inside it are sorted as into one output file. Lines are being deduped.
        fnameFilter - a predicate for including files in the sort operation
        trgt - The target file to write to
        tmpDir - The temp folder to use for intermediate sorting results. Note that a new temp subfolder is created under this folder, and finally is cleaned up.
        memLimit - The limit of memory usage, in megabytes
        Throws:
        IOException
        InterruptedException
      • lineCount

        public static int lineCount​(File source,
                                    FileReadOptions options)
                             throws IOException
        Parameters:
        source - The file to examine
        options - File reading options
        Returns:
        The number of lines in the file
        Throws:
        IOException
      • lineCount

        public static int lineCount​(File source)
                             throws IOException
        Counts lines in a file, which is assumed to be UTF8, not compressed.
        Parameters:
        source - The file to examine
        Returns:
        The number of lines in the file
        Throws:
        IOException
      • joinPaths

        public static String joinPaths​(String prefixPath,
                                       String suffixPath)
        Parameters:
        prefixPath - The path to serve as a prefix. May be empty. May end with '/'.
        suffixPath - A path to serve as a suffix. May be empty. May start with '/'.
        Returns:
        The concatenated path, using '/' as path separator.
      • deleteFile

        public static void deleteFile​(File f,
                                      org.slf4j.Logger logger)
        Deletes a file/folder, ignoring failures. In case of a failure reports to the log.
        Parameters:
        f - The file to delete. May be null. May be a folder. In case of a folder, the complete folder is deleted, recursively.
        logger - The logger to log to in case deletion fails (as warning). Use null for no logging.
      • deleteFiles

        public static void deleteFiles​(org.slf4j.Logger logger,
                                       File... files)
        Deletes a set of files/folders, ignoring failures.
        Parameters:
        logger - The logger to log to in case deletion fails (as warning). Use null for no logging.
        files - The files to delete. Null files are ignored. File objects may be folders - In this case, the complete folder is deleted, recursively.
      • deleteFiles

        public static void deleteFiles​(File... files)
        Deletes a set of files/folders, ignoring failures.
        Parameters:
        files - The files to delete. Null files are ignored. File objects may be folders - In this case, the complete folder is deleted, recursively.
      • sampleLines

        public static void sampleLines​(File srcFile,
                                       int srcSize,
                                       File dstFile,
                                       int toSample)
                                throws IOException
        Uses a streaming sampling algorithm to select M lines from a given file, guaranteeing equal probability for all subsets of the requested sample size (See https://eyalsch.wordpress.com/2010/04/01/random-sample/ for details). Time complexity: Linear in size of the input file Space complexity: O(1)
        Parameters:
        srcFile - The file to sample lines from. Assumed to be textual, UTF8.
        srcSize - The number of lines in srcFile.
        dstFile - The destination file to contain the produced sample
        toSample - The number of lines to sample.
        Throws:
        IOException - In case of read/write error
      • saveFileHead

        public static void saveFileHead​(File file,
                                        int lineCountToCopy,
                                        File outputFile)
                                 throws IOException
        Saves the head of a given file in another new file/
        Parameters:
        file - The input text file
        lineCountToCopy - The number of lines to copy from the input file. Less lines may be copied if the file line count is lower than this number.
        outputFile - The output file
        Throws:
        IOException
      • gzip

        public static File gzip​(File inFile)
                         throws IOException
        Compresses a given file (to gz), leaving the original
        Parameters:
        inFile - The file to compress
        Returns:
        The new compressed file, created in the same folder, with the same name as the original, but with the ".gz" suffix
        Throws:
        IOException - If reading/writing fails
      • compress

        public static File compress​(File inFile,
                                    Compression compression)
                             throws IOException
        Compresses a given file, leaving the original. Uses the default compression level of the chosen compression.
        Parameters:
        inFile - The file to compress
        compression - The compression to use. Using Compression.NONE does nothing and returns the input file.
        Returns:
        The new compressed file, created in the same folder, with the same name as the original, but with the suffix corresponding to the selected compression.
        Throws:
        IOException - If reading/writing fails
      • compress

        public static File compress​(File inFile,
                                    Compression compression,
                                    int compressionLevel)
                             throws IOException
        Compresses a given file, leaving the original
        Parameters:
        inFile - The file to compress
        compression - The compression to use. Using Compression.NONE does nothing and returns the input file.
        compressionLevel - The compression level to use. Specific to the chosen compression scheme and should be used with caution.
        Returns:
        The new compressed file, created in the same folder, with the same name as the original, but with the suffix corresponding to the selected compression.
        Throws:
        IOException - If reading/writing fails
      • compressAll

        public static void compressAll​(File folder,
                                       Compression compression,
                                       int parallelism)
                                throws IOException
        Compresses all files in the given folder, deleting the originals Original file names are appended the corresponding compression suffix (.gz/.zst).
        Parameters:
        folder - The input folder
        compression - The compression to use. NONE is illegal.
        parallelism - The number of threads to use
        Throws:
        IOException
      • compressAll

        public static void compressAll​(File folder,
                                       Compression compression,
                                       int compressionLevel,
                                       int parallelism)
                                throws IOException
        Compresses all files in the given folder, deleting the originals Original file names are appended the corresponding compression suffix (.gz/.zst).
        Parameters:
        folder - The input folder
        compression - The compression to use. NONE is illegal.
        compressionLevel - The compression level to use. Specific to the chosen compression scheme and should be used with caution.
        parallelism - The number of threads to use
        Throws:
        IOException - in case of IO error or if the thread is interrupted
      • gunzip

        public static File gunzip​(File inFile)
                           throws IOException
        Decompresses a given file (from gz), leaving the original
        Parameters:
        inFile - The file to decompress
        Returns:
        The new decompressed file, in the same folder as the input file. The name will be the same as the original, with the ".gz" suffix removed. In case of no such suffix of the original file, the suffix ".decompressed" is added to the target file
        Throws:
        IOException - If reading/writing fails
      • decompress

        public static File decompress​(File inFile,
                                      Compression compression)
                               throws IOException
        Decompresses a given file, leaving the original
        Parameters:
        inFile - The file to decompress
        compression - The compression to assume
        Returns:
        The new decompressed file, in the same folder as the input file. The name will be the same as the original, with the corresponding suffix removed (.gz, or .zst) In case of no such suffix of the original file, the suffix ".decompressed" is added to the target file.
        Throws:
        IOException - If reading/writing fails
      • decompressAll

        public static void decompressAll​(File folder,
                                         Compression compression,
                                         int parallelism)
                                  throws IOException
        Decompresses all files in the given folder, deleting the originals File names are removed the relevant extension (.gz/.zst), and if not found, the name is appended with ".decompressed".
        Parameters:
        folder - The input folder
        compression - The compression to assume. NONE is illegal.
        parallelism - The number of threads to use
        Throws:
        IOException - in case of IO error or if the thread is interrupted
      • areIdentical

        public static boolean areIdentical​(File file1,
                                           File file2)
                                    throws IOException
        Compares two given files
        Parameters:
        file1 - File #1
        file2 - File #2
        Returns:
        True if and only if the byte sequences of the two files are identical
        Throws:
        IOException
      • createTempFile

        public static File createTempFile​(String prefix,
                                          String suffix,
                                          File folder)
                                   throws IOException
        Creates a unique temp file in the given folder. The file is deleted automatically when the JVM exits.
        Parameters:
        prefix - The name prefix of the temp file. Must be at least 3 char long.
        suffix - The name suffix of the temp file (e.g. ".csv").
        folder - The folder where to create the temp file, or null for default temp folder.
        Returns:
        A new temp file in the given folder. The file is set to be deleted at JVM shutdown, but it's recommended to delete it explicitly when not used anymore.
        Throws:
        IOException
      • createTempFile

        public static File createTempFile​(String prefix,
                                          String suffix)
                                   throws IOException
        Creates a unique temp file in the system's default tmp folder. The file is deleted automatically when the JVM exits.
        Parameters:
        prefix - The name prefix of the temp file. Must be at least 3 char long. Must not include path separators.
        prefix - The name suffix of the temp file (e.g. ".csv").
        Returns:
        A new temp file in the system's temp folder. The file is set to be deleted at JVM shutdown, but it's recommended to delete it explicitly when not used anymore.
        Throws:
        IOException
      • createUniqueFile

        public static File createUniqueFile​(String prefix,
                                            String suffix,
                                            File folder)
                                     throws IOException
        Creates a unique file in the given folder. The file is not deleted automatically when the JVM exits.
        Parameters:
        prefix - The name prefix of the temp file. Must be at least 3 char long. Must not include path separators.
        suffix - The name suffix of the temp file (e.g. ".csv").
        folder - The folder where to create the temp file
        Returns:
        A new file in the given folder
        Throws:
        IOException
      • createUniqueFile

        public static File createUniqueFile​(String prefix,
                                            String suffix)
                                     throws IOException
        Creates a unique file in the system's default tmp folder. The file is not deleted automatically when the JVM exits.
        Parameters:
        prefix - The name prefix of the temp file. Must be at least 3 char long. Must not include path separators.
        prefix - The name suffix of the temp file (e.g. ".csv").
        Returns:
        A new file in the system's temp folder
        Throws:
        IOException
      • createTempFolder

        public static File createTempFolder​(String prefix,
                                            File parentFolder)
                                     throws IOException
        creates a temp folder under a given parent folder
        Parameters:
        prefix - The name prefix for the temp folder
        parentFolder - The parent folder under which to create the temp folder
        Returns:
        A new temp folder under the given parent folder. The folder is set to be deleted at JVM shutdown
        Throws:
        IOException
      • getSystemDefaultTmpFolder

        public static File getSystemDefaultTmpFolder()
        Returns:
        the tmp folder on the local machine
      • createUniqueFolder

        public static File createUniqueFolder​(String prefix,
                                              File parentFolder)
                                       throws IOException
        creates a unique folder under a given parent folder
        Parameters:
        prefix - The name prefix for the folder
        parentFolder - The parent folder under which to create the folder
        Returns:
        A new folder with unique name under the given parent folder. The folder is not set to be deleted at JVM shutdown
        Throws:
        IOException
      • createTempFolder

        public static File createTempFolder​(String prefix)
                                     throws IOException
        creates a temp folder under the system's default tmp folder
        Parameters:
        prefix - The name prefix for the temp folder
        Returns:
        A new temp folder under the system's tmp folder. The folder is set to be deleted at JVM shutdown, provided that it's empty or all files in it are also created as temporary.
        Throws:
        IOException
      • createUniqueFolder

        public static File createUniqueFolder​(String prefix)
                                       throws IOException
        creates a unique folder under the system's default tmp folder
        Parameters:
        prefix - The name prefix for the folder
        Returns:
        A new folder under the system's tmp folder. The folder is not set to be deleted at JVM shutdown
        Throws:
        IOException
      • getWriter

        public static BufferedWriter getWriter​(OutputStream os,
                                               Charset charset,
                                               FileWriteOptions options)
                                        throws IOException
        Wraps a gives output stream, setting the requires write options It's recommended to call this method from within a try-with-resources block.
        Parameters:
        os - The output stream to wrap
        charset - The character encoding to use
        options - The write options
        Returns:
        The new buffered writer. The caller should make sure to close it.
        Throws:
        IOException
      • getWriter

        public static BufferedWriter getWriter​(File f,
                                               Charset charset,
                                               FileWriteOptions options)
                                        throws IOException
        Creates a new buffered writer for a given file. It's recommended to call this method from within a try-with-resources block.
        Parameters:
        f - The file
        charset - The character encoding to use
        options - The file writing options
        Returns:
        The new buffered writer. The caller should make sure to close it.
        Throws:
        IOException
      • getWriter

        public static BufferedWriter getWriter​(File f,
                                               FileWriteOptions options)
                                        throws IOException
        Creates a new buffered writer for a given file, using UTF8. It's recommended to call this method from within a try-with-resources block.
        Parameters:
        f - The file
        options - The file writing options
        Returns:
        The new buffered writer. The caller should make sure to close it.
        Throws:
        IOException
      • getWriter

        public static BufferedWriter getWriter​(File f,
                                               Charset charset)
                                        throws IOException
        Creates a new buffered writer for a given file, using default file writing settings (See FileWriteOptions). It's recommended to call this method from within a try-with-resources block.
        Parameters:
        f - The file
        charset - The character encoding to use
        Returns:
        The new buffered writer. The caller should make sure to close it.
        Throws:
        IOException
      • getWriter

        public static BufferedWriter getWriter​(File f)
                                        throws IOException
        Creates a new buffered writer for a given file, using UTF8 and default file writing settings (See FileWriteOptions). It's recommended to call this method from within a try-with-resources block.
        Parameters:
        f - The file
        Returns:
        The new buffered writer. The caller should make sure to close it.
        Throws:
        IOException
      • getCompressionInputStream

        public static InputStream getCompressionInputStream​(InputStream is,
                                                            Compression compression)
                                                     throws IOException
        Parameters:
        is - The input stream to wrap
        compression - Compression of the data supplied by the given input stream
        Returns:
        The decompressing input stream
        Throws:
        IOException
      • getCompressionOutputStream

        public static OutputStream getCompressionOutputStream​(OutputStream os,
                                                              Compression compression)
                                                       throws IOException
        Parameters:
        os - The output stream to wrap
        compression - The required compression to apply
        Returns:
        The compressing output stream
        Throws:
        IOException
      • getCompressionOutputStream

        public static OutputStream getCompressionOutputStream​(OutputStream os,
                                                              Compression compression,
                                                              int compressionLevel)
                                                       throws IOException
        Parameters:
        os - Output stream
        compression - The required compression to apply
        compressionLevel - The compression level to use. Specific to the chosen compression scheme.
        Returns:
        The compressing output stream
        Throws:
        IOException
      • getReader

        public static BufferedReader getReader​(InputStream is,
                                               Charset charset,
                                               FileReadOptions options)
                                        throws IOException
        Wraps an input stream and converts it to a reader. It's recommended to call this method from within a try-with-resources block.
        Parameters:
        is - The input stream
        charset - The character encoding to use
        options - The read options
        Returns:
        The new buffered reader. The caller should make sure to close it.
        Throws:
        IOException
      • getReader

        public static BufferedReader getReader​(InputStream is)
                                        throws IOException
        Wraps an input stream and converts it to a reader. It's recommended to call this method from within a try-with-resources block. Assumes UTF8, no compression and default buffer size.
        Parameters:
        is - The input stream
        Returns:
        The new buffered reader. The caller should make sure to close it.
        Throws:
        IOException
      • getReader

        public static BufferedReader getReader​(File f,
                                               Charset charset,
                                               FileReadOptions options)
                                        throws IOException
        Creates a new buffered reader from a given file. It's recommended to call this method from within a try-with-resources block.
        Parameters:
        f - The file
        charset - The character encoding to use
        options - The file read options
        Returns:
        The new buffered reader. The caller should make sure to close it.
        Throws:
        IOException
      • getReader

        public static BufferedReader getReader​(File f,
                                               FileReadOptions options)
                                        throws IOException
        Creates a new buffered reader from a given file, using UTF8. It's recommended to call this method from within a try-with-resources block.
        Parameters:
        f - The file
        options - The file read options
        Returns:
        The new buffered reader. The caller should make sure to close it.
        Throws:
        IOException
      • getReader

        public static BufferedReader getReader​(File f,
                                               Charset charset)
                                        throws IOException
        Creates a new buffered reader from a given file. It's recommended to call this method from within a try-with-resources block.
        Parameters:
        f - The file
        charset - The character encoding to use
        Returns:
        The new buffered reader. The caller should make sure to close it.
        Throws:
        IOException
      • getReader

        public static BufferedReader getReader​(File f)
                                        throws IOException
        Creates a new buffered reader from a given file, using UTF8. It's recommended to call this method from within a try-with-resources block.
        Parameters:
        f - The file
        Returns:
        The new buffered reader. The caller should make sure to close it.
        Throws:
        IOException
      • getInputStreamFromClasspath

        public static InputStream getInputStreamFromClasspath​(String path)
        Return the input stream of the given resource path
        Parameters:
        path - the resource path
        Returns:
        resource's input stream
      • getReaderFromClasspath

        public static BufferedReader getReaderFromClasspath​(String path,
                                                            Charset charset,
                                                            FileReadOptions options)
                                                     throws IOException
        Creates a new buffered reader from a given file. It's recommended to call this method from within a try-with-resources block.
        Parameters:
        path - The path in classpath
        charset - The character encoding to use
        options - The file read options
        Returns:
        The new buffered reader. The caller should make sure to close it.
        Throws:
        IOException
      • getReaderFromClasspath

        public static BufferedReader getReaderFromClasspath​(String path,
                                                            FileReadOptions options)
                                                     throws IOException
        Creates a new buffered reader from a given file, using UTF8. It's recommended to call this method from within a try-with-resources block.
        Parameters:
        path - The path in classpath
        options - The file read options
        Returns:
        The new buffered reader. The caller should make sure to close it.
        Throws:
        IOException
      • getReaderFromClasspath

        public static BufferedReader getReaderFromClasspath​(String path,
                                                            Charset charset)
                                                     throws IOException
        Creates a new buffered reader from a given file. It's recommended to call this method from within a try-with-resources block.
        Parameters:
        path - The path in classpath
        charset - The character encoding to use
        Returns:
        The new buffered reader. The caller should make sure to close it.
        Throws:
        IOException
      • getReaderFromClasspath

        public static BufferedReader getReaderFromClasspath​(String path)
                                                     throws IOException
        Creates a new buffered reader from a given file, using UTF8. It's recommended to call this method from within a try-with-resources block.
        Parameters:
        path - The path in classpath
        Returns:
        The new buffered reader. The caller should make sure to close it.
        Throws:
        IOException
      • validateFolderNotEmpty

        public static void validateFolderNotEmpty​(File folder)
                                           throws IOException
        throws exception if folder is invalid or empty.
        Throws:
        IOException - if folder is empty
      • getExtension

        public static String getExtension​(String name)
        Parameters:
        name - A file name
        Returns:
        The file extension. The extension is defined here as the suffix starting with the first '.'. If none, null is returned.
      • removeExtension

        public static String removeExtension​(String name)
        Parameters:
        name - A file name
        Returns:
        The file name, with the extension removed, if found. The extension is defined as the suffix starting with the first '.'.
      • getDotlessExtension

        public static String getDotlessExtension​(String name)
        Parameters:
        name - A file name
        Returns:
        The file extension. The extension is defined here as the suffix starting with the last '.'. If none, null is returned.
      • removeDotlessExtension

        public static String removeDotlessExtension​(String name)
        Parameters:
        name - A file name
        Returns:
        The file name, with the extension removed, if found. The extension is defined here as the suffix starting with the last '.'.
      • removeCSVExtension

        public static String removeCSVExtension​(String fileName)
        Parameters:
        fileName - A file name. May include path.
        Returns:
        The same name, but with the CSV extension removed (either .csv or .csv.gz or .csv.zst)
      • copyFile

        public static void copyFile​(File src,
                                    File dst)
                             throws IOException
        Copies a file from one location to the other
        Parameters:
        src - The file to copy
        dst - The destination file
        Throws:
        IOException
      • copyFileToFolder

        public static void copyFileToFolder​(File src,
                                            File dstFolder)
                                     throws IOException
        Copies a file from one location to the other, keeping the filename
        Parameters:
        src - The file to copy
        dstFolder - The destination folder
        Throws:
        IOException
      • getTotalSizeRecursive

        public static long getTotalSizeRecursive​(File folder)
        Parameters:
        folder - A local folder
        Returns:
        The total size, in bytes, of all descendant files under the given folder
      • close

        public static void close​(Closeable... closeables)
                          throws IOException
        Closes the resources in the order they are provided. Tries to close as much as possible closeables and then throws exception if needs to. Any of the closeables may be null
        Parameters:
        closeables - the closeables that needs to be closed. Ignores null closeables inside the array.
        Throws:
        IOException - the IOException that aggregates all the closable IOExceptions
      • close

        public static void close​(Collection<? extends Closeable> closeables)
                          throws IOException
        Closes the resources in the order they are provided. Tries to close as much as possible closeables and then throws exception if needs to. Any of the closeables may be null
        Parameters:
        closeables - the closeables that needs to be closed. Ignores null closeables inside the array.
        Throws:
        IOException - the IOException that aggregates all the closable IOExceptions
      • close

        public static void close​(Closeable closeable)
                          throws IOException
        Closes the closeable.
        Parameters:
        closeable - the closeable that needs to be closed. May be null.
        Throws:
        IOException - if an I/O error occurs
      • closeSilently

        public static void closeSilently​(Closeable... closeables)
        Closes the given closeables, in a silent manner (ignoring IOExceptions)
        Parameters:
        closeables - the closeables that needs to be closed. Any of them may be null.
      • closeSilently

        public static void closeSilently​(Collection<? extends Closeable> closeables)
        Closes the given closeables, in a silent manner (ignoring IOExceptions)
        Parameters:
        closeables - the closeables that needs to be closed. Any of them may be null.