Class FileWriterBuilder
Writer instances for writing to files with configurable options.
This builder provides a convenient way to create file writers with custom character encodings, append mode, and buffering options. It's particularly useful when you need to write files with specific encodings or control whether to append to existing files.
Features:
- Fluent API - all methods return
this for method chaining - Character encoding support - specify custom charset for file writing
- Append mode - optionally append to existing files instead of overwriting
- Buffering support - optional buffering for improved performance
- Multiple file specification methods - accept File or String path
Use Cases:
- Writing files with specific character encodings (UTF-8, ISO-8859-1, etc.)
- Appending to log files or data files
- Creating writers with consistent encoding across an application
- Writing files where buffering improves performance
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 writing files that need to be read with a specific encoding.
See Also:
FileReaderBuilder- Builder for file readers- I/O Package
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionappend()Enables append mode, which appends to the file instead of overwriting it.buffered()Enables buffering for improved write performance.build()Creates a newWriterfor writing to the configured file.Sets the character encoding for writing to the file by charset name.Sets the character encoding for writing to the file.static FileWriterBuildercreate()Creates a new builder.static FileWriterBuilderCreates a new builder initialized with the specified file.static FileWriterBuilderCreates a new builder initialized with the specified file path.Sets the file to write to.Sets the file path to write to.
-
Constructor Details
-
FileWriterBuilder
public FileWriterBuilder()
-
-
Method Details
-
create
Creates a new builder.Example:
Writer
writer = FileWriterBuilder.create () .file("output.txt" ) .build();- Returns:
- A new builder instance.
-
create
Creates a new builder initialized with the specified file.Example:
File
file =new File("output.txt" ); Writerwriter = FileWriterBuilder.create (file ) .charset("UTF-8" ) .build();- Parameters:
file- The file to write to.- Returns:
- A new builder instance initialized with the specified file.
-
create
Creates a new builder initialized with the specified file path.Example:
Writer
writer = FileWriterBuilder.create ("/path/to/output.txt" ) .append() .build();- Parameters:
path- The file path to write to.- Returns:
- A new builder instance initialized with the specified path.
-
append
Enables append mode, which appends to the file instead of overwriting it.When append mode is enabled, data written to the file will be appended to the end of the existing file content rather than overwriting it. This is useful for log files or data files where you want to preserve existing content.
Example:
// Append to log file WriterlogWriter = FileWriterBuilder.create () .file("app.log" ) .append() .build();logWriter .write("New log entry\n" );// Appends to existing content - Returns:
- This object for method chaining.
-
buffered
Enables buffering for improved write performance.When buffering is enabled, the writer wraps the underlying output stream with a
BufferedOutputStream, which can significantly improve performance for multiple small writes by batching them together.Example:
// Buffered writer for better performance Writerwriter = FileWriterBuilder.create () .file("output.txt" ) .buffered() .build();- Returns:
- This object for method chaining.
-
build
Creates a newWriterfor writing to the configured file.The writer is created with the specified character encoding, append mode, and buffering options. The file will be created if it doesn't exist (unless in append mode, where the file must exist or be creatable).
Example:
try (Writerwriter = FileWriterBuilder.create () .file("output.txt" ) .charset("UTF-8" ) .buffered() .build()) {writer .write("Hello World" ); }- Returns:
- A new
Writerfor writing to the file. - Throws:
FileNotFoundException- If the file could not be created or opened for writing.
-
charset
Sets the character encoding for writing to the file.If not specified, the system's default charset (
Charset.defaultCharset()) is used. Specifying the encoding is important when writing files that need to be read with a specific character encoding.Example:
Writer
writer = FileWriterBuilder.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 writing to 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:
Writer
writer = FileWriterBuilder.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 write to.Example:
File
f =new File("output.txt" ); Writerwriter = FileWriterBuilder.create () .file(f ) .build();- Parameters:
value- The file to write to.- Returns:
- This object for method chaining.
-
file
Sets the file path to write to.Example:
Writer
writer = FileWriterBuilder.create () .file("/path/to/output.txt" ) .build();- Parameters:
path- The file path to write to.- Returns:
- This object for method chaining.
-