OutputStreamWriter class

 

see Writer class for descriptions of OutputStreamWriter's methods. They are:

 

.close( ) Closes the stream, flushing it first.

.flush( ) Flushes all associated buffers in a stream without closing it.

.write( int c ) Writes a single char, supplied as the low-order two bytes of a four-byte int.

.write( char[ ] c ) Writes an array of characters.

.write( char[ ] c, int off, int len ) Writes a portion len of a character array, beginning at offset

.write( String s ) Writes a String.

.write( String s, int off, int len ) Writes a portion len of a String, beginning at off

 

plus OutputStreamWriter's own method:

 

.getEncoding( ) mentioned below

 

 

OutputStreamWriter is the only (character stream oriented) Writer class whose name contains the (byte stream oriented) word OutputStream. That is because it is the only Writer class which writes byte streams. It converts Unicode characters in its stream to bytes, according to whatever character encoding is specified in its constructor. It can also write Unicode characters.

 

OutputStreamWriter has constructors allowing you to optionally specify the name of the character encoding that should be used to convert the characters to bytes. If you do not specify one, it simply uses the default file encoding of the default locale.

 

OutputStreamWriter can create Unicode. It does not always have to create byte encodings. The first constructor snippet below creates a Unicode file with UTF-16 encoding. It will contain 16-bit characters. The second example creates a UTF-8 file. The third creates an ASCII file. The fourth and final one creates a file in the machine's default character set.

 

OutputStreamWriter osw = new OutputStreamWriter ( new FileOutputStream( "mynewUTF16file", true ), "UTF-16" ));

OutputStreamWriter osw = new OutputStreamWriter ( new FileOutputStream( "mynewUTF8file", true ), "UTF-8" ));

OutputStreamWriter osw = new OutputStreamWriter ( new FileOutputStream( "mynewASCIIfile", true ), "US-ASCII" ));

OutputStreamWriter osw = new OutputStreamWriter ( new FileOutputStream( "mynewregularfile", true ));

 

OutputStreamWriter adds one method to those provided by Writer. It is .getEncoding( ) for retrieving the name of the character encoding assigned to the stream.

 

An OutputStreamWriter is typically chained to a BufferedWriter for efficiency. i.e.

 

BufferedWriter bw = new BufferedWriter( new OutputStreamWriter( System.out ));

 

Converting byte text streams to chars. OutputStreamWriter can be used to convert any byte file to Unicode chars. A file with UTF-16 encoding is created and printed here.

 

import java.io.*;

 

public class Convert {
 public static void main(String[ ] args) {
 FileInputStream fis = null;
 FileOutputStream fos = null;
 OutputStreamWriter osw = null;
 try {
 fis = new FileInputStream(new File( "inputregularfile" ));
 fos = new FileOutputStream(new File( "outputUTF16file" ));
 osw = new OutputStreamWriter( fos, "UTF-16" );
 int c; 
while ( ( c = fis.read( )) != -1 ) { 
System.out.print( String.valueOf( (char) c )); // also print it
 osw.write( c ); 
} 
} 
catch (IOException ex) { } 
finally {
 try {
 if (fis != null) {
 fis.close( );
 }
 osw.close( );
 } catch (IOException ex) { }
 }
 }
}