Writer
In this chapter you will learn:
Use Writer
Writer
is the abstract class for writing to character streams.
The following table lists all Writer classes.
Writer
Abstract class that describes character stream outputBufferedWriter
Buffered output character streamCharArrayWriter
Output stream that writes to a character arrayFileWriter
Output stream that writes to a fileFilterWriter
Filtered writerOutputStreamWriter
Output stream that translates characters to bytesPipedWriter
Output pipePrintWriter
Output stream that contains print( ) and println( )StringWriter
Output stream that writes to a string
The following code creates BufferedReader
from StringReader
and wraps PrintWriter
from BufferedWriter
and FileWriter
.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.EOFException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringReader;
//from ja v a 2 s .c om
public class MainClass {
public static void main(String[] args) throws IOException {
try {
BufferedReader in4 = new BufferedReader(new StringReader("asdf"));
PrintWriter out1 = new PrintWriter(new BufferedWriter(new FileWriter("IODemo.out")));
int lineCount = 1;
String s = null;
while((s = in4.readLine()) != null )
out1.println(lineCount++ + ": " + s);
out1.close();
} catch(EOFException e) {
System.err.println("End of stream");
}
}
}
Next chapter...
What you will learn in the next chapter:
Home » Java Tutorial » Reader Writer