StringWriter

posted in: Java | 0

StringWriter

  • StringWriter is a character stream that collects its output in a string buffer, which can then be used to construct a string.
  • Closing a StringWriter has no effect. The methods in this class can be called after the stream has been closed without generating an IOException.

Syntax for StringWriter Class declaration

The declaration of Java.io. StringWriter class is as follows:

public class StringWriter extends Writer

Indexes of FilterWriter:

               There are three fields for Java.io.FilterWriter. They are:

1) Variable index

2) Constructor index

3) Method index

1)      Variable Index:

  • lock — This is the object used to synchronize operations on this stream.

2)      Constructor Index:

  • StringWriter () – Create a new StringWriter using the default initial string-buffer size.
  • StringWriter (int initialSize) – Create a new StringWriter using the specified initial string-buffer size.

3)      Method Index:

  • There are 11 methods defined in the FilterWriter class. They are as follows:
  1. append(char)
  2. append(CharSequence)
  3. append(CharSequence, int, int)
  4. close()
  5. flush()
  6. getBuffer()
  7. toString()
  8. write(char[], int, int)
  9. write(int)
  10. write(String)
  11. write(String, int, int)
  • The methods are also inherited from the following classes:
  1. Java.io.Writer
  2. Java.lang.Object

Variables:

  • lock: The object used to synchronize operations on this stream.

 Syntax:

 protected Object lock 

Constructors:

1)      StringWriter

Syntax:

public StringWriter()

Description:

  • Create a new StringWriter using the default initial string-buffer size.

2)      StringWriter(int)

Syntax:

public StringWriter(int initialSize)
  • initialSize – The number of char values that will fit into this buffer before it is automatically expanded

Description:

  • Create a new StringWriter using the specified initial string-buffer size.

Throws:

IllegalArgumentException – If initalSize is negative.

Methods:

1)      write(int):-

Syntax:

public void write(int c)
  • c – int specifying a character to be written

Description:

  • It writes a single character. It overrides write in class Writer.

2)      write(char[], int, int):-

Syntax:

public void write(char[] cbuf, int off,  int len)
  • cbuf – Array of characters
  • off – Offset from which to start writing characters
  • len – Number of characters to write

Description:

  • It writes a portion of an array of characters.It is specified by write in class Writer.

3)      write(String):-

Syntax:

public void write(String str)
  • str – String to be written

Description:

  • It writes a string. It overrides write in class Writer.

4)      write(String, int, int):-

Syntax:-

public void write(String  str, int off, int len)
  • str -String to be written
  • off -Offset from which to start writing characters
  • len – Number of characters to write

Description:

  • It writes a portion of a string. It overrides write in class Writer.

5)      append(CharSequence):-

Syntax:

public StringWriter append(CharSequence csq)
  • csq – The character sequence to append. If csq is null, then the four characters “null” are appended to this writer.

Description:

  • It appends the specified character sequence to this writer. An invocation of this method of the form out.append(csq) behaves in exactly the same way as the invocation out.write(csq.toString()) .
  • It is specified by append in interface Appendable.It overrides append in class Writer.

6)      append(CharSequence, int, int)

Syntax:            

public StringWriter append(CharSequence csq,  int start, int end)
  • csq – The character sequence from which a subsequence will be appended. If csq is null, then characters will be appended as if csq contained the four characters “null”.
  • start – The index of the first character in the subsequence
  • end – The index of the character following the last character in the subsequence

Description:

  • It appends a subsequence of the specified character sequence to this writer.
  • An invocation of this method of the form out.append(csq, start, end) when csq is not null, behaves in exactly the same way as the invocation out.write(csq.subSequence(start, end).toString()).
  • It is specified by append in interface Appendable. It overrides append in class Writer.

Throws:

IndexOutOfBoundsException – If start or end are negative, start is greater than end, or end is greater than csq.length().

7)      append(char):-

Syntax:

public StringWriter append(char c)
  • c – The 16-bit character to append

Description:

  • It appends the specified character to this writer.
  • An invocation of this method of the form out.append(c) behaves in exactly the same way as the invocation out.write(c).
  • It is specified by append in interface Appendable. It overrides append in class Writer.

8)      toString()

Syntax:

public String toString()

Description:

  • It returns the buffer’s current value as a string.It overrides toString in class Object.

9)      getBuffer()

Syntax:

public StringBuffer getBuffer()

Description:

  • It returns the string buffer the current buffer value.

10)  flush()

Syntax:

public void flush()

Description:

  • It flushes the stream. It is specified by flush in interface Flushable and flush in class Writer.

11)  close()

Syntax:

public void close() throws IOException
  • IOException – If an I/O error occurs

Description:

  • Closing a StringWriter has no effect. The methods in this class can be called after the stream has been closed without generating an IOException.
  • It is specified by close in interface Closeable, close in interface AutoCloseable and close in class Writer.

Methods inherited from java.io.Writer:

i)   append(char)

ii)  append(CharSequence)

iii) append(CharSequence, int, int)

iv) write(char[])

v)  write(String)

i)  append(char)

Syntax:

public Writer append (char c) throws IOException
  • c– The 16-bit character to append.
  • IOException – If an I/O error occurs.

Description:

Appends the specified character to this writer. An invocation of this method of the form out.append(c) behaves in exactly the same way as the invocation out.write(c). It is specified by “append in interface Appendable”. It returns the Writer

ii)  append(CharSequence)

Syntax:

public Writer append (CharSequence csq) throws IOException
  • csq – The character sequence to append. If csq is null, then the four characters “null” are appended to this writer.
  • IOException – If an I/O error occurs

