StringWriter Example
suggest changeThe Java StringWriter class is a character stream that collects output in a 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 shows a simple use of StringWriter with BufferedReader to read file data from the stream.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents