Java OutputStreamWriter Class
What is Java OutputStreamWriterClass?
- The OutputStreamWriter class in Java is a subclass of the Writer class and is used to write characters to an output stream in a specified character encoding. It is commonly used to write text to a file or a network socket
- When an instance of OutputStreamWriter is created, it takes an OutputStream as a parameter, which specifies the output stream to which the characters will be written.
- To understand the Java OutputStreamWriterClass, Read the Complete Article.
Create an OutputStreamWriter:
- To create an OutputStreamWriter, you need to specify an output stream and optionally the character encoding to use.
- Here’s an example of how to create an OutputStreamWriter using a FileOutputStream as the output stream and UTF-8 encoding:
try (OutputStream os = new FileOutputStream("output.txt");
OutputStreamWriter osw = new OutputStreamWriter(os, StandardCharsets.UTF_8)) {
// Use the OutputStreamWriter to write to the output stream
} catch (IOException e) {
System.err.println("Error creating OutputStreamWriter: " + e.getMessage());
}
- In this example, a new FileOutputStream is created with the filename “output.txt”. Then, an OutputStreamWriter is created using the output stream os and specifying UTF-8 encoding.
- The try-with-resources statement is used to automatically close the output stream and OutputStreamWriter after they are no longer needed. You can use the osw object to write characters to the output stream using the various write methods provided by the OutputStreamWriter class.
Methods of OutputStreamWriter:
The OutputStreamWriter class in Java provides several methods for writing characters to an output stream. Here are some of the most commonly used methods:
write(char[] cbuf, int off, int len) – Writes len characters from the cbuf array starting at index off to the output stream. Returns the number of characters written.
write(int c) – Writes a single character to the output stream. Returns void.
write(String str, int off, int len) – Writes len characters from the str string starting at index off to the output stream. Returns void.
flush() – Flushes the output stream, ensuring that all characters have been written. Returns void.
close() – Closes the OutputStreamWriter, flushing any remaining characters to the output stream and releasing any system resources associated with the stream. Returns void
- These methods allow you to write characters to an output stream using an OutputStreamWriter object. The write methods can be used to write characters in various formats, such as arrays of characters and strings.
- The flush method ensures that all characters have been written to the output stream, while the close method is used to release any resources held by the OutputStreamWriter.
Let’s look at the Java OutputStreamWriter to perform certain operations.
Example 1:Java program to OutputStreamWriter to write data to a File Using Write Method.
import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.nio.charset.StandardCharsets; public class Main { public static void main (String[]args) { try { // Create a FileOutputStream object OutputStream os = new FileOutputStream ("output.txt"); // Create an OutputStreamWriter object with UTF-8 encoding OutputStreamWriter osw = new OutputStreamWriter (os, StandardCharsets.UTF_8); // Write data to the file osw. write ("This is some text that will be written to a file using OutputStreamWriter.\n"); osw. write ("It supports UTF-8 encoding and can write various data types.\n"); // Flush the output stream osw.flush (); // Close the output stream osw.close (); System.out.println ("Data written to file successfully."); } catch (IOException e) { System.err.println ("Error writing to file: " + e.getMessage ()); } } }
Output
This is some text that will be written to a file using OutputStreamWriter. It supports UTF-8 encoding and can write various data types.
Example 2 : Java program to OutputStreamWriter to write data to a File Using Get Encoding Method.
import java.io.*; public class Main { public static void main (String[]args) { try { FileOutputStream fos = new FileOutputStream ("output.txt"); OutputStreamWriter osw = new OutputStreamWriter (fos, "UTF-8"); osw.write ("Hello, world!"); String encoding = osw.getEncoding (); osw.close (); System.out.println ("Data written to file using " + encoding + " encoding."); } catch (IOException e) { System.err.println ("Error writing to file: " + e.getMessage ()); } } }
Output
Hello, world!
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment