Class LocalDir
This class provides a unified interface for working with directories regardless of their location, allowing code to transparently access files within directories from either the classpath (as resources) or the file system. This is particularly useful in applications that need to work with directory-based resources in both development (file system) and production (packaged JAR) environments.
Features:
- Unified directory access - works with both classpath resources and file system directories
- File resolution - can resolve files within the directory using relative paths
- Transparent resolution - automatically resolves files based on construction parameters
- Immutable - directory location cannot be changed after construction
Use Cases:
- Accessing template directories that may be on classpath or file system
- Loading configuration files from directories in both development and production
- Resolving resources within package directories
- Applications that need to support both embedded and external directory access
Usage:
Path Resolution:
The resolve(String) method resolves files within the directory using relative paths.
For classpath directories, the path resolution follows Java resource path conventions:
null or empty string - resolves relative to the class's package- Absolute path (starts with
'/' ) - resolves relative to classpath root - Relative path - resolves relative to the specified classpath path
Thread Safety:
This class is immutable and therefore thread-safe. Multiple threads can safely access a LocalDir instance concurrently without synchronization.
See Also:
LocalFile- File counterpart for individual file access- I/O Package
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
LocalDir
Constructor for classpath directory.Creates a LocalDir that references a directory on the classpath, relative to the specified class. The path resolution follows Java resource path conventions.
Path Examples:
// Package directory (null or empty) LocalDirdir1 =new LocalDir(MyClass.class ,null );// Resolves files in same package as MyClass // Absolute path from classpath root LocalDirdir2 =new LocalDir(MyClass.class ,"/com/example/templates" );// Resolves files from classpath root // Relative path from class package LocalDirdir3 =new LocalDir(MyClass.class ,"templates" );// Resolves files relative to MyClass package - Parameters:
clazz- The class used to retrieve resources. Must not benull .clazzPath- The subpath. Can be any of the following:null or an empty string - Package of the class- Absolute path (starts with
'/' ) - Relative to root package - Relative path (does not start with
'/' ) - Relative to class package
-
LocalDir
Constructor for file system directory.Creates a LocalDir that references a directory on the file system using a
Path.Example:
// Absolute path LocalDirdir1 =new LocalDir(Paths.get("/var/config" ));// Relative path LocalDirdir2 =new LocalDir(Paths.get("data/templates" ));// From File object Filef =new File("output" ); LocalDirdir3 =new LocalDir(f .toPath());- Parameters:
path- Filesystem directory location. Must not benull .
-
-
Method Details
-
equals
-
hashCode
-
resolve
Resolves a file within this directory using the specified relative path.This method attempts to locate a file within the directory. If the file exists and is readable, a
LocalFileinstance is returned. If the file does not exist or is not accessible,null is returned.For classpath directories, the path is resolved according to the directory's path type:
- Package directory (null clazzPath) - path is kept relative
- Root directory ("/") - path is made absolute if not already
- Absolute clazzPath - resolved path is made absolute
- Relative clazzPath - resolved path remains relative
Example:
LocalDir
dir =new LocalDir(MyClass.class ,"templates" );// Resolve file in directory LocalFilefile =dir .resolve("index.html" );if (file !=null ) { InputStreamis =file .read(); }// Resolve file in subdirectory LocalFilefile2 =dir .resolve("pages/about.html" );Security Note:
This method does not perform path validation or security checks (e.g., checking for path traversal attacks or malformed values). The caller is responsible for ensuring the path is safe and valid.
- Parameters:
path- The relative path to the file to resolve within this directory. Must be a non-null relative path.- Returns:
- A
LocalFileinstance if the file exists and is readable, ornull if it does not.
-
toString
-