Class FileUtil

java.lang.Object
com.ntu.fdae.group1.bto.utils.FileUtil

public final class FileUtil extends Object
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 Details

    • readCsvLines

      public static List<String[]> readCsvLines(String filePath) throws IOException
      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
      All file operations use UTF-8 encoding.
      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
      All file operations use UTF-8 encoding.
      Parameters:
      filePath - Path to the CSV file to write to
      data - 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

      public static LocalDate parseLocalDate(String dateString)
      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

      public static String formatLocalDate(LocalDate date)
      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

      public static <E extends Enum<E>> E parseEnum(Class<E> enumClass, String value)
      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 enum
      value - The string value to parse
      Returns:
      The enum constant if successful, or null if parsing fails or input is invalid
    • parseEnum

      public static <E extends Enum<E>> E parseEnum(Class<E> enumClass, String value, E defaultValue)
      Safely parses an enum value from a string (case-insensitive) with a default value. This method extends parseEnum(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 enum
      value - The string value to parse
      defaultValue - The value to return if parsing fails or input is invalid
      Returns:
      The enum constant if successful, or the defaultValue if parsing fails
    • joinList

      public static String joinList(List<String> list, String delimiter)
      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 join
      delimiter - The delimiter to use between elements
      Returns:
      A single string with all elements joined, or empty string for null/empty lists
    • splitString

      public static List<String> splitString(String str, String delimiter)
      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 split
      delimiter - The delimiter regex
      Returns:
      List of strings resulting from the split operation (potentially empty)
    • parseIntOrDefault

      public static Integer parseIntOrDefault(String value, Integer defaultValue)
      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 parse
      defaultValue - The default value to return if parsing fails
      Returns:
      The parsed integer or the default value
    • parseDoubleOrDefault

      public static Double parseDoubleOrDefault(String value, Double defaultValue)
      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 parse
      defaultValue - The default value to return if parsing fails
      Returns:
      The parsed double or the default value