Java - ObjectStreamWriter getEncoding() method



Description

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.method flushes the stream.

Declaration

Following is the declaration for java.io.ObjectStreamWriter.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 - Usage of ObjectStreamWriter getEncoding() method

The following example shows the usage of ObjectStreamWriter getEncoding() method.

OutputStreamWriterDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

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();
      }
   }
}

Output

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

UTF8
F

Example - Getting default encoding used by OutputStreamWriter

The following example shows the usage of ObjectStreamWriter getEncoding() method.

OutputStreamWriterDemo.java

package com.tutorialspoint;

import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.IOException;

public class OutputStreamWriterDemo {
   public static void main(String[] args) {
      try {
         OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream("encoding1.txt"));

         System.out.println("Encoding used: " + writer.getEncoding());

         writer.write("Sample text.");
         writer.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Encoding used: UTF8

Explanation

  • When you don't specify an encoding, OutputStreamWriter uses the default character encoding of the JVM (e.g., UTF-8, platform dependent).

  • getEncoding() returns the actual encoding used by the writer.

  • Useful for debugging or ensuring correct text file formats.

Example - Specifying and verifying a custom encoding

The following example shows the usage of ObjectStreamWriter getEncoding() method.

OutputStreamWriterDemo.java

package com.tutorialspoint;

import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.IOException;

public class OutputStreamWriterDemo {
   public static void main(String[] args) {
      try {
         // Specify encoding explicitly
         OutputStreamWriter writer = new OutputStreamWriter(
            new FileOutputStream("encoding2.txt"), "UTF-16"
         );

         System.out.println("Encoding used: " + writer.getEncoding());

         writer.write("This text is written in UTF-16.");
         writer.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Encoding used: UTF-16

Explanation

  • You can explicitly provide a character encoding (e.g., "UTF-16").

  • getEncoding() confirms that the writer is using the specified encoding.

  • Ensures compatibility when writing files for systems that expect specific formats (e.g., XML, Windows apps, etc.).

java_io_outputstreamwriter.htm
Advertisements