Class FileReaderBuilder
Reader instances from files with configurable character encoding.
This builder provides a convenient way to create file readers with custom character encodings and optional handling for missing files. It's particularly useful when you need to read files with specific encodings or handle cases where files may not exist.
Features:
- Fluent API - all methods return
this for method chaining - Character encoding support - specify custom charset for file reading
- Missing file handling - optional support for returning empty reader when file doesn't exist
- Multiple file specification methods - accept File, String path, or Path
Use Cases:
- Reading files with specific character encodings (UTF-8, ISO-8859-1, etc.)
- Handling optional configuration files that may not exist
- Creating readers with consistent encoding across an application
- Reading files where encoding must be explicitly specified
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.
See Also:
FileWriterBuilder- Builder for file writersPathReaderBuilder- Builder for Path-based readers- I/O Package
-
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 file.Sets the character encoding for reading the file by charset name.Sets the character encoding for reading the file.static FileReaderBuildercreate()Creates a new builder.static FileReaderBuilderCreates a new builder initialized with the specified file.Sets the file to read from.Sets the file path to read from.
-
Constructor Details
-
FileReaderBuilder
public FileReaderBuilder()
-
-
Method Details
-
create
Creates a new builder.Example:
Reader
reader = FileReaderBuilder.create () .file("data.txt" ) .build();- Returns:
- A new builder instance.
-
create
Creates a new builder initialized with the specified file.Example:
File
file =new File("config.properties" ); Readerreader = FileReaderBuilder.create (file ) .charset("UTF-8" ) .build();- Parameters:
file- The file to read from.- Returns:
- A new builder instance initialized with the specified file.
-
allowNoFile
Enables handling of missing files by returning an empty reader instead of throwing an exception.When this option is enabled, if the file is
null or does not exist, thebuild()method will return aStringReaderwith empty content instead of throwing aFileNotFoundException. This is useful for optional configuration files.Example:
// Without allowNoFile - throws exception if file doesn't exist Readerreader1 = FileReaderBuilder.create () .file("required.txt" ) .build();// Throws FileNotFoundException if file missing // With allowNoFile - returns empty reader if file doesn't exist Readerreader2 = FileReaderBuilder.create () .file("optional.txt" ) .allowNoFile() .build();// Returns empty StringReader if file missing - Returns:
- This object for method chaining.
-
build
Creates a newReaderfor reading from the configured file.If
allowNoFile()was called and the file isnull or does not exist, this method returns an emptyStringReader. Otherwise, it creates anInputStreamReaderwith the specified character encoding.Example:
try (Readerreader = FileReaderBuilder.create () .file("data.txt" ) .charset("UTF-8" ) .build()) {// Read from file }- Returns:
- A new
Readerfor reading from the file. - Throws:
FileNotFoundException- If the file could not be found andallowNoFile()was not called.
-
charset
Sets the character encoding for reading the file.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.Example:
Reader
reader = FileReaderBuilder.create () .file("data.txt" ) .charset(StandardCharsets.UTF_8) .build();- Parameters:
cs- The character encoding to use. The default isCharset.defaultCharset().- Returns:
- This object for method chaining.
-
charset
Sets the character encoding for reading the file by charset name.This is a convenience method that accepts a charset name string and converts it to a
CharsetusingCharset.forName(String).Example:
Reader
reader = FileReaderBuilder.create () .file("data.txt" ) .charset("UTF-8" ) .build();- Parameters:
cs- The character encoding name (e.g.,"UTF-8" ,"ISO-8859-1" ). The default isCharset.defaultCharset(). Must not benull .- Returns:
- This object for method chaining.
-
file
Sets the file to read from.Example:
File
f =new File("config.properties" ); Readerreader = FileReaderBuilder.create () .file(f ) .build();- Parameters:
value- The file to read from.- Returns:
- This object for method chaining.
-
file
Sets the file path to read from.Example:
Reader
reader = FileReaderBuilder.create () .file("/path/to/file.txt" ) .build();- Parameters:
path- The file path to read from.- Returns:
- This object for method chaining.
-