English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, we will learn about Java OutputStreamWriter and its methods with the help of examples.
The OutputStreamWriter class in the java.io package can be used to convert character data into byte data.
It inherits from the abstract class Writer.
The OutputStreamWriter class can be used with other output streams. It is also known as a bridge between byte streams and character streams because OutputStreamWriter converts its characters to bytes.
For example, some characters may require2bytes are stored in memory. To write this type of data, we can use the output stream writer, which converts characters to the corresponding bytes and stores them together.
To create OutputStreamWriter, we must first import the java.io.OutputStreamWriter package. After importing the package, we can create the output stream writer here.
//Create an OutputStream FileOutputStream file = new FileOutputStream(String path); //Create an OutputStreamWriter OutputStreamWriter output = new OutputStreamWriter(file);
In the above example, we created an OutputStreamWriter named output and a FileOutputStream named file.
Here, we use the default character encoding to write characters to the output stream.
However, we can specify the character encoding type used for writing data (UTF8OrUTF16)
//Create an OutputStreamWriter, specifying the character encoding OutputStreamWriter output = new OutputStreamWriter(file, Charset cs);
Here, we use the Charset class to specify the type of character encoding.
The OutputStreamWriter class provides implementations for different methods that appear in the Writer class.
write() -Write a character to the writer
write(char[] array) - Write the characters in the specified array to the writer
write(String data) - Write the specified string to the writer
import java.io.FileOutputStream; import java.io.OutputStreamWriter; public class Main {}} public static void main(String args[]) { String data = "This is a line of text inside the file."; try { // Create FileOutputStream FileOutputStream file = new FileOutputStream("output.txt"); //Create an OutputStreamWriter OutputStreamWriter output = new OutputStreamWriter(file); //Write a string to the file output.write(data); //Close the writer output.close(); } catch (Exception e) { e.getStackTrace(); } } }
In the above example, we used file output stream to create an output stream reader. The output stream reader is withoutput.txtFile link.
FileOutputStream file = new FileOutputStream("output.txt"); OutputStreamWriter output = new OutputStreamWriter(file);
To write data to the file, we used the write() method.
Here, when we run the program,output.txtThe file will be filled with the following content.
This is a line of text inside the file.
The getEncoding() method can be used to get the encoding type used to write data to the output stream. For example,
import java.io.OutputStreamWriter; import java.nio.charset.Charset; import java.io.FileOutputStream; class Main { public static void main(String[] args) { try { // Create an output stream FileOutputStream file = new FileOutputStream("output.txt"); // Create an output stream reader with default encoding OutputStreamWriter output1 = new OutputStreamWriter(file); // Create an output stream reader with specified encoding OutputStreamWriter output2 = new OutputStreamWriter(file, Charset.forName("UTF8"); // Return the character encoding of the output stream System.out.println("output1The character encoding: " + output1.getEncoding()); System.out.println("output2The character encoding: " + output2.getEncoding()); // Close the reader output1.close(); output2.close(); } catch(Exception e) { e.getStackTrace(); } } }
Output Results
output1with the character encoding: Cp1252 output2with the character encoding: UTF8
In the above example, we created2output stream writers, named output1and output2.
output1No character encoding specified. Therefore, the getEncoding() method returns the default character encoding.
output2Specify Character EncodingUTF8. Therefore, the getEncoding() method returns the specified character encoding.
Note: We have specified the type of character encoding using the Charset.forName() method.
To close the output stream writer, we can use the close() method. Once the close() method is called, data cannot be written to the writer.
Method | Description |
---|---|
flush() | Force all existing data in the writer to be written to the corresponding destination |
append() | Insert the specified character into the current writer |