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 1st 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 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

In this tutorial, we’re going to explore how to append newlines to a StringBuilder.

First, we’ll discuss why hard-coding newlines aren’t reliable across operating systems. Then, we’ll dive into platform-independent methods for handling newlines in Java.

2. The Challenge of Platform-Dependent Newlines

In Java, when working with Strings and StringBuilders, adding a new line can become tricky because different operating systems handle newlines differently. Thus, if we build an application that runs across multiple platforms, simply adding \n might not always work as expected.

2.1. Newline Representation Conventions

  • Unix/Linux/macOS represent newline using Line Feed(LF) denoted by “\n”.
  • Windows uses a combination of Carriage Return and  Line Feed(CRLF) denoted as “\r\n”.

To learn more about line break types and their implications, check out the Difference Between CR LF, LF, and CR Line Break Types.

The following code used “\n” to add a new line in StringBuilder:

StringBuilder sb = new StringBuilder();
sb.append("Line 1");
sb.append("\n");
sb.append("Line 2");

If we hard-code a newline with “\n”,  it may display as expected on the Unix operating system but not on Windows, which expects CRLF (“\r\n”).

3. Platform Independent Methods to Append Newlines

To ensure that our code works consistently across different platforms, it is important to use platform-independent methods. Let’s look at a few options:

3.1. Using System.lineSeparator()

Java provides System.lineSeparator(), which returns the appropriate newline character based on the underlying operating system.

Therefore, this is the recommended way to handle newlines in cross-platform applications:

StringBuilder sb = new StringBuilder();
sb.append("First Line");
sb.append(System.lineSeparator());
sb.append("Second Line");

Here, System.lineSeparator() dynamically returns “\n” for Linux/macOS and “\r\n” for Windows.

3.2. Using System.getProperty(“line.separator”)

An alternative approach is to use System.getProperty(“line.separator”), which returns the newline character specific to the system we’re running on:

StringBuilder sb = new StringBuilder();
sb.append("First Line");
sb.append(System.getProperty("line.separator"));
sb.append("Second Line");

Although System.getProperty(“line.separator”)  works the same way as System.lineSeparator(),  it’s less commonly used because System.lineSeparator() was introduced as a cleaner solution in Java 7.

3.3. Using String.format(“%n”)

Another option is using String.format() with the %n format specifier, which is replaced by the platform-specific newline character:

StringBuilder sb = new StringBuilder();
sb.append("First Line");
sb.append(String.format("%n"));
sb.append("Second Line");

This method also ensures platform independence and is useful when working with formatted strings.

4. Helper Class/Function

To simplify newline handling further, we can encapsulate the logic in a helper function or class.

For example, we can create a helper class that wraps StringBuilder and provides custom methods for appending a new line. This helps avoid code duplication and makes it easier to maintain and update:

public class StringBuilderHelper {

    private StringBuilder sb;

    public StringBuilderHelper() {
        sb = new StringBuilder();
    }

    public StringBuilderHelper append(Object obj) {
        sb.append(obj != null ? obj.toString() : "");
    }

    public StringBuilderHelper appendLineSeparator() {
        sb.append(System.lineSeparator());
    }

    @Override
    public String toString() {
        return sb.toString();
    }
}

Here’s how you can use the StringBuilderHelper class:

@Test
public void whenAppendingString_thenCorrectStringIsBuilt() {
    StringBuilderHelper gsBuilder = new StringBuilderHelper();
    gsBuilder.append("Hello")
      .appendLineSeparator()
      .append("World");

    assertEquals("Hello" + System.lineSeparator() + "World", gsBuilder.toString());
}

This helper class encapsulates StringBuilder and adds an appendNewLine() method that appends a platform-independent newline using System.lineSeparator(). It also returns this(object reference) to allow method chaining, just like StringBuilder.

5. Conclusion

In this article, we explored various ways to add newlines to a StringBuilder. First, we saw that hardcoding platform-specific newlines can lead to issues on different operating systems. Then we discussed platform-independent methods like System.lineSeparator() and String.format(“%n”), which work perfectly with cross-platform applications. Finally, we covered how to create a helper class to simplify adding newlines simplifying newline management by encapsulating the logic and supporting method chaining for a cleaner and more maintainable approach.

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 1st 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 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW