Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

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.

As usual, the accompanying source code can be found over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments