Package org.pipecraft.infra.io
Class FileUtils
- java.lang.Object
-
- org.pipecraft.infra.io.FileUtils
-
public class FileUtils extends Object
A set of utility methods for manipulating files- Author:
- Eyal Schneider
-
-
Field Summary
Fields Modifier and Type Field Description static StringCSV_EXTENSIONstatic StringCSV_GZ_EXTENSIONstatic StringCSV_ZSTD_EXTENSIONstatic StringGZ_EXTENSIONstatic StringNDJSON_EXTENSIONstatic CharsetUTF8static StringZSTD_EXTENSION
-
Constructor Summary
Constructors Constructor Description FileUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static booleanareIdentical(File file1, File file2)Compares two given filesstatic voidclose(Closeable closeable)Closes the closeable.static voidclose(Closeable... closeables)Closes the resources in the order they are provided.static voidclose(Collection<? extends Closeable> closeables)Closes the resources in the order they are provided.static voidcloseSilently(Closeable... closeables)Closes the given closeables, in a silent manner (ignoring IOExceptions)static voidcloseSilently(Collection<? extends Closeable> closeables)Closes the given closeables, in a silent manner (ignoring IOExceptions)static Filecompress(File inFile, Compression compression)Compresses a given file, leaving the original.static Filecompress(File inFile, Compression compression, int compressionLevel)Compresses a given file, leaving the originalstatic voidcompressAll(File folder, Compression compression, int parallelism)Compresses all files in the given folder, deleting the originals Original file names are appended the corresponding compression suffix (.gz/.zst).static voidcompressAll(File folder, Compression compression, int compressionLevel, int parallelism)Compresses all files in the given folder, deleting the originals Original file names are appended the corresponding compression suffix (.gz/.zst).static voidcopyFile(File src, File dst)Copies a file from one location to the otherstatic voidcopyFileToFolder(File src, File dstFolder)Copies a file from one location to the other, keeping the filenamestatic FilecreateFolder(File parentFolder, String folderPath)creates a folderstatic FilecreateFolder(String folderPath)creates a folder, or keeps it if existsstatic FilecreateTempFile(String prefix, String suffix)Creates a unique temp file in the system's default tmp folder.static FilecreateTempFile(String prefix, String suffix, File folder)Creates a unique temp file in the given folder.static FilecreateTempFolder(String prefix)creates a temp folder under the system's default tmp folderstatic FilecreateTempFolder(String prefix, File parentFolder)creates a temp folder under a given parent folderstatic FilecreateUniqueFile(String prefix, String suffix)Creates a unique file in the system's default tmp folder.static FilecreateUniqueFile(String prefix, String suffix, File folder)Creates a unique file in the given folder.static FilecreateUniqueFolder(String prefix)creates a unique folder under the system's default tmp folderstatic FilecreateUniqueFolder(String prefix, File parentFolder)creates a unique folder under a given parent folderstatic Filedecompress(File inFile, Compression compression)Decompresses a given file, leaving the originalstatic voiddecompressAll(File folder, Compression compression, int parallelism)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".static voiddeleteFile(File f, org.slf4j.Logger logger)Deletes a file/folder, ignoring failures.static voiddeleteFiles(File... files)Deletes a set of files/folders, ignoring failures.static voiddeleteFiles(org.slf4j.Logger logger, File... files)Deletes a set of files/folders, ignoring failures.static InputStreamgetCompressionInputStream(InputStream is, Compression compression)static OutputStreamgetCompressionOutputStream(OutputStream os, Compression compression)static OutputStreamgetCompressionOutputStream(OutputStream os, Compression compression, int compressionLevel)static StringgetDotlessExtension(String name)static StringgetExtension(String name)static BufferedInputStreamgetInputStream(File f, FileReadOptions options)Creates an input stream that reads from a file, using the givenFileReadOptions.static BufferedInputStreamgetInputStream(InputStream is, FileReadOptions options)Wraps an input stream, adding the features defined by the givenFileReadOptions.static InputStreamgetInputStreamFromClasspath(String path)Return the input stream of the given resource pathstatic net.minidev.json.JSONObjectgetJSONFromClasspath(String filename)Utility for loading JSON objects from classpath filesstatic net.minidev.json.JSONObjectgetJSONFromFile(File file)static net.minidev.json.JSONObjectgetJSONFromInpuStream(InputStream is)static List<net.minidev.json.JSONObject>getJSONListFromClasspath(String filename)Utility for loading a list of JSON objects from classpath files, assuming that every line has one JSON object.static List<net.minidev.json.JSONObject>getJSONListFromFile(File file)Utility for loading a list of JSON objects from a file, assuming that every line has one JSON object.static List<String>getLinesFromClasspath(String filename)Utility for loading the lines of a classpath file (UTF8).static List<String>getLinesFromFile(File file)Utility for loading the lines of a local text file (UTF8).static List<String>getLinesFromFile(File file, Charset charset, FileReadOptions options)Utility for loading the lines of a local text filestatic Set<String>getLinesFromFileAsSet(File file)Utility for loading the lines of a local text file (UTF8) into a setstatic Set<String>getLinesFromFileAsSet(File file, Charset charset, FileReadOptions options)Utility for loading the lines of a local text file into a setstatic List<String>getListFromReader(Reader reader)Utility for loading the lines from a readerstatic BufferedOutputStreamgetOutputStream(File f, FileWriteOptions options)Creates an output stream that writes into a file, using the givenFileWriteOptions.static BufferedOutputStreamgetOutputStream(OutputStream os, FileWriteOptions options)Wraps an output stream, adding it options by the givenFileWriteOptions.static BufferedReadergetReader(File f)Creates a new buffered reader from a given file, using UTF8.static BufferedReadergetReader(File f, Charset charset)Creates a new buffered reader from a given file.static BufferedReadergetReader(File f, Charset charset, FileReadOptions options)Creates a new buffered reader from a given file.static BufferedReadergetReader(File f, FileReadOptions options)Creates a new buffered reader from a given file, using UTF8.static BufferedReadergetReader(InputStream is)Wraps an input stream and converts it to a reader.static BufferedReadergetReader(InputStream is, Charset charset, FileReadOptions options)Wraps an input stream and converts it to a reader.static BufferedReadergetReaderFromClasspath(String path)Creates a new buffered reader from a given file, using UTF8.static BufferedReadergetReaderFromClasspath(String path, Charset charset)Creates a new buffered reader from a given file.static BufferedReadergetReaderFromClasspath(String path, Charset charset, FileReadOptions options)Creates a new buffered reader from a given file.static BufferedReadergetReaderFromClasspath(String path, FileReadOptions options)Creates a new buffered reader from a given file, using UTF8.static Set<String>getSetFromReader(Reader reader)Utility for loading the lines from a reader, into a deduped setstatic StringgetStringFromClasspath(String filename)Utility for loading string from classpath filesstatic FilegetSystemDefaultTmpFolder()static longgetTotalSizeRecursive(File folder)static BufferedWritergetWriter(File f)Creates a new buffered writer for a given file, using UTF8 and default file writing settings (SeeFileWriteOptions).static BufferedWritergetWriter(File f, Charset charset)Creates a new buffered writer for a given file, using default file writing settings (SeeFileWriteOptions).static BufferedWritergetWriter(File f, Charset charset, FileWriteOptions options)Creates a new buffered writer for a given file.static BufferedWritergetWriter(File f, FileWriteOptions options)Creates a new buffered writer for a given file, using UTF8.static BufferedWritergetWriter(OutputStream os, Charset charset, FileWriteOptions options)Wraps a gives output stream, setting the requires write options It's recommended to call this method from within a try-with-resources block.static Filegunzip(File inFile)Decompresses a given file (from gz), leaving the originalstatic Filegzip(File inFile)Compresses a given file (to gz), leaving the originalstatic StringjoinPaths(String prefixPath, String suffixPath)static intlineCount(File source)Counts lines in a file, which is assumed to be UTF8, not compressed.static intlineCount(File source, FileReadOptions options)static List<List<String>>readCSV(File file)Reads and parses a CSV file with no headers.static List<List<String>>readCSV(File file, Charset charset, FileReadOptions options)Reads and parses a CSV file with no headers.static List<List<String>>readCSV(File file, FileReadOptions options)Reads and parses a CSV file with no headers.static List<List<String>>readCSV(Reader csvReader)Reads and parses a CSV file with no headers.static StringremoveCSVExtension(String fileName)static StringremoveDotlessExtension(String name)static StringremoveExtension(String name)static voidsampleLines(File srcFile, int srcSize, File dstFile, int toSample)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).static voidsaveFileHead(File file, int lineCountToCopy, File outputFile)Saves the head of a given file in another new file/static voidsort(File srcDir, FilenameFilter fnameFilter, File trgt, File tmpDir, int memLimit)Same as {@link this#sort(File, FilenameFilter, File, File, int)}, but enables to supply aFilenameFilterto exclude files from the provided directory from being sorted.static voidsort(File src, File trgt, File tmpDir, int memLimit)Serves for sorting large files on the file system, limiting the memory used in the process.static voidvalidateFolderNotEmpty(File folder)throws exception if folder is invalid or empty.static <T> voidwriteCSV(Collection<T> lines, File f, Function<T,Object[]> toLineTransformer)Writes CSV file with no headers.static <T> voidwriteCSV(Collection<T> lines, File f, Function<T,Object[]> toLineTransformer, FileWriteOptions options)Writes CSV file with no headers.static <T> voidwriteCSV(Iterator<T> iterator, File file, Function<T,Object[]> toLineTransformer, FileWriteOptions options, String... headers)Writes items from iterator into the given csv file.static voidwriteCSV(List<List<String>> lines, File f)Writes CSV file with no headers.static voidwriteJSONListToFile(List<net.minidev.json.JSONObject> jsonList, File file)static voidwriteJSONToFile(net.minidev.json.JSONObject json, File file)static voidwriteLines(Collection<String> lines, File f)Writes the given lines to a file, using default file write optionsstatic voidwriteLines(Collection<String> lines, File f, FileWriteOptions options)Writes the given lines to a file.
-
-
-
Field Detail
-
CSV_GZ_EXTENSION
public static final String CSV_GZ_EXTENSION
- See Also:
- Constant Field Values
-
CSV_ZSTD_EXTENSION
public static final String CSV_ZSTD_EXTENSION
- See Also:
- Constant Field Values
-
ZSTD_EXTENSION
public static final String ZSTD_EXTENSION
-
CSV_EXTENSION
public static final String CSV_EXTENSION
- See Also:
- Constant Field Values
-
NDJSON_EXTENSION
public static final String NDJSON_EXTENSION
- See Also:
- Constant Field Values
-
GZ_EXTENSION
public static final String GZ_EXTENSION
-
UTF8
public static final Charset UTF8
-
-
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 fromcharset- The charset the text file is encoded withoptions- 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 fromcharset- The charset the text file is encoded withoptions- 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:
IOExceptionnet.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:
IOExceptionnet.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:
IOExceptionnet.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:
IOExceptionnet.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:
IOExceptionnet.minidev.json.parser.ParseException
-
writeJSONToFile
public static void writeJSONToFile(net.minidev.json.JSONObject json, File file) throws IOException- Parameters:
json- The json object to writefile- 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 linefile- A file to write the json objects to- Throws:
IOExceptionnet.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 fromoptions- (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 fromoptions- (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 columnsf- The file to write tooptions- 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 rowfile- the file to writetoLineTransformer- transformer of each object into csv columnsoptions- file write optionsheaders- 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 tooptions- 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 folderfolderPath- 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 src, File trgt, File tmpDir, int memLimit) throws IOException, InterruptedException
Serves for sorting large files on the file system, limiting the memory used in the process. NOTE: this method is not cross-platform, and uses linux' sort command. Use with care.- Throws:
IOExceptionInterruptedException
-
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
FilenameFilterto 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 operationtrgt- The target file to write totmpDir- 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:
IOExceptionInterruptedException
-
lineCount
public static int lineCount(File source, FileReadOptions options) throws IOException
- Parameters:
source- The file to examineoptions- 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 sampletoSample- 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 filelineCountToCopy- 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 compresscompression- 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 compresscompression- 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 foldercompression- 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 foldercompression- 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 decompresscompression- 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 foldercompression- 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 #1file2- 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 folderparentFolder- 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 folderparentFolder- 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
-
getOutputStream
public static BufferedOutputStream getOutputStream(OutputStream os, FileWriteOptions options) throws IOException
Wraps an output stream, adding it options by the givenFileWriteOptions.- Parameters:
os- The stream to wrapoptions- The write options- Returns:
- The resulting output stream (buffered)
- Throws:
IOException
-
getOutputStream
public static BufferedOutputStream getOutputStream(File f, FileWriteOptions options) throws IOException
Creates an output stream that writes into a file, using the givenFileWriteOptions.- Parameters:
f- The file to write tooptions- The write options- Returns:
- The resulting output stream (buffered)
- Throws:
IOException
-
getInputStream
public static BufferedInputStream getInputStream(InputStream is, FileReadOptions options) throws IOException
Wraps an input stream, adding the features defined by the givenFileReadOptions.- Parameters:
is- The input stream to wrapoptions- The read options- Returns:
- The resulting input stream (Buffered)
- Throws:
IOException
-
getInputStream
public static BufferedInputStream getInputStream(File f, FileReadOptions options) throws IOException
Creates an input stream that reads from a file, using the givenFileReadOptions.- Parameters:
f- The file to read fromoptions- The read options- Returns:
- The resulting input stream (Buffered)
- 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 wrapcharset- The character encoding to useoptions- 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 filecharset- The character encoding to useoptions- 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 fileoptions- 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 (SeeFileWriteOptions). It's recommended to call this method from within a try-with-resources block.- Parameters:
f- The filecharset- 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 (SeeFileWriteOptions). 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 wrapcompression- 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 wrapcompression- 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 streamcompression- The required compression to applycompressionLevel- 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 streamcharset- The character encoding to useoptions- 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 filecharset- The character encoding to useoptions- 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 fileoptions- 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 filecharset- 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 classpathcharset- The character encoding to useoptions- 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 classpathoptions- 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 classpathcharset- 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 copydst- 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 copydstFolder- 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.
-
-