|
Chapter 11 The java.io Package |
|
StringWriter
Name
StringWriter
- Class Name:
-
java.io.StringWriter
- Superclass:
-
java.io.Writer
- Immediate Subclasses:
-
None
- Interfaces Implemented:
-
None
- Availability:
-
New as of JDK 1.1
The StringWriter class represents
a stream whose data is written to a string. This class is similar to the
CharArrayWriter class, which
writes its data to a char array.
The StringWriter class uses
a StringBuffer to store its
data; a String can be retrieved
with the toString() method.
public class java.io.StringWriter extends java.io.Writer {
// Constructors
public StringWriter();
protected StringWriter(int initialSize);
// Instance Methods
public void close();
public void flush();
public StringBuffer getBuffer();
public String toString();
public void write(int c);
public void write(char[] cbuf, int off, int len);
public void write(String str);
public void write(String str, int off, int len);
}
- Description
-
This constructor creates a StringWriter
with an internal buffer that has a default size of 16 characters. The buffer
grows automatically as data is written to the stream.
- Parameters
-
- initialSize
-
The initial
buffer size.
- Description
-
This constructor creates a StringWriter
with an internal buffer that has a size of initialSize
characters. The buffer grows automatically as data is written to the stream.
- Overrides
-
Writer.close()
- Description
-
This method does nothing. For most subclassesof Writer,
this method releases any system resources
that are associated with the Writer
object. However, the StringWriter's
internal buffer may be needed for subsequent calls to toString().
For this reason, close() does
nothing, and the internal buffer is not released until the StringWriter
is garbage collected.
- Overrides
-
Writer.flush()
- Description
-
This method does nothing. The StringWriter
writes data directly into its internal buffer; thus it is never necessary
to flush the stream.
- Returns
-
A reference to the internal data buffer.
- Description
-
This method returns a reference to the StringBuffer
object that is used in this StringWriter.
- Returns
-
A String constructed from the internal data buffer.
- Overrides
-
Object.toString()
- Description
-
This method returns a reference to a String object created
from the characters stored in this object's internal buffer.
- Parameters
-
- c
-
The value to write.
- Overrides
-
Writer.write(int)
- Description
-
This method writes the given value into the internal buffer. If the buffer
is full, it is expanded.
- Parameters
-
- cbuf
-
An array of characters to write to the stream.
- off
-
An offset into the character array.
- len
-
The number of characters to write.
- Overrides
-
Writer.write(char[], int, int)
- Description
-
This method copies len characters
to this object's internal buffer from cbuf,
starting off elements from
the beginning of the array. If the internal buffer is full, it is expanded.
- Parameters
-
- str
-
A String
to write to the stream.
- Overrides
-
Writer.write(String)
- Description
-
This method copies the characters of str into
this object's internal buffer. If the internal buffer is full, it
is expanded.
- Parameters
-
- str
-
A String to write to the stream.
- off
-
An offset into the string.
- len
-
The number of characters to write.
- Overrides
-
Writer.write(String, int, int)
- Description
-
This method copies len characters
to this object's internal buffer from str,
starting off characters from
the beginning of the given string. If the internal buffer is full, it is
expanded.
CharArrayWriter,
String,
StringBuffer,
Writer
|
|