Description:

  • Appends the specified character sequence to this writer. An invocation of this method of the form out.append(csq) behaves in exactly the same way as the invocation out.write(csq.toString()).
  • Depending on the specification of toString for the character sequence csq, the entire sequence may not be appended. For instance, invoking the toString method of a character buffer will return a subsequence whose content depends upon the buffer’s position and limit. 
  • It is specified by “append in interface Appendable”. It returns the Writer.

iii)  append(CharSequence, int, int)

Syntax:

public Writer append (CharSequence csq, int start, int end) throws IOException
  • csq – The character sequence from which a subsequence will be appended. If csq is null, then characters will be appended as if csq contained the four characters “null”.
  • start – The index of the first character in the subsequence
  • end – The index of the character following the last character in the subsequence
  • IndexOutOfBoundsException – If start or end are negative, start is greater than end, or end is greater than csq.length().
  • IOException – If an I/O error occurs

Description:

  • Appends a subsequence of the specified character sequence to this writer.Appendable.
  • An invocation of this method of the form out.append(csq, start, end) when csq is not null behaves in exactly the same way as the invocation out.write(csq.subsequence(start, end).toString()).
  •  It is specified by “append in interface Appendable”. It returns the Writer

iv)  write(char[]):

Syntax:

public void write(char[] cbuf)throws IOException
  • cbuf – Array of characters to be written
  • IOException – If an I/O error occurs

Description:

Writes an array of characters.

v)  write(String)

Syntax:

public void write(String str)throws IOException
  • str – String to be written
  • IOException – If an I/O error occurs

Description: Writes a string.

Methods inherited from class java.lang.Object

i)    clone()

ii)   equals()

iii)  finalize()

iv)  getClass()

v)   hashCode()

vi)  notify()

vii) notiftAll()

viii)toString()

ix)  wait(…)

x)   wait(,,,)

xi)  wait(—)

 1)      clone():–

Syntax:

protected object clone() throws CloneNotSupportedException
  • CloneNotSupportedException – if the object’s class does not support the Cloneable interface. Subclasses that override the clone method can also throw this exception to indicate that an instance cannot be cloned.

Description:

  • Creates and returns a copy of this object. The precise meaning of “copy” may depend on the class of the object. Returns a clone of this sentence.

2)      equals():–

Syntax:

public boolean equals (Object obj)
  • obj – the reference object with which to compare.true if this object is the same as the obj argument; false otherwise.

Description:

  • Indicates whether some other object is “equal to” this one. The equals method implements an equivalence relation on non-null object references.

3)      finalize():–

Syntax:

protected void finalize() throws Throwable
  • Throwable – the Exception raised by this method

Description:

  • Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. A subclass overrides the finalize method to dispose of system resources or to perform other cleanup.

4)      getClass():–

Syntax:

public final Class <?> getClass()

Description:

  • Returns the runtime class of this Object. The returned Class object is the object that is locked by static synchronized methods of the represented class. It returns the Class object that represents the runtime class of this object.

5)      hashCode():–

Syntax:

                public int hashCode()

Description:

  • Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by HashMap. It returns a hash code value for this object.

6)      notify():–

Syntax:

      public final void notify()

Description:

  • Wakes up a single thread that is waiting on this object’s monitor. If any threads are waiting on this object, one of them is chosen to be awakened. The choice is arbitrary and occurs at the discretion of the implementation. A thread waits on an object’s monitor by calling one of the wait methods.

Throws:

  • IllegalMonitorStateException– if the current thread is not the owner of this object’s monitor.

7)      notifyAll():–

Syntax:

     public final void notifyAll()

Description:

  • Wakes up all threads that are waiting on this object’s monitor. A thread waits on an object’s monitor by calling one of the wait methods.

Throws:

  • IllegalMonitorStateException – if the current thread is not the owner of this object’s monitor.

8)      toString():–

Syntax:

public String toString()

Description:

  • Returns a string representation of the object. In general, the toString method returns a string that “textually represents” this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.It returns a string representation of the object.

9)      wait():–

Syntax:

public final void wait() throws InterruptedException

Description:

  • Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object. In other words, this method behaves exactly as if it simply performs the call wait(0).

Throws:

  • IllegalMonitorStateException – if the current thread is not the owner of the object’s monitor.
  • InterruptedException – if any thread interrupted the current thread before or while the current thread was waiting for a notification. The interrupted status of the current thread is cleared when this exception is thrown.

10)   wait(long):–

Syntax: 

         public final void wait(long timeout) throws InterruptedException
  • timeout – the maximum time to wait in milliseconds.

Description:

  • Causes the current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed.

Throws:

  • IllegalArgumentException – if the value of timeout is negative.
  • IllegalMonitorStateException – if the current thread is not the owner of the object’s monitor.
  • InterruptedException – if any thread interrupted the current thread before or while the current thread was waiting for a notification. The interrupted status of the current thread is cleared when this exception is thrown.

11)  wait(long, int):–

Syntax:

      public final void wait(long timeout, int nanos) throws InterruptedException
  • timeout – the maximum time to wait in milliseconds.
  • nanos – additional time, in nanoseconds range 0-999999.
  • IllegalArgumentException – if the value of timeout is negative or the value of nanos is not in the range 0-999999.
  • IllegalMonitorStateException – if the current thread is not the owner of this object’s monitor.
  • InterruptedException – if any thread interrupted the current thread before or while the current thread was waiting for a notification. The interrupted status of the current thread is cleared when this exception is thrown.

Description:

  • Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed.

 

 

 

Leave a Reply