Class FileUtils

java.lang.Object
io.fluxcapacitor.common.FileUtils

public class FileUtils extends Object
Utility class for reading and writing files and classpath resources.

Supports:

  • Reading text files from file system, URL, URI, or classpath
  • Writing string content to files
  • Graceful resolution of paths in JAR and non-JAR environments
  • Loading and merging Properties from multiple modules
  • Safe fallbacks using Optional

Default character encoding is StandardCharsets.UTF_8.

  • Constructor Details

    • FileUtils

      public FileUtils()
  • Method Details

    • writeFile

      public static void writeFile(String fileName, String content)
      Writes a string to a file using UTF-8 encoding.
      Parameters:
      fileName - the target file name
      content - the content to write
    • loadFile

      public static String loadFile(String fileName)
      Loads the contents of a file located relative to the calling class.
    • loadFile

      public static String loadFile(Class<?> referencePoint, String fileName)
      Loads a classpath resource relative to a given reference class.
    • loadFile

      public static String loadFile(String fileName, Charset charset)
      Loads a file with a specified charset, using the calling class as reference point.
    • loadFile

      public static String loadFile(Class<?> referencePoint, String fileName, Charset charset)
      Loads a file from a classpath location using a reference class and charset.
      Throws:
      NullPointerException - if the resource cannot be found
    • getFile

      public static URI getFile(Class<?> referencePoint, String fileName)
      Gets a file from a classpath location using a reference class.
      Throws:
      NullPointerException - if the resource cannot be found
    • loadFile

      public static String loadFile(URI uri)
      Loads file contents from a URI.
    • loadFile

      public static String loadFile(URI uri, Charset charset)
      Loads file contents from a URI using a specific charset.
    • loadFile

      public static String loadFile(File file)
      Loads file contents from a File.
    • loadFile

      public static String loadFile(File file, Charset charset)
      Loads file contents from a File using a specific charset.
    • loadFile

      public static String loadFile(URL url, Charset charset)
      Loads file contents from a URL using a specific charset.
      Throws:
      IllegalArgumentException - if the URL does not represent a file
    • tryLoadFile

      public static Optional<String> tryLoadFile(String fileName)
      Attempts to load file content relative to the caller class, returns empty if not found.
    • tryLoadFile

      public static Optional<String> tryLoadFile(Class<?> referencePoint, String fileName)
      Attempts to load file content relative to a reference class.
    • tryLoadFile

      public static Optional<String> tryLoadFile(String fileName, Charset charset)
      Attempts to load file content with specified charset.
    • tryLoadFile

      public static Optional<String> tryLoadFile(Class<?> referencePoint, String fileName, Charset charset)
      Attempts to load file content with specified reference class and charset.
    • tryLoadFile

      public static Optional<String> tryLoadFile(File file)
      Attempts to load file content from a File.
    • tryLoadFile

      public static Optional<String> tryLoadFile(File file, Charset charset)
      Attempts to load file content from a File with specified charset.
    • safeResolve

      public static URI safeResolve(URI base, String relativePath)
      Safely resolves a relative path against a base URI, including JAR URLs.
    • getResourceBaseUri

      public static URI getResourceBaseUri(@NonNull @NonNull String resourcePath)
      Retrieves the base URI of a given resource path. The resource is resolved relative to the caller's class loader. If the resource path does not exist or cannot be resolved to a valid URI, an exception is thrown.
    • getResourceBaseUri

      public static URI getResourceBaseUri(@NonNull @NonNull String resourcePath, ClassLoader classLoader)
      Retrieves the base URI of a given resource path. The resource is resolved relative to classLoader. If the resource path does not exist or cannot be resolved to a valid URI, an exception is thrown.
    • loadProperties

      public static Properties loadProperties(String fileName)
      Loads properties from the classpath, merging from all available matching resources.

      If the same key appears in multiple modules, a warning is logged.

      Parameters:
      fileName - resource path to a properties file
      Returns:
      merged properties across all modules