Signup/Sign In

Java OutputStreamWriter

In this tutorial, we will learn about the OutputStreamWriter class in Java. This class belongs to java.io package. This class is also known as bridge between byte streams and character streams because this class converts its characters into bytes.

Constructor

All the variants of constructor by this class are given in the table below.

Constructor Description
OutputStreamWriter(OutputStream out) It creates an OutputStreamWriter that uses the default character encoding.
OutputStreamWriter(OutputStream out, Charset cs) It creates an OutputStreamWriter that uses the given charset.
OutputStreamWriter(OutputStream out, CharsetEncoder enc) It creates an OutputStreamWriter that uses the given charset encoder.
OutputStreamWriter(OutputStream out, String charsetName) It creates an OutputStreamWriter that uses the named charset.

Methods

All the methods of OutputStreamWriter class are given in the table below.

Methods Description
void close() It closes the stream, flushing it first.
void flush() It flushes the stream.
String getEncoding() It returns the name of the character encoding being used by this stream.
void write(char[] cbuf, int off, int len) It writes a portion of an array of characters.
void write(int c) It writes a single character.
void write(String str, int off, int len) It writes a portion of a string.

Example of OutputStreamWriter.

In the following method, we are writing data to the file using OutputStreamWriter class while creating an object of this class we pass the stream as an argument to the constructor, and then using the write method we write the characters to the file.

package studytonight;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
public class StudyTonight 
{
	public static void main(String args[])
	{
		try
		{
			OutputStream stream = new FileOutputStream("E:\\studytonight\\output.txt"); 
			OutputStreamWriter streamWriter = new OutputStreamWriter(stream);            
			streamWriter.write(65); 
			streamWriter.write(66); 
			streamWriter.write(67); 
			streamWriter.write(68); 
			streamWriter.write(69);    
			streamWriter.flush();            
			streamWriter.close(); 
			System.out.println("Data is written to the file successfully...");
		}
		catch(Exception e)
		{
			System.out.println("Error: "+e.toString());
		}
	}
}


Data is written to the file successfully...

ABCDE

Example of OutputStreamWriter

In the following example, we are implementing the overloaded method of write() method. In the method write(char[] str, int offset, int len), str is a string which we want to write, offset is a starting position from where it will write and last parameter len is a length of the string up to which it will write.

package studytonight;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
public class StudyTonight 
{
	public static void main(String args[])
	{
		try
		{
			OutputStream stream = new FileOutputStream("E:\\studytonight\\output.txt"); 
			OutputStreamWriter streamWriter = new OutputStreamWriter(stream);            
			String str="Hello Studytonight";
			streamWriter.write(str, 6, 12);    
			streamWriter.flush();            
			streamWriter.close(); 
			System.out.println("Data is written to the file successfully...");
		}
		catch(Exception e)
		{
			System.out.println("Error: "+e.toString());
		}
	}
}


Data is written to the file successfully...

output.txt

Studytonight

Conclusion

In this tutorial, we learned about OutputStreamWriter class in Java. This class is also known as a bridge between byte streams and character streams because this class converts its characters into bytes.



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.