Class NoCloseWriter
- All Implemented Interfaces:
Closeable,Flushable,Appendable,AutoCloseable
Writer that prevents the underlying writer from being closed.
This class wraps an existing Writer and intercepts the close() method,
making it a no-op (except for flushing). All other operations are delegated to the underlying
writer. This is useful when you need to pass a writer to code that will close it, but you
want to keep the underlying writer open for further use.
Features:
- Prevents closing -
close()flushes but doesn't close the underlying writer - Transparent delegation - all other operations pass through to the wrapped writer
- Useful for resource management - allows multiple consumers without premature closing
Use Cases:
- Passing writers to APIs that close them, but you need to keep the writer open
- Multiple operations on the same writer where intermediate operations might close it
- Resource management scenarios where you control the writer lifecycle
- Wrapping system writers that should not be closed
Usage:
Important Notes:
- The
close()method flushes the writer but does not close the underlying writer - You are responsible for closing the underlying writer when you're done with it
- This wrapper does not prevent resource leaks - ensure the underlying writer is eventually closed
See Also:
NoCloseOutputStream- OutputStream counterpart- I/O Package
-
Field Summary
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionappend(char c) append(CharSequence csq) append(CharSequence csq, int start, int end) voidclose()Flushes the writer but does not close the underlying Writer.voidflush()toString()voidwrite(char[] cbuf) voidwrite(char[] cbuf, int off, int len) voidwrite(int c) voidvoidMethods inherited from class java.io.Writer
nullWriter
-
Constructor Details
-
NoCloseWriter
Constructor.Creates a new NoCloseWriter that wraps the specified Writer. The wrapper will prevent the underlying writer from being closed via the
close()method.Example:
FileWriter
fw =new FileWriter("file.txt" ); NoCloseWriterwrapper =new NoCloseWriter(fw );- Parameters:
w- The Writer to wrap. Must not benull .
-
-
Method Details
-
append
- Specified by:
appendin interfaceAppendable- Overrides:
appendin classWriter- Throws:
IOException
-
append
- Specified by:
appendin interfaceAppendable- Overrides:
appendin classWriter- Throws:
IOException
-
append
- Specified by:
appendin interfaceAppendable- Overrides:
appendin classWriter- Throws:
IOException
-
close
Flushes the writer but does not close the underlying Writer.This method flushes any buffered data to the underlying writer but does not close it. The underlying writer remains open and can continue to be used after this method is called.
Example:
FileWriter
fw =new FileWriter("file.txt" ); NoCloseWriterwrapper =new NoCloseWriter(fw );wrapper .close();// Flushes but doesn't close fw fw .write("still works" );// fw is still open - Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein classWriter- Throws:
IOException- If an I/O error occurs while flushing.
-
flush
- Specified by:
flushin interfaceFlushable- Specified by:
flushin classWriter- Throws:
IOException
-
toString
-
write
- Overrides:
writein classWriter- Throws:
IOException
-
write
- Specified by:
writein classWriter- Throws:
IOException
-
write
- Overrides:
writein classWriter- Throws:
IOException
-
write
- Overrides:
writein classWriter- Throws:
IOException
-
write
- Overrides:
writein classWriter- Throws:
IOException
-