Java - OutputStreamWriter Class



Introduction

The Java OutputStreamWriter class is a bridge from character streams to byte streams. Characters written to it are encoded into bytes using a specified charset.

Class declaration

Following is the declaration for Java.io.OutputStreamWriter class −

 public class OutputStreamWriter extends Writer 

Field

Following are the fields for Java.io.OutputStreamWriter class −

  • protected Object lock − This is the object used to synchronize operations on this stream.

Class constructors

Sr.No. Constructor & Description
1

OutputStreamWriter(OutputStream out)

This creates an OutputStreamWriter that uses the default character encoding.

2

OutputStreamWriter(OutputStream out, Charset cs)

This creates an OutputStreamWriter that uses the given charset.

3

OutputStreamWriter(OutputStream out, CharsetEncoder enc)

This creates an OutputStreamWriter that uses the given charset encoder.

4

OutputStreamWriter(OutputStream out, String charsetName)

This creates an OutputStreamWriter that uses the named charset.

Class methods

Sr.No. Method & Description
1 void close()

This method closes the stream, flushing it first.

2 void flush()

This method flushes the stream.

3 String getEncoding()

This method returns the name of the character encoding being used by this stream.

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

This method writes a portion of an array of characters.

5 void write(int c)

This method writes a single character.

6 void write(String str, int off, int len)

This method writes a portion of a string.

Methods inherited

This class inherits methods from the following classes −

  • Java.io.Writer
  • Java.io.Object

Example - Using close() to release file resources

The following example shows the usage of ObjectStreamWriter close() 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 { FileOutputStream fos = new FileOutputStream("output_writer1.txt"); OutputStreamWriter writer = new OutputStreamWriter(fos); writer.write("This is written using OutputStreamWriter."); // Close the writer to flush and release resources writer.close(); System.out.println("Data written and stream closed."); } catch (IOException e) { e.printStackTrace(); } } } 

Output

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

 Data written and stream closed. 

Explanation

  • close() flushes any buffered characters and closes the stream.

  • It's essential to prevent data loss and free system resources.

  • If close() is not called, some characters may remain in the buffer and not be written to the file.

Example - Using flush() to force data write before closing

The following example shows the usage of ObjectStreamWriter flush() 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 { FileOutputStream fos = new FileOutputStream("flush_output1.txt"); OutputStreamWriter writer = new OutputStreamWriter(fos); writer.write("Flush ensures data is written immediately."); writer.flush(); // Forces characters to be written to file // Writer is still open here, more data could be written System.out.println("Data flushed to file."); writer.close(); // Finally close the stream } catch (IOException e) { e.printStackTrace(); } } } 

Output

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

 Data flushed to file. 

Explanation

  • write() writes to an internal buffer.

  • flush() pushes that buffered data to the underlying FileOutputStream immediately, without closing the stream.

  • Useful when you want to make sure data is written while keeping the stream open for more output later.

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.

Advertisements