OutPutStreamWriter Class

OutputStream os = new FileOutputStream("d:\\test.txt");
Writer osr = new OutputStreamWriter(os);
Characters written to it are encoded into bytes using a specified charset. The encoding used by an OutputStreamWriter may be specified by name, by providing a CharToByteConverter , or by accepting the default encoding, which is defined by the system property file.encoding . It has alternative constructors that allow you to specify the character set (ex: ISO-Latin1, UTF-8, UTF-16 etc.) to use to convert the written characters to the bytes written to the underlying OutputStream .
Example
import java.util.*;
import java.io.*;
public class TestClass{
public static void main( String[] args ){
try {
OutputStream os = new FileOutputStream("d:\\test.txt");
Writer osr = new OutputStreamWriter(os);
osr.write("Java Stream handling !!");
osr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
When do you use a Reader/Writer and when a Stream?

- If you are dealing non-ASCII Unicode characters, e.g. Chinese, use Readers/Writers.
- If you are handling with binary data (e.g. an image) use Streams.
- If you are dealing ordinary ASCII text (the traditional 0-127 characters) you can use either.
Related Topics