The OutputStreamWriter Class

The java.io.OutputStreamWriter class connects byte streams and character streams: It writes bytes onto the underlying output stream after translating characters according to a specified character encoding.

The encoding can be set in the constructor, or you can accept the platform's default encoding.

 public OutputStreamWriter(OutputStream out, String enc) 
  throws UnsupportedEncodingException
 public OutputStreamWriter(OutputStream out)

For example, to attach an OutputStreamWriter to System.out with the default encoding:

OutputStreamWriter osw = new OutputStreamWriter(System.out);

On the other hand if you wanted to write a file encoded in UTF-16, you might do this:

FileOutputStream fos = new FileOutputStream("file.utf16.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-16");

public String getEncoding()

The getEncoding() method returns a string containing the name of the encoding used by this writer.

The other methods just override methods from java.io.Writer, but behave identically from the perspective of the programmer.

 public void write(int c) throws IOException
 public void write(char[] text, int offset, int length)
  throws IOException
 public void write(String s, int offset, int length) 
  throws IOException
 public void flush() throws IOException
 public void close() throws IOException

Previous | Next | Top | Cafe au Lait

Copyright 1997 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified August 15, 1997