Java.io.OutputStreamWriter.getEncoding() Method

Advertisements


Description

The java.io.OutputStreamWriter.getEncoding() method returns the name of the character encoding being used by this stream. If the encoding has an historical name then that name is returned; otherwise the encoding's canonical name is returned. If this instance was created with the OutputStreamWriter(OutputStream, String) constructor then the returned name, being unique for the encoding, may differ from the name passed to the constructor. This method may return null if the stream has been closed.

Declaration

Following is the declaration for java.io.OutputStreamWriter.getEncoding() method

public String getEncoding()

Parameters

  • NA

Return Value

This method returns the historical name of this encoding, or possibly null if the stream has been closed

Exception

  • IOException -- if an I/O error occurs.

Example

The following example shows the usage of java.io.OutputStreamWriter.getEncoding() method.

package com.tutorialspoint;

import java.io.*;

public class OutputStreamWriterDemo {

   public static void main(String[] args) {

      try {

         // create a new OutputStreamWriter
         OutputStream os = new FileOutputStream("test.txt");
         OutputStreamWriter writer = new OutputStreamWriter(os);

         // create a new FileInputStream to read what we write
         FileInputStream in = new FileInputStream("test.txt");

         // write something in the file
         writer.write(70);

         // flush the stream
         writer.flush();

         // get and print the encoding for this stream
         System.out.println("" + writer.getEncoding());

         // read what we write
         System.out.println("" + (char) in.read());


      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

Let us compile and run the above program, this will produce the following result:

UTF8
F



Advertisements
Advertisements