Java OutputStreamWriter

Java OutputStreamWriter is a class in the Java IO API that bridges character streams to byte streams. It writes characters to a byte stream using a specified character encoding.

It is commonly used when we want to write character data to a stream, such as a file or a network socket. The OutputStreamWriter converts the character data to bytes and writes them to the underlying output stream.

Here is an example of how to use OutputStreamWriter:

import java.io.*;

public class Example {
   public static void main(String[] args) throws IOException {
      FileOutputStream fos = new FileOutputStream("output.txt");
      OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
      
      osw.write("Hello World!");
      osw.close();
   }
}

In the example above, we create a FileOutputStream object to write data to a file called “output.txt”. We then create an OutputStreamWriter object that wraps the FileOutputStream object and specifies the character encoding to use as UTF-8. Finally, we write the string “Hello World!” to the OutputStreamWriter using the write() method and close the stream using the close() method.

Constructor:

The OutputStreamWriter class has several constructors, but the most commonly used constructor takes two arguments:

public OutputStreamWriter(OutputStream out, String charsetName) throws UnsupportedEncodingException
  • out: The OutputStream object to write to.
  • charsetName: The name of the character set encoding to use.

Here is an example of how to create an OutputStreamWriter object using this constructor:

OutputStream outputStream = new FileOutputStream("file.txt");
OutputStreamWriter writer = new OutputStreamWriter(outputStream, "UTF-8");

In this example, we create a FileOutputStream object to write data to a file called “file.txt”. We then create an OutputStreamWriter object that wraps the FileOutputStream object and specifies the character encoding to use as UTF-8.

Note that the constructor may throw an UnsupportedEncodingException if the specified character set is not supported by the system. In such cases, you should handle the exception appropriately, such as by using a default character set.

Methods:

The OutputStreamWriter class provides several methods for writing character data to the underlying output stream:

  1. void write(int c) throws IOException: Writes a single character to the output stream.
  2. void write(char[] cbuf) throws IOException: Writes an array of characters to the output stream.
  3. void write(char[] cbuf, int off, int len) throws IOException: Writes a portion of an array of characters to the output stream.
  4. void write(String str) throws IOException: Writes a string to the output stream.
  5. void write(String str, int off, int len) throws IOException: Writes a portion of a string to the output stream.
  6. void flush() throws IOException: Flushes the output stream.
  7. void close() throws IOException: Closes the output stream.

Here is an example of how to use some of these methods:

OutputStream outputStream = new FileOutputStream("file.txt");
OutputStreamWriter writer = new OutputStreamWriter(outputStream, "UTF-8");

writer.write("Hello World!");
writer.write('\n');
char[] chars = {'H', 'e', 'l', 'l', 'o'};
writer.write(chars, 0, 3);
writer.flush();
writer.close();

In this example, we write the string “Hello World!” to the output stream, followed by a newline character. We then write a portion of a char array containing the characters ‘H’, ‘e’, ‘l’, ‘l’, ‘o’, and flush and close the stream.

Example:

Here is an example that demonstrates how to use the OutputStreamWriter to write a string to a file:

import java.io.*;

public class OutputStreamWriterExample {
    public static void main(String[] args) {
        try {
            FileOutputStream fos = new FileOutputStream("output.txt");
            OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");

            String message = "Hello, world!";
            osw.write(message);

            osw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

In this example, we create a FileOutputStream object to write data to a file called “output.txt”. We then create an OutputStreamWriter object that wraps the FileOutputStream object and specifies the character encoding to use as UTF-8.

We then write the string “Hello, world!” to the OutputStreamWriter using the write() method and close the stream using the close() method.

This will write the string “Hello, world!” to the “output.txt” file in the specified character encoding (UTF-8 in this case).