Class PathReaderBuilder
Reader instances from Path objects with configurable character encoding.
This builder provides a convenient way to create readers from NIO Path objects with custom
character encodings and optional handling for missing files. It's similar to FileReaderBuilder
but works with the modern NIO Path API instead of the legacy File API.
Features:
- Fluent API - all methods return
this for method chaining - NIO Path support - works with modern
PathAPI - Character encoding support - specify custom charset for file reading
- Missing file handling - optional support for returning empty reader when file doesn't exist
- Multiple path specification methods - accept Path or String path
Use Cases:
- Reading files with specific character encodings using NIO Path API
- Handling optional configuration files that may not exist
- Creating readers with consistent encoding across an application
- Working with NIO-based file operations
Usage:
Character Encoding:
By default, the builder uses the system's default charset (Charset.defaultCharset()).
You can specify a custom charset using charset(Charset) or charset(String).
This is important when reading files that were written with a specific encoding.
Comparison with FileReaderBuilder:
- FileReaderBuilder: Works with legacy
FileAPI - PathReaderBuilder: Works with modern NIO
PathAPI - FileReaderBuilder: Uses
FileInputStream - PathReaderBuilder: Uses
Files.newBufferedReader(Path, Charset)
See Also:
FileReaderBuilder- Builder for File-based readers- I/O Package
- Since:
- 9.1.0
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionEnables handling of missing files by returning an empty reader instead of throwing an exception.build()Creates a newReaderfor reading from the configured path.Sets the character encoding for reading the path by charset name.Sets the character encoding for reading the path.static PathReaderBuildercreate()Creates a new builder.static PathReaderBuilderCreates a new builder initialized with the specified path.Sets the path to read from by string path.Sets the path to read from.
-
Constructor Details
-
PathReaderBuilder
public PathReaderBuilder()
-
-
Method Details
-
create
Creates a new builder.Example:
Reader
reader = PathReaderBuilder.create () .path(Paths.get("data.txt" )) .build();- Returns:
- A new builder instance.
-
create
Creates a new builder initialized with the specified path.Example:
Path
path = Paths.get("config.properties" ); Readerreader = PathReaderBuilder.create (path ) .charset("UTF-8" ) .build();- Parameters:
path- The path to read from.- Returns:
- A new builder instance initialized with the specified path.
-
allowNoFile
Enables handling of missing files by returning an empty reader instead of throwing an exception.When this option is enabled, if the path is
null or does not exist, thebuild()method will return aStringReaderwith empty content instead of throwing anIOException. This is useful for optional configuration files.Example:
// Without allowNoFile - throws exception if file doesn't exist Readerreader1 = PathReaderBuilder.create () .path(Paths.get("required.txt" )) .build();// Throws NoSuchFileException if file missing // With allowNoFile - returns empty reader if file doesn't exist Readerreader2 = PathReaderBuilder.create () .path(Paths.get("optional.txt" )) .allowNoFile() .build();// Returns empty StringReader if file missing - Returns:
- This object for method chaining.
-
build
Creates a newReaderfor reading from the configured path.If
allowNoFile()was called and the path isnull or does not exist, this method returns an emptyStringReader. Otherwise, it creates a buffered reader usingFiles.newBufferedReader(Path, Charset)with the specified character encoding.Example:
try (Readerreader = PathReaderBuilder.create () .path(Paths.get("data.txt" )) .charset("UTF-8" ) .build()) {// Read from file }- Returns:
- A new
Readerfor reading from the path. - Throws:
IllegalStateException- If no path is configured andallowNoFile()was not called.NoSuchFileException- If the path does not exist andallowNoFile()was not called.IOException- If an I/O error occurs opening the path.
-
charset
Sets the character encoding for reading the path.If not specified, the system's default charset (
Charset.defaultCharset()) is used. Specifying the encoding is important when reading files that were written with a specific character encoding. Passingnull resets to the default charset.Example:
Reader
reader = PathReaderBuilder.create () .path(Paths.get("data.txt" )) .charset(StandardCharsets.UTF_8) .build();- Parameters:
charset- The character encoding to use. The default isCharset.defaultCharset().null resets to the default.- Returns:
- This object for method chaining.
-
charset
Sets the character encoding for reading the path by charset name.This is a convenience method that accepts a charset name string and converts it to a
CharsetusingCharset.forName(String). Passingnull resets to the default charset.Example:
Reader
reader = PathReaderBuilder.create () .path(Paths.get("data.txt" )) .charset("UTF-8" ) .build();- Parameters:
charset- The character encoding name (e.g.,"UTF-8" ,"ISO-8859-1" ). The default isCharset.defaultCharset().null resets to the default.- Returns:
- This object for method chaining.
-
path
Sets the path to read from.Example:
Path
p = Paths.get("config.properties" ); Readerreader = PathReaderBuilder.create () .path(p ) .build();- Parameters:
path- The path to read from.- Returns:
- This object for method chaining.
-
path
Sets the path to read from by string path.This is a convenience method that converts a string path to a
PathusingPaths.get(String, String...).Example:
Reader
reader = PathReaderBuilder.create () .path("/path/to/file.txt" ) .build();- Parameters:
path- The file path to read from. Must not benull .- Returns:
- This object for method chaining.
-