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

Implement java.io.IO

    XMLWordPrintable

Details

    • CSR
    • Resolution: Approved
    • P3
    • 23
    • core-libs
    • None
    • source
    • minimal
    • Java API
    • SE

    Description

      Summary

      Create a class containing static methods, suitable for static import, that provide a convenient way to perform I/O operations.

      Problem

      Printing a line requires the code System.out.println which is somewhat verbose and which raises a bunch of questions from beginning programmers. A simple method invocation like println would be preferable.

      Reading a line of input from the user is considerably more painful. It requires wrapping System.in inside an InputStreamReader within a BufferedReader, and it also requires exception handling. Using java.io.Console avoids some of this, but it raises other issues. For example, the console can be null, so this case needs to be handled. Again, it would be preferable to have a simple method invocation such as readln. In addition, this method should allow prompting the user for input, and it should allow advanced editing (cursor navigation and character insertion and deletion at locations other than the end of the current line).

      See JEP 477 for more discussion and context regarding automatic static import for implicit classes.

      Solution

      Create a new public class java.io.IO that contains the following methods:

      public static void println(Object obj)
      public static void print(Object obj)
      public static String readln(String prompt)

      These methods invoke the corresponding instance methods on the Console instance returned by System.console(), or else they throw IOError if System.console() returns null.

      Add the following instance methods to the java.io.Console class:

      public Console println(Object obj)
      public Console print(Object obj)
      public String readln(String prompt)

      The print and println methods do the straightforward thing of printing the String representation of the argument, obtained as if by String.valueOf(obj), with or without printing a trailing newline, respectively. The readln method writes the prompt string to the console and then reads a line of input from the console. The Console class already has existing methods that perform similar functions, however, they use string formatting instead of plain strings. Also note that the Console printing methods return Console whereas the IO static methods return void, as method chaining isn't useful for static methods.

      The java.io.Console class is used because prompting and interactive editing require close coordination between the input and output. Reading prompted input includes flushing at the right time and performing terminal control operations such as cursor motion and character insertion/deletion. This is not possible if independent utility methods were layered on top of System.in and System.out.

      The intent is to introduce readln as a common method name for reading a line of text. Retrofitting other classes such as BufferedReader that allow line reading will be handled separately.

      Specification

      The specification consists of:

      • three new methods added to java.io.Console
      • a new class, called java.io.IO, with the three wrapper methods around those new Console methods

      For details see the attached diff.

      Attachments

        1. diff.txt
          13 kB
        2. diff2.txt
          13 kB

        Issue Links

          Activity

            People

              smarks Stuart Marks
              jlaskey Jim Laskey (Inactive)
              Pavel Rappo
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: