Java.io.CharArrayWriter.toString() Method
Advertisements
Description
The java.io.CharArrayWriter.toString() method returns a string of the buffered content from this writer.
Declaration
Following is the declaration for java.io.CharArrayWriter.toString() method:
public String toString()
Parameters
NA
Return Value
The method returns string of the buffer content allocated in this writer.
Exception
NA
Example
The following example shows the usage of java.io.CharArrayWriter.toString() method.
package com.tutorialspoint;
import java.io.CharArrayWriter;
public class CharArrayWriterDemo {
public static void main(String[] args) {
char[] ch = {'A','B','C','D','E'};
CharArrayWriter chw = null;
try{
// create character array writer
chw = new CharArrayWriter();
// write character buffer to the writer
chw.write(ch);
// get buffered content as string
String str = chw.toString();
// print the string
System.out.print(str);
}catch(Exception e){
// for any error
e.printStackTrace();
}finally{
// releases all system resources from writer
if(chw!=null)
chw.close();
}
}
}
Let us compile and run the above program, this will produce the following result:
ABCDE