Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

OutputStreamWriter write() Method in Java

In this tutorial, we will learn about the write() method from the OutputStreamWriter class in Java. This method is used to write the data to the output stream. This method comes in three overloading methods.

Example 1

In this example, we are writing the data to the output stream using write() method, here we are implementing one of the overloading methods of the write() method. void write(String str, int off, int len) this method accepts the string which we are going to write on the output stream with that we also pass an offset from that point it will start writing the data and we also pass the length of that data. Based on all these parameters it will slice the data and write it to the output stream.

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

class StudyTonight
{
	public static void main(String[] args) throws IOException 
	{ 
		String str = "Hello Studytonight";
		try
		{
			OutputStream outputStream = new FileOutputStream("E:\\studytonight\\output.txt");
			OutputStreamWriter writer = new OutputStreamWriter(outputStream);

			writer.write(str, 0, 5);
			writer.flush();	
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	} 
}

output.txt

Hello

Example 2

In this example, we are writing the data to the output stream using the write() method, here we are implementing one of the overloading methods of the write() method. void write(char c) this method accepts the single character and writes it to the output stream.

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

class StudyTonight
{
	public static void main(String[] args) throws IOException 
	{ 
		try
		{
			OutputStream outputStream = new FileOutputStream("E:\\studytonight\\output.txt");
			OutputStreamWriter writer = new OutputStreamWriter(outputStream);

			writer.write('A');
			writer.flush();	
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	} 
}

output.txt

A

Example 3

In this example, we are writing the data to the output stream using write() method, here we are implementing one of the overloading methods of the write() method. void write(char arr[], int off, int len) this method accepts the string which we are going to write on the output stream with that we also pass an offset from that point it will start writing the data and we also pass the length of that data. Based on all these parameters it will slice the data and write it to the output stream.

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

class StudyTonight
{
	public static void main(String[] args) throws IOException 
	{ 
		char arr[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K'};
		try
		{
			OutputStream outputStream = new FileOutputStream("E:\\studytonight\\output.txt");
			OutputStreamWriter writer = new OutputStreamWriter(outputStream);

			writer.write(arr, 3, 5);
			writer.flush();	
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	} 
}

output.txt

DEFGH

Conclusion

In this tutorial, we learned about OutputStreamWriter write() method in Java. This method is used to write the data to the output stream.