Class FileUtils
java.lang.Object
io.fluxzero.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
Propertiesfrom multiple modules - Safe fallbacks using
Optional
Default character encoding is StandardCharsets.UTF_8.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic URIGets a file from a classpath location using a reference class.static URIgetResourceBaseUri(@NonNull String resourcePath) Retrieves the base URI of a given resource path.static URIgetResourceBaseUri(@NonNull String resourcePath, ClassLoader classLoader) Retrieves the base URI of a given resource path.static StringLoads file contents from aFile.static StringLoads file contents from aFileusing a specific charset.static StringLoads a classpath resource relative to a given reference class.static StringLoads a file from a classpath location using a reference class and charset.static StringLoads the contents of a file located relative to the calling class.static StringLoads a file with a specified charset, using the calling class as reference point.static StringLoads file contents from aURI.static StringLoads file contents from aURIusing a specific charset.static StringLoads file contents from aURLusing a specific charset.static PropertiesloadProperties(String fileName) Loads properties from the classpath, merging from all available matching resources.static URIsafeResolve(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 aFilewith 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 voidWrites a string to a file using UTF-8 encoding.
-
Constructor Details
-
FileUtils
public FileUtils()
-
-
Method Details
-
writeFile
-
loadFile
-
loadFile
-
loadFile
-
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
-
loadFile
-
loadFile
-
loadFile
-
loadFile
Loads file contents from aURLusing a specific charset.- Throws:
IllegalArgumentException- if the URL does not represent a file
-
tryLoadFile
-
tryLoadFile
-
tryLoadFile
-
tryLoadFile
-
tryLoadFile
-
tryLoadFile
-
safeResolve
-
getResourceBaseUri
-
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
-