OutPutStreamWriter Class

Java OutPutStreamWriter tutorial The OutPutStreamWriter write characters to an output stream, translating characters into bytes according to a specified character encoding . Each OutputStreamWriter incorporates its own CharToByteConverter , and is thus a bridge from character streams to byte streams.
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?

When and where use a Reader/Writer or a Stream?
  1. If you are dealing non-ASCII Unicode characters, e.g. Chinese, use Readers/Writers.
  2. If you are handling with binary data (e.g. an image) use Streams.
  3. If you are dealing ordinary ASCII text (the traditional 0-127 characters) you can use either.