Class IO

java.lang.Object
java.lang.IO

public final classIOextendsObject
A collection of static methods that provide convenient access toSystem.inandSystem.out for line-oriented input and output.

Thereadln() andreadln(String) methods decode bytes read fromSystem.in into characters. The charset used for decoding is specified by thestdin.encoding property. If this property is not present,or if the charset it names cannot be loaded, then UTF-8 is used instead. Decodingalways replaces malformed and unmappable byte sequences with the charset's defaultreplacement string.

Charset decoding is set up upon the first call to one of thereadln methods.Decoding may buffer additional bytes beyond those that have been decoded to charactersreturned to the application. After the first call to one of thereadln methods,any subsequent use ofSystem.in results in unspecified behavior.

API Note:
The expected use case is that certain applications will use only thereadlnmethods to read from the standard input, and they will not mix these calls withother techniques for reading fromSystem.in.
Since:
25
  • Method Details

    • println

      public static void println(Object obj)
      Writes a string representation of the specified object and then writesa line separator to the standard output.

      The effect is as ifprintln(obj)had been called onSystem.out.

      Parameters:
      obj - the object to print, may benull
    • println

      public static void println()
      Writes a line separator to the standard output.

      The effect is as ifprintln()had been called onSystem.out.

    • print

      public static void print(Object obj)
      Writes a string representation of the specified object to thestandard output.

      The effect is as ifprint(obj)had been called onSystem.out.

      Parameters:
      obj - the object to print, may benull
    • readln

      public static String readln()
      Reads a single line of text from the standard input.

      One line is read from the decoded input as if byBufferedReader.readLine()and then the result is returned.

      If necessary, this method first sets up charset decoding, as described inabove in the class specification.

      Returns:
      a string containing the line read from the standard input, notincluding any line separator characters. Returnsnull if anend of stream has been reached without having read any characters.
      Throws:
      IOError - if an I/O error occurs
    • readln

      public static String readln(String prompt)
      Writes a prompt and then reads a line of input.

      Writes a prompt as if by callingprint, and then reads a singleline of text as if by callingreadln.

      If necessary, this method first sets up charset decoding, as described inabove in the class specification.

      Parameters:
      prompt - the prompt string, may benull
      Returns:
      a string containing the line read from the standard input, notincluding any line separator characters. Returnsnull if anend of stream has been reached without having read any characters.
      Throws:
      IOError - if an I/O error occurs