JDK-8294410 : PrintStream does not allow the line separator to be overridden
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 11,17,21,22
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • OS: generic
  • CPU: generic
  • Submitted: 2022-09-19
  • Updated: 2025-04-09
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
tbdUnresolved
Related Reports
Duplicate :  
Description
A DESCRIPTION OF THE PROBLEM :
java.io.PrintStream.println(...), unlike java.io.PrintWriter.println(...), does not allow to change the line break sequence it uses.

This is due to the fact that, contrary to its specification, it does not call print(...) followed by println() but newLine() which is private and therefore cannot be overridden.

E.g.:
/**
 * Prints a String and then terminate the line.  This method behaves as
 * though it invokes {@link #print(String)} and then
 * {@link #println()}.
 *
 * @param x  The {@code String} to be printed.
 */


This issue might be related to https://bugs.java.com/bugdatabase/view_bug.do?bug_id=8215412


FREQUENCY : always



Comments
Changing the value of the system property line.separator mid flight seems dubious. For many APIs, changing the system properties that they use mid-flight has no effect as the value of the property at startup is used. I think this issue should be closed and the submitter strongly discouraged from changing this system property at run-time.
07-10-2023

After applying the following changes: - outputStream.println("q"); - outputStream.println("w"); + outputStream.write("q".getBytes()); + outputStream.write(System.getProperty("line.separator").getBytes()); + outputStream.write("w".getBytes()); + outputStream.write(System.getProperty("line.separator").getBytes()); The outputs become: First separator's length = 2. First multiline string will be small. It's length = 6. Second separator's length = 100. Second multiline string should be huge. It's length = 202.
03-10-2023

The observations on Windows 11: JDK 11: Failed, the line separator is not overridden JDK 17: Failed. JDK 21: Failed. JDK 22ea+33: Failed.
03-10-2023

Additional information of reproducer: ---------- BEGIN SOURCE ---------- public class Main { public static void main(String[] args) throws java.io.IOException { System.out.println("First separator's length = " + System.getProperty("line.separator").length() + "."); System.out.print("First multiline string will be small. It's length = "); System.out.println(getString().length() + "."); System.out.println(); System.setProperty("line.separator", "e".repeat(100)); System.out.println("Second separator's length = " + System.getProperty("line.separator").length() + "."); System.out.print("Second multiline string should be huge. It's length = "); System.out.println(getString().length() + "."); } public static String getString() throws java.io.IOException { try (java.io.PipedOutputStream pipeOut = new java.io.PipedOutputStream(); java.io.PipedInputStream pipeIn = new java.io.PipedInputStream(); java.io.PrintStream outputStream = new java.io.PrintStream(pipeOut); ) { pipeOut.connect(pipeIn); outputStream.println("q"); outputStream.println("w"); outputStream.close(); return new String(pipeIn.readAllBytes()); } } } ---------- END SOURCE ----------
03-10-2023

No response form the submitter. Resolved as incomplete and will re-open when more information received.
27-09-2022

Requested a simple reproducer from the submitter.
20-09-2022