Java.io - SPLessons

Java.io OutputStreamWriter

Home > Lesson > Chapter 35
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java.io OutputStreamWriter

Java.io OutputStreamWriter

shape Introduction

The Java.io OutputStreamWriter is the class used to convert the character (which is in the form of text) into the bytes. Here the text will be in UTF - 8 format, so to read the characters in that format use output stream writer which will wraps the output stream.

shape Conceptual figure

Class Declaration

shape Declaration

Java.io OutputStreamWriter class is declared below: Public abstract class Java.io OutputStreamWriter extends Writer

shape Fields

Java.io OutputStreamWriter class fields are:
  • Protected Object Lock : For synchronize operations this object is used in OutputStreamWriter.

Class Constructors

shape Table

Constructor Description
OutputStreamWriter(OutputStream out) The function of this constructor is to create the output stream writer instance.
OutputStreamWriter(OutputStream out, Charset cs) The function of this constructor is to create the output stream writer instance with specified charset.
OutputStreamWriter(OutputStream out, String CharsetName ) The function of this constructor is to create the output stream writer instance with specified name of charset.

Class Methods

shape Table

Method Description
void close () The function of this method is to close the present stream and associated to input stream systems resources are released.
void flush () The function of this method is to flush the specified output stream.
void write (int c) The function of this method is to write the represented character into output stream
String getEncoding() The function of this method is to get the name of encoded character which was used by this input stream.
void write (Char[] cbuf, int off, int n) By using this method it writes the data up to ‘n’ bytes from the output stream to an array of characters.

Inherited Methods

shape Description

From the following classes, methods are inherited to the output stream writer class.
  • Java.io.Object
  • Java.io.Writer

shape Example

Following is an example for Java.io OutputStreamWriter. JavaOutputStreamWriterExample.java [java]import java.io.OutputStreamWriter; import java.io.OutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; public class JavaOutputStreamWriterExample { public static void main(String args[]) { OutputStreamWriter osWriter = null; OutputStream outStream = null; File outputFile = null; String str1 = "Hello Java."; String str2 = "Java IO is fun."; try{ outputFile = new File("E:\\splesson.txt"); outStream = new FileOutputStream(outputFile); osWriter = new OutputStreamWriter(outStream); System.out.println("Writing data in file..!!"); osWriter.write(str1); osWriter.append(str2); System.out.println("Data successfully written in file..!!"); } catch(IOException e) { System.out.println("Exception caught..!!"); e.printStackTrace(); } finally { try {//closing objects System.out.println("Flushing object..!!"); outStream.flush(); osWriter.flush(); System.out.println("Closing object..!!"); outStream.close(); osWriter.close(); } catch(IOException ioe) { System.out.println("IOException caught..!!"); ioe.printStackTrace(); } } } } [/java] Java OutputStreamWriter is useful in encoding a character streams to byte streams. The bytes are encoded using the specified charset. It can use a user defined charset or system’s default charset. One must wrap OutputStreamWriter within a java BufferedWriter class, it is a good practice and it also improves program efficiency. Those strings will be stored in the given file. Output Following is the text will be displayed in the console. [java]Writing data in file..!! Data successfully written in file..!! Flushing object..!! Closing object..!! [/java] Now check the data in the file. [java]Hello Java.Java IO is fun.[/java]

Summary

shape Key Points

  • The output stream writer is the class used to convert the character (which is in the form of text) into the bytes.
  • Here the text will be in UTF - 8 format, so to read the characters in that format use output stream writer which will wraps the output stream.