Package io.fluxcapacitor.common
Class FileUtils
java.lang.Object
io.fluxcapacitor.common.FileUtils
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic URI
Gets a file from a classpath location using a reference class.static URI
getResourceBaseUri
(@NonNull String resourcePath) Retrieves the base URI of a given resource path.static URI
getResourceBaseUri
(@NonNull String resourcePath, ClassLoader classLoader) Retrieves the base URI of a given resource path.static String
Loads file contents from aFile
.static String
Loads file contents from aFile
using a specific charset.static String
Loads a classpath resource relative to a given reference class.static String
Loads a file from a classpath location using a reference class and charset.static String
Loads the contents of a file located relative to the calling class.static String
Loads a file with a specified charset, using the calling class as reference point.static String
Loads file contents from aURI
.static String
Loads file contents from aURI
using a specific charset.static String
Loads file contents from aURL
using a specific charset.static Properties
loadProperties
(String fileName) Loads properties from the classpath, merging from all available matching resources.static URI
safeResolve
(URI base, String relativePath) Safely resolves a relative path against a base URI, including JAR URLs.tryLoadFile
(File file) Attempts to load file content from aFile
.tryLoadFile
(File file, Charset charset) Attempts to load file content from aFile
with specified charset.tryLoadFile
(Class<?> referencePoint, String fileName) Attempts to load file content relative to a reference class.tryLoadFile
(Class<?> referencePoint, String fileName, Charset charset) Attempts to load file content with specified reference class and charset.tryLoadFile
(String fileName) Attempts to load file content relative to the caller class, returns empty if not found.tryLoadFile
(String fileName, Charset charset) Attempts to load file content with specified charset.static void
Writes a string to a file using UTF-8 encoding.
-
Constructor Details
-
FileUtils
public FileUtils()
-
-
Method Details
-
writeFile
Writes a string to a file using UTF-8 encoding.- Parameters:
fileName
- the target file namecontent
- the content to write
-
loadFile
Loads the contents of a file located relative to the calling class. -
loadFile
Loads a classpath resource relative to a given reference class. -
loadFile
Loads a file with a specified charset, using the calling class as reference point. -
loadFile
Loads a file from a classpath location using a reference class and charset.- Throws:
NullPointerException
- if the resource cannot be found
-
getFile
Gets a file from a classpath location using a reference class.- Throws:
NullPointerException
- if the resource cannot be found
-
loadFile
Loads file contents from aURI
. -
loadFile
Loads file contents from aURI
using a specific charset. -
loadFile
Loads file contents from aFile
. -
loadFile
Loads file contents from aFile
using a specific charset. -
loadFile
Loads file contents from aURL
using a specific charset.- Throws:
IllegalArgumentException
- if the URL does not represent a file
-
tryLoadFile
Attempts to load file content relative to the caller class, returns empty if not found. -
tryLoadFile
Attempts to load file content relative to a reference class. -
tryLoadFile
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
Attempts to load file content from aFile
. -
tryLoadFile
Attempts to load file content from aFile
with specified charset. -
safeResolve
Safely resolves a relative path against a base URI, including JAR URLs. -
getResourceBaseUri
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 toclassLoader
. If the resource path does not exist or cannot be resolved to a valid URI, an exception is thrown. -
loadProperties
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
-