Java StringWriter class is a character stream that collects output from string buffer, which can be used to construct a string.
The StringWriter class extends the Writer class.
In StringWriter class, system resources like network sockets and files are not used, therefore closing the StringWriter is not necessary.
import java.io.*;
public class StringWriterDemo {
public static void main(String[] args) throws IOException {
char[] ary = new char[1024];
StringWriter writer = new StringWriter();
FileInputStream input = null;
BufferedReader buffer = null;
input = new FileInputStream("c://stringwriter.txt");
buffer = new BufferedReader(new InputStreamReader(input, "UTF-8"));
int x;
while ((x = buffer.read(ary)) != -1) {
writer.write(ary, 0, x);
}
System.out.println(writer.toString());
writer.close();
buffer.close();
}
}
The above example helps us to know simple example of StringWriter using BufferedReader to read file data from the stream.