Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8049847

Enhance PrintWriter line.separator handling

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Won't Fix
    • Icon: P4 P4
    • None
    • 6u26
    • core-libs
    • x86
    • windows_vista

      A DESCRIPTION OF THE REQUEST :
      Handling of a PrintWriters line.separator string is awkard when dealing with multiple streams with different conventions.

      Currently, the line separator is fetched from the system properties in the constructer, and used for subsequently.

      In order to use a specific convention for a certain writer, the system properites must be manipulated, which is awkward.

      However, the .format() method does not use the PrintWriters internal variable but instead uses the system parameter, the first time it is needed.

      JUSTIFICATION :
      The printwriter.format("%") does not work as expected, as it does not follow the convention of printwriter.println() if the system property "line.separator" is modified.

      It is awkard to manipulate the system properties in order to configure a printwriter, if writing different types of files.






      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      PrintWriter should accept a line separator string as a constructor argument .
      The line separator string should be passed on to the Formatter used in .format()
      ACTUAL -
      PrintWriter uses the current system property "line.separator"

      The Formatter used in pw.format() uses a pontially different line separator system property,

      ---------- BEGIN SOURCE ----------
      import java.io.PrintWriter;

      public class Test {

      public static void main(String[] args) {
      System.setProperty("line.separator", "\n");
      PrintWriter pw = new PrintWriter(System.out,true);
      pw.println("Hello world.");
      System.setProperty("line.separator", "xxx\n");
      pw.println("Hello again.");
      pw.format("Hello yet another time.%n");
      }


      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Need to do:

      pw.format("hello");
      pw.println();

      instead of

      pw.format("hello%n");

      Which works for simple strings, but is awkward for multi line strings.

            Unassigned Unassigned
            coffeys Sean Coffey
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported: