Package com.ntu.fdae.group1.bto.utils
Class FileUtil
java.lang.Object
com.ntu.fdae.group1.bto.utils.FileUtil
Utility class providing common file operations, CSV handling, and data
parsing
capabilities for the BTO Management System.
This class offers static methods for:
- Reading from and writing to CSV files
- Parsing dates and enum values from strings
- Handling string operations common in file processing
- Safely parsing numeric types with default values
The class uses UTF-8 encoding for all file operations and ISO-8601 format for date handling. It is designed to be used throughout the application for consistent file and data manipulation.
-
Method Summary
Modifier and TypeMethodDescriptionstatic String
formatLocalDate
(LocalDate date) Formats a LocalDate into a string using the predefined ISO date format.static String
Joins a list of strings with a specified delimiter.static Double
parseDoubleOrDefault
(String value, Double defaultValue) Safely parses a Double from a string with a fallback default value.static <E extends Enum<E>>
ESafely parses an enum value from a string (case-insensitive).static <E extends Enum<E>>
ESafely parses an enum value from a string (case-insensitive) with a default value.static Integer
parseIntOrDefault
(String value, Integer defaultValue) Safely parses an Integer from a string with a fallback default value.static LocalDate
parseLocalDate
(String dateString) Safely parses a LocalDate from a string using the predefined ISO date format.readCsvLines
(String filePath) Reads all lines from a CSV file, skipping the header.splitString
(String str, String delimiter) Splits a delimited string into a List of strings.static void
writeCsvLines
(String filePath, List<String[]> data, String[] header) Writes data to a CSV file, overwriting existing content.
-
Method Details
-
readCsvLines
Reads all lines from a CSV file, skipping the header. This method reads a CSV file, processes each line by:- Skipping the header line
- Splitting each line by the CSV delimiter
- Removing double quotes from each field
- Ignoring empty lines
- Parameters:
filePath
- Path to the CSV file- Returns:
- List of String arrays, where each array represents a row's columns
- Throws:
IOException
- If an I/O error occurs reading from the file
-
writeCsvLines
public static void writeCsvLines(String filePath, List<String[]> data, String[] header) throws IOException Writes data to a CSV file, overwriting existing content. This method handles the complete process of writing data to a CSV file:- Creates parent directories if they don't exist
- Writes the header row first
- Writes each data row, joining column values with the CSV delimiter
- Overwrites the file if it already exists
- Parameters:
filePath
- Path to the CSV file to write todata
- List of String arrays representing rows (excluding header)header
- Array of strings for the header row- Throws:
IOException
- If an I/O error occurs writing to the file
-
parseLocalDate
Safely parses a LocalDate from a string using the predefined ISO date format. This method handles all potential parsing issues:- Returns null for null or empty input strings
- Trims the input string before parsing
- Logs a warning if the date cannot be parsed
- Parameters:
dateString
- The string representation of a date to parse- Returns:
- LocalDate object if successful, or null if parsing fails or input is invalid
-
formatLocalDate
Formats a LocalDate into a string using the predefined ISO date format. This method handles:- Returning an empty string for null dates
- Logging a warning if formatting fails
- Providing today's date as fallback if formatting fails
- Parameters:
date
- The LocalDate object to format- Returns:
- Formatted date string, empty string for null dates, or current date if formatting fails
-
parseEnum
Safely parses an enum value from a string (case-insensitive). This method attempts to convert a string to an enum constant by:- Converting the string to uppercase
- Trimming whitespace
- Returning null for null or empty input strings
- Logging a warning if the enum value cannot be parsed
- Type Parameters:
E
- The enum type- Parameters:
enumClass
- The class of the enumvalue
- The string value to parse- Returns:
- The enum constant if successful, or null if parsing fails or input is invalid
-
parseEnum
Safely parses an enum value from a string (case-insensitive) with a default value. This method extendsparseEnum(Class, String)
by providing a default value when parsing fails. It:- Returns the default value for null or empty input strings
- Logs a warning if the enum value cannot be parsed
- Returns the default value if parsing fails
- Type Parameters:
E
- The enum type- Parameters:
enumClass
- The class of the enumvalue
- The string value to parsedefaultValue
- The value to return if parsing fails or input is invalid- Returns:
- The enum constant if successful, or the defaultValue if parsing fails
-
joinList
Joins a list of strings with a specified delimiter. This method:- Filters out null values from the list
- Handles null or empty lists by returning an empty string
- Joins all remaining strings with the specified delimiter
- Parameters:
list
- The list of strings to joindelimiter
- The delimiter to use between elements- Returns:
- A single string with all elements joined, or empty string for null/empty lists
-
splitString
Splits a delimited string into a List of strings. This method:- Handles null or empty input by returning an empty list
- Uses the specified delimiter as a regex for splitting
- Preserves trailing empty strings by using -1 as the limit parameter
- Parameters:
str
- The string to splitdelimiter
- The delimiter regex- Returns:
- List of strings resulting from the split operation (potentially empty)
-
parseIntOrDefault
Safely parses an Integer from a string with a fallback default value. This method:- Returns the default value for null or empty input strings
- Trims the input string before parsing
- Logs a warning if the number cannot be parsed
- Returns the default value if parsing fails
- Parameters:
value
- The string to parsedefaultValue
- The default value to return if parsing fails- Returns:
- The parsed integer or the default value
-
parseDoubleOrDefault
Safely parses a Double from a string with a fallback default value. This method:- Returns the default value for null or empty input strings
- Trims the input string before parsing
- Logs a warning if the number cannot be parsed
- Returns the default value if parsing fails
- Parameters:
value
- The string to parsedefaultValue
- The default value to return if parsing fails- Returns:
- The parsed double or the default value
-