Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

We often work with OutputStream when we need to transfer data to external destinations such as files and networks. Data can either be in binary or string format. We use OutputStream, which is a byte stream, to deal with binary data, and Writer, which is a character stream, to deal with string data.

However, there are situations where we have to write strings into an OutputStream due to the limitations imposed by the selected API. In some instances, the API may only provide OutputStream instead of Writer. In this tutorial, we’ll explore different methods for writing strings into OutputStream in such scenarios.

2. Byte Conversion

The most intuitive approach involves converting the string into bytes and then writing the converted bytes into the OutputStream:

String str = "Hello";
byte[] bytes = str.getBytes(StandardCharsets.UTF_8);
outputStream.write(bytes);

This approach is straightforward but has a major drawback. We need to explicitly convert the string into bytes beforehand and specify the character encoding each time when calling getBytes(). This makes our code cumbersome.

3. OutputStreamWriter

A better approach is to utilize OutputStreamWriter to wrap our OutputStream. OutputStreamWriter serves as a wrapper that transforms a character stream into a byte stream. Strings written to it are encoded into bytes using the chosen character encoding.

We write the string via the write() method without specifying the character encoding. Each invocation of write() on OutputStreamWriter implicitly converts the string into encoded bytes, providing a more streamlined process:

try (OutputStreamWriter writer = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8)) {
    writer.write("Hello");
}

If the wrapped OutputStream is not yet buffered, we advise using the BufferedWriter to wrap around the OutputStreamWriter to make the writing process more efficient:

BufferedWriter bufferedWriter = new BufferedWriter(writer);

This reduces IO operations by first writing data to a buffer and then flushing the buffered data to the stream in a larger chunk.

4. PrintStream

Another option is to utilize PrintStream, which provides functionality similar to OutputStreamWriter. Similar to OutputStreamWriter, we can specify the encoding when we instantiate PrintStream:

try (PrintStream printStream = new PrintStream(outputStream, true, StandardCharsets.UTF_8)) {
    printStream.print("Hello");
}

If we don’t define the character encoding explicitly in the constructor, the default encoding will be UTF-8:

PrintStream printStream = new PrintStream(outputStream);

The difference between PrintStream and OutputStreamWriter is that PrintStream provides additional print() methods for writing different data types to the OutputStream. Also, PrintWriter never throws an IOException for its print() and write() methods:

printStream.print(100); // integer
printStream.print(true); // boolean

5. PrintWriter

PrintWriter serves a similar purpose to PrintStream, offering functionality for writing formatted representations of data to an OutputStream:

try (PrintWriter writer = new PrintWriter(outputStream)) {
    writer.print("Hello");
}

Besides wrapping OutputStream, PrintWriter provides additional constructors to wrap Writer as well. Another difference between them is that PrintWriter provides write() methods for writing character arrays, whereas PrintStream provides write() methods for writing byte arrays:

char[] c = new char[] {'H', 'e', 'l' ,'l', 'o'};
try (PrintWriter writer = new PrintWriter(new StringWriter())) {
    writer.write(c);
}

6. Conclusion

In this article, we’ve explored several approaches for writing strings to an OutputStream in Java.

We began with the straightforward conversion of strings into bytes, which requires explicit encoding for each write operation and impacts the code’s maintainability. Subsequently, we explored three different Java classes that wrap around the OutputStream to encode strings into bytes seamlessly.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW