APIdia Launch Partner
Top Description Fields Constructors Methods
java.lang

public final Class IO

extends Object
Class Inheritance
Imports
java.io.BufferedReader, .InputStreamReader, .IOError, .IOException, java.nio.charset.Charset, .StandardCharsets, jdk.internal.util.StaticProperty

A collection of static methods that provide convenient access to System#in and System#out for line-oriented input and output.

The readln() and readln(String) methods decode bytes read from System.in into characters. The charset used for decoding is specified by the stdin.encoding property. If this property is not present, or if the charset it names cannot be loaded, then UTF-8 is used instead. Decoding always replaces malformed and unmappable byte sequences with the charset's default replacement string.

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

API Note

The expected use case is that certain applications will use only the readln methods to read from the standard input, and they will not mix these calls with other techniques for reading from System.in.

Since
25

Field Summary

Modifier and TypeField and Description
private static BufferedReader
br

The BufferedReader used by readln().

Constructor Summary

AccessConstructor and Description
private
IO()

Method Summary

Modifier and TypeMethod and Description
public static void
println(Object
the object to print, may be null
obj
)

Writes a string representation of the specified object and then writes a line separator to the standard output.

public static void
println()

Writes a line separator to the standard output.

pack-priv static synchronized BufferedReader

Returns:

the internal BufferedReader instance
reader
()

On the first call, creates an InputStreamReader to decode characters from System.in, wraps it in a BufferedReader, and returns the BufferedReader.

public static String

Returns:

a string containing the line read from the standard input, not including any line separator characters. Returns null if an end of stream has been reached without having read any characters.
readln
()

Reads a single line of text from the standard input.

public static String

Returns:

a string containing the line read from the standard input, not including any line separator characters. Returns null if an end of stream has been reached without having read any characters.
readln
(String
the prompt string, may be null
prompt
)

Writes a prompt and then reads a line of input.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

brback to summary
private static BufferedReader br

The BufferedReader used by readln(). Initialized under a class lock by the reader() method. All access should be through the reader() method.

Constructor Detail

IOback to summary
private IO()

Method Detail

printlnback to summary
public static void println(Object obj)

Writes a string representation of the specified object and then writes a line separator to the standard output.

The effect is as if println(obj) had been called on System.out.

Parameters
obj:Object

the object to print, may be null

printlnback to summary
public static void println()

Writes a line separator to the standard output.

The effect is as if println() had been called on System.out.

readerback to summary
pack-priv static synchronized BufferedReader reader()

On the first call, creates an InputStreamReader to decode characters from System.in, wraps it in a BufferedReader, and returns the BufferedReader. These objects are cached and returned by subsequent calls.

Returns:BufferedReader

the internal BufferedReader instance

readlnback to summary
public static String readln()

Reads a single line of text from the standard input.

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

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

Returns:String

a string containing the line read from the standard input, not including any line separator characters. Returns null if an end of stream has been reached without having read any characters.

Exceptions
IOError:
if an I/O error occurs
readlnback to summary
public static String readln(String prompt)

Writes a prompt and then reads a line of input.

Writes a prompt as if by calling print, and then reads a single line of text as if by calling readln.

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

Parameters
prompt:String

the prompt string, may be null

Returns:String

a string containing the line read from the standard input, not including any line separator characters. Returns null if an end of stream has been reached without having read any characters.

Exceptions
IOError:
if an I/O error occurs