Class OutputStreamWriter in Java

Class OutputStreamWriter in Java:

This category of classes decides where our output will go. It may go an array of bytes(No String) or by charterers. We can create Arrays of bytes a file or a Pipe.
In addition,the FilterOutputStream provides a base class for classes that attach the following to OutputStream.

  1. attributes
  2. useful interfaces

The OutputStreamWriter class is a bridge between the byte-oriented world of the
OutputStream class and the character-oriented world of the Writer class.

Ads code goes here

The OutputStreamWriter represents a character stream, but it sends its output to an underlying byte stream.

A character encoding scheme is responsible for translating the Unicode characters to bytes. An OutputStreamWriter can be created using an explicit encoding scheme or a default encoding scheme.

OutputStreamWriter is a character OutputStream that writes data as byte OutputStream so when characters are written to an OutputStreamWriter, it translates them to bytes according to the Locale info or according to platform-specific character encoding. After conversion, it writes those bytes to the specified OutputStream.

For example, to write a Unicode character stream as an ISO-8859-5 byte stream, you can construct an OutputStreamWriter with the encoding 8859_5 as follows:

OutputStreamWriter outr = new OutputStreamWriter(out, "8859_5");

Each time you write to an OutputStreamWriter object, bytes may be written to the underlying byte stream. To improve efficiency, you may want to wrap the utputStreamWriter in a BufferedWriter.

getEncoding() method returns the name of the encoding being used to convert characters to byte.

READ  Class LineNumberInputStream in Java

The class structure of OutputStreamWriter is given as:

public class java.io.OutputStreamWriter extends java.io.Writer{
//constructor
public OutputStreamWriter(OutputStream out,String encoding) 
throws UnsupportedEncodingException;
public OutputStreamWriter(OutputStream out);
//methods:
public void close() throws IOException;
public void flush() throws IOException;
public String getEncoding(();
public void write(int c) throws IOException;
public void write(char[] cbuffer,int offset,int length) throws IOException;
public void write(String str,int offset,int length) throws IOException;
}
 

The details of the class structure are given as follows:

public OutputStreamWriter(OutputStream out);

public OutputStreamWriter(OutputStream out) constructor creates an OutputStreamWriter that writes its data to out and translates characters to bytes using the system’s default encoding scheme.

Parameter
out – The output stream to use.

public OutputStreamWriter(OutputStream out, String enc);

public OutputStreamWriter(OutputStream out, String enc) constructor creates an OutputStreamWriter that writes its data to out and translates characters to bytes using the given encoding scheme.

Parameter
out – The output stream to use.
enc – The name of an encoding scheme.

public void close();

public void close() method calls the close() method of the underlying output stream, which releases any system resources associated with this object.

public void flush();

public void flush() method writes out any buffered data in the internal buffer and calls the flush() method of the underlying output stream, which forces any bytes that may be buffered to be written to the underlying device.

public String getEncoding();

public String getEncoding() method returns the name of the character encoding scheme this OutputStreamWriter is currently using.
This method returns a String that contains the name of the character encoding scheme of this writer.

public void write(int c);

public void write(int c) method converts the given character to bytes using the current encoding scheme and places the converted bytes into an internal buffer. When the buffer fills up, it is written to the underlying byte stream.

READ  Class ParserException in Java

Parameter
c – The value to write.

public void write(char[] cbuf, int off, int len);

public void write(char[] cbuf, int off, int len) method converts len characters from the array cbuf to bytes, starting at offset off, using the current encoding scheme. The method places the converted bytes into an internal buffer. When the buffer fills up, it is written to the underlying byte stream.

Parameter
cbuf – An array of characters to write.
off – An offset into the character array.
len – The number of characters to write.

public void write(String str, int off, int len);

public void write(String str, int off, int len) method converts len characters from the string str to bytes, starting at offset off, using the current encoding scheme. The method places the converted bytes into an internal buffer. When the buffer fills up, it is written to the underlying byte stream.

Parameter
str – The string to be written.
off – An offset into start in the string.
len – The number of characters to write.

Apart from these OutputStreamWriter class also has inherited methods from class- Object. They are as follows:

  • clone()
  • finalize()
  • hashCode()
  • notifyAll()
  • wait()
  • wait(long, int)
  • equals(Object)
  • getClass()
  • notify()
  • toString()
  • wait(long)

OutputStreamWriter class also has inherited methods from class- Writer. They are as follows:

  • write(String)
  • write(char[])

Share and Enjoy !

Leave a Comment

Your email address will not be published. Required fields are marked *