Class LocalFile
This class provides a unified interface for working with files regardless of their location, allowing code to transparently access files from either the classpath (as resources) or the file system. This is particularly useful in applications that need to work with files in both development (file system) and production (packaged JAR) environments.
Features:
- Unified file access - works with both classpath resources and file system files
- Optional caching - can cache file contents in memory for fast repeated access
- Transparent resolution - automatically resolves files based on construction parameters
- Thread-safe reading - synchronized access for cached content
Use Cases:
- Reading configuration files that may be on classpath or file system
- Accessing template files in both development and production environments
- Loading resources that need to work in both JAR and unpackaged scenarios
- Applications that need to support both embedded and external file access
Usage:
Caching:
The cache() method loads the entire file contents into memory, which can improve
performance for files that are accessed multiple times. Once cached, subsequent calls to
read() return a ByteArrayInputStream backed by the cached data. Caching is
thread-safe and synchronized.
Thread Safety:
This class is thread-safe for reading operations. The caching mechanism uses synchronization
to ensure thread-safe access to cached content. Multiple threads can safely call read()
concurrently.
See Also:
LocalDir- Directory counterpart for resolving files within directories- I/O Package
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncache()Caches the contents of this file into an internal byte array for quick future retrieval.getName()Returns the name of this file (filename without directory path).read()Returns an input stream for reading the contents of this file.longsize()Returns the size of this file in bytes.
-
Constructor Details
-
LocalFile
Constructor for classpath file.Creates a LocalFile that references a file on the classpath, relative to the specified class. The path is resolved using
Class.getResourceAsStream(String).Example:
// File in same package as MyClass LocalFilefile1 =new LocalFile(MyClass.class ,"config.properties" );// File in subdirectory LocalFilefile2 =new LocalFile(MyClass.class ,"templates/index.html" );// Absolute path from classpath root LocalFilefile3 =new LocalFile(MyClass.class ,"/com/example/config.xml" );- Parameters:
clazz- The class used to retrieve resources. Must not benull .clazzPath- The path relative to the class. Must be a non-null normalized relative path. Use absolute paths (starting with'/' ) to reference from classpath root.
-
LocalFile
Constructor for file system file.Creates a LocalFile that references a file on the file system using a
Path. The path must point to an actual file (not a directory) and must have a filename component.Example:
// Absolute path LocalFilefile1 =new LocalFile(Paths.get("/etc/config.properties" ));// Relative path LocalFilefile2 =new LocalFile(Paths.get("data/input.txt" ));// From File object Filef =new File("output.log" ); LocalFilefile3 =new LocalFile(f .toPath());- Parameters:
path- Filesystem file location. Must not benull . Must not be a root path (must have a filename).- Throws:
IllegalArgumentException- if the path is a root path (has no filename).
-
-
Method Details
-
cache
Caches the contents of this file into an internal byte array for quick future retrieval.This method reads the entire file into memory and stores it in a byte array. Subsequent calls to
read()will return aByteArrayInputStreambacked by this cached data, avoiding repeated file I/O operations. This is useful for files that are accessed multiple times.The caching operation is thread-safe and synchronized. If multiple threads call this method concurrently, only one will perform the actual read operation.
Example:
LocalFile
file =new LocalFile(MyClass.class ,"template.html" );file .cache();// Load into memory // Multiple reads are fast - no I/O InputStreamis1 =file .read(); InputStreamis2 =file .read();Memory Considerations:
- The entire file is loaded into memory, so use with caution for large files
- Once cached, the file contents remain in memory for the lifetime of the LocalFile object
- Cache is shared across all threads accessing this LocalFile instance
- Returns:
- This object for method chaining.
- Throws:
IOException- If the file could not be read or does not exist.
-
getName
Returns the name of this file (filename without directory path).For classpath files, this is the last component of the classpath path. For file system files, this is the filename component of the path.
Example:
LocalFile
file1 =new LocalFile(MyClass.class ,"templates/index.html" ); Stringname1 =file1 .getName();// Returns "index.html" LocalFilefile2 =new LocalFile(Paths.get("/var/log/app.log" )); Stringname2 =file2 .getName();// Returns "app.log" - Returns:
- The name of this file (filename component only).
-
read
Returns an input stream for reading the contents of this file.If the file has been cached via
cache(), this method returns aByteArrayInputStreambacked by the cached data. Otherwise, it returns a new input stream that reads directly from the file (classpath resource or file system).Each call to this method returns a new input stream. The caller is responsible for closing the returned stream.
Example:
LocalFile
file =new LocalFile(MyClass.class ,"data.txt" );// Read file contents try (InputStreamis =file .read()) {// Process stream }- Returns:
- An input stream for reading the contents of this file.
- Throws:
IOException- If the file could not be read, does not exist, or is not accessible.
-
size
Returns the size of this file in bytes.For file system files, this method returns the actual file size using
Files.size(Path). For classpath files, the size cannot be determined and this method returns-1 .Example:
LocalFile
fsFile =new LocalFile(Paths.get("/var/log/app.log" ));long size =fsFile .size();// Returns actual file size LocalFilecpFile =new LocalFile(MyClass.class ,"resource.txt" );long size2 =cpFile .size();// Returns -1 (unknown) - Returns:
- The size of this file in bytes, or
-1 if the size cannot be determined (e.g., for classpath resources). - Throws:
IOException- If the file size could not be determined (for file system files).
-