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. Introduction

Different operating systems use different end-of-line (EOL) characters, which might lead to problems when files are transferred or processed between systems. Besides, normalizing EOL characters implies rendering them consistent by using a single format to guarantee uniformity across platforms.

This tutorial provides the different Java methods for normalizing EOL characters.

2. Understanding EOL Characters

In Java, EOL characters represent the end of a line in a text file. Different operating systems use different sequences to denote EOL:

  • Unix/Linux: \n (line feed)
  • Windows: \r\n (carriage Return followed by line feed)
  • Old Mac: \r (carriage return)

3. Using String.replaceAll() Method

One straightforward approach to standardize EOL characters involves using Java’s String class and its replaceAll() method. Let’s walk through how we can implement this approach:

String originalText = "This is a text\rwith different\r\nEOL characters\n";
String expectedText = "This is a text" + System.getProperty("line.separator")
  + "with different" + System.getProperty("line.separator") + "EOL characters" + System.getProperty("line.separator");

@Test
public void givenText_whenUsingStringReplace_thenEOLNormalized() {
    String normalizedText = originalText.replaceAll("\\r\\n|\\r|\\n", System.getProperty("line.separator"));
    assertEquals(expectedText, normalizedText);
}

In this test method, we utilize the replaceAll() method to replace all occurrences of (“\r\n“), (“\r“), or (“\n“) with System.getProperty(“line.separator”), ensuring platform-independent normalization of end-of-line characters. Finally, we verify the equality between expectedText and normalizedText using the assertEquals() method.

This method effectively replaces all occurrences of the specified target strings with the platform-specific line separator.

4. Using Apache Commons Lang

Apache Commons Lang provides a rich set of utilities for string manipulation. By leveraging the StringUtils class, we can efficiently normalize EOL characters within text. Here’s the implementation:

@Test
public void givenText_whenUsingStringUtils_thenEOLNormalized() {
    String normalizedText = StringUtils.replaceEach(
      originalText, 
      new String[]{"\r\n", "\r", "\n"}, 
      new String[]{System.getProperty("line.separator"), System.getProperty("line.separator"), System.getProperty("line.separator")});
    assertEquals(expectedText, normalizedText);
}

In this approach, we utilize the StringUtils.replaceEach() method and pass the originalText string along with arrays containing the target strings to be replaced (“\r\n“, “\r“, “\n“) and the corresponding replacement strings obtained from System.getProperty(“line.separator”).

5. Using Java 8 Stream API

Java 8’s Stream API presents a modern and concise approach to processing collections or arrays. By leveraging this API, we can streamline the normalization of EOL characters within text:

@Test
public void givenText_whenUsingStreamAPI_thenEOLNormalized() {
    String normalizedText = Arrays.stream(originalText.split("\\r\\n|\\r|\\n"))
      .collect(Collectors.joining(System.getProperty("line.separator"))).trim();
    assertEquals(expectedText.trim(), normalizedText);
}

Initially, we split the originalText into an array of tokens using the split() method with a regex pattern (“\r\n|\r|\n“). Subsequently, we convert this array into a stream using Arrays.stream(). Finally, we employ the Collectors.joining() method to concatenate the tokens, utilizing System.getProperty(“line.separator”) as the delimiter.

6. Conclusion

In conclusion, whether opting for simplicity with String.replaceAll(), robustness with Apache Commons Lang, or conciseness with Java 8 Stream API, the goal remains consistent: harmonizing EOL characters for enhanced code readability and compatibility.

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