Java IO Tutorial - Java OutputStreamWriter .getEncoding ()








Syntax

OutputStreamWriter.getEncoding() has the following syntax.

public String getEncoding()

Example

In the following code shows how to use OutputStreamWriter.getEncoding() method.

//  w w w.j  a  v a 2  s  .c  o m


import java.io.*;

public class Main {

   public static void main(String[] args) {

      try {
         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());
         writer.close();
         os.close();

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

The code above generates the following result.