Java Fundamental Classes Reference

Previous Chapter 11
The java.io Package
Next
 

StringWriter

Name

StringWriter

Synopsis

Class Name:

java.io.StringWriter

Superclass:

java.io.Writer

Immediate Subclasses:

None

Interfaces Implemented:

None

Availability:

New as of JDK 1.1

Description

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.

Class Summary

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); 
}

Constructors

StringWriter

public StringWriter()

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.

protected StringWriter (int initialSize)

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.

Instance Methods

close

public void close()

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.

flush

public void flush()

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.

getBuffer

public StringBuffer getBuffer()

Returns

A reference to the internal data buffer.

Description

This method returns a reference to the StringBuffer object that is used in this StringWriter.

toString

public String toString()

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.

write

public void write(int c)

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.

public void write(char[] cbuf, int off, int len)

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.

public void write(String str)

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.

public void write(String str, int off, int len)

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.

Inherited Methods

Method

Inherited From

Method

Inherited From

clone()

Object

equals(Object)

Object

finalize()

Object

getClass()

Object

hashCode()

Object

notify()

Object

notifyAll()

Object

wait()

Object

wait(long)

Object

wait(long, int)

Object

write(char[])

Writer

   

See Also

CharArrayWriter, String, StringBuffer, Writer


Previous Home Next
StringReader Book Index SyncFailedException

Java in a Nutshell Java Language Reference Java AWT Java Fundamental Classes Exploring Java