Class IO
This class provides static methods for various file and data handling operations, such as reading, writing, compressing, and hashing files and strings.
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
copyResourceToFile
(String resourceName, Path targetPath) Copies a resource from the application's classpath to a specified targetPath
.static String
formatFrequency
(double value) Formats a frequency value into scientific notation.static String
formatNumber
(double value) Formats a number to three decimal places.static String
gzipCompress
(String content) Compresses a string using GZIP compression and encodes the result in Base64.static String
gzipDecompress
(String content) Decompresses a Base64-encoded GZIP-compressed string.static String
Generates the MD5 hash of the given string.static String
randomAlphanumeric
(int length) Generates a random alphanumeric string of the specified length.Reads a tabular file and converts its content into a nested map structure.static void
Writes the specified content to a file at the given path.
-
Method Details
-
formatFrequency
Formats a frequency value into scientific notation.This method formats the given frequency value using the
frequencyFormat
. If the formatted value equals ".10E1", it is replaced with "1.00E0" for consistency.- Parameters:
value
- The frequency value to format.- Returns:
- A
String
representing the formatted frequency in scientific notation.
-
formatNumber
Formats a number to three decimal places.This method formats the given number using the
decimalFormat
. The result is a string representation of the number with up to three decimal places.- Parameters:
value
- The number to format.- Returns:
- A
String
representing the formatted number.
-
randomAlphanumeric
Generates a random alphanumeric string of the specified length.This method uses the
RandomStringUtils
instance to generate a random string consisting of both letters and digits. The generated string is suitable for use in scenarios where a random identifier or token is needed.- Parameters:
length
- The length of the random alphanumeric string to generate.- Returns:
- A
String
containing the generated random alphanumeric characters.
-
writeFile
Writes the specified content to a file at the given path.This method ensures that the parent directories of the target file are created if they do not exist. It then writes the provided content to the file using UTF-8 encoding. If the file already exists, its content is overwritten.
- Parameters:
path
- ThePath
where the file will be written.content
- TheString
content to write to the file.- Throws:
IOException
- If an I/O error occurs during directory creation or file writing.
-
readTabularFileAsNestedMap
public static Map<String,Map<String, readTabularFileAsNestedMapString>> (File file) throws IOException Reads a tabular file and converts its content into a nested map structure.This method reads a tabular file where the first row contains headers and each subsequent row contains data. The first column is treated as the key for the outer map, and the remaining columns are stored in an inner map with their corresponding headers as keys. The file can use tab or comma as delimiters.
- Parameters:
file
- TheFile
object representing the tabular file to read.- Returns:
- A
HashMap
where the outer map's key is the first column's value, and the value is anotherHashMap
containing the remaining columns as key-value pairs. - Throws:
IOException
- If an I/O error occurs or the file format is invalid.
-
copyResourceToFile
Copies a resource from the application's classpath to a specified targetPath
.This method retrieves a resource as an
InputStream
from the application's classpath using the specified resource path. The resource is then copied to the target file path, overwriting any existing file at the target location.- Parameters:
resourceName
- The path to the resource within the application's classpath.targetPath
- The file path where the resource should be copied.- Throws:
MusialException
- If the resource cannot be found or an I/O error occurs during the copy operation.
-
gzipCompress
Compresses a string using GZIP compression and encodes the result in Base64.This method compresses the input string using the GZIP algorithm and then encodes the compressed byte array into a Base64 string. The method ensures proper resource management by using a try-with-resources block for the output streams.
- Parameters:
content
- TheString
to be compressed.- Returns:
- A Base64-encoded
String
representing the GZIP-compressed content. - Throws:
IOException
- If an I/O error occurs during compression.
-
gzipDecompress
Decompresses a Base64-encoded GZIP-compressed string.This method decodes the input string from Base64, decompresses the resulting GZIP-compressed data, and returns the decompressed content as a string. It uses a buffer to read the decompressed data in chunks and appends it to a
StringBuilder
.- Parameters:
content
- The Base64-encoded GZIP-compressed string to decompress.- Returns:
- A
String
containing the decompressed content. - Throws:
IOException
- If an I/O error occurs during decompression.
-
md5Hash
Generates the MD5 hash of the given string.This method computes the MD5 hash of the input string and returns it as a hexadecimal string. It uses the
DigestUtils.md5Hex(String)
method from the Apache Commons Codec library to perform the hashing.
-