Class IO
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
readlnmethods to read from the standard input, and they will not mix these calls with other techniques for reading fromSystem.in. - Since:
- 25
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidWrites a string representation of the specified object to the standard output.static voidprintln()Writes a line separator to the standard output.static voidWrites a string representation of the specified object and then writes a line separator to the standard output.static Stringreadln()Reads a single line of text from the standard input.static StringWrites a prompt and then reads a line of input.Methods declared in class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitModifier and TypeMethodDescriptionprotected Objectclone()Answers a new instance of the same class as the receiver, whose slots have been filled in with the values in the slots of the receiver.booleanCompares the argument to the receiver, and answers true if they represent the same object using a class specific comparison.protected voidfinalize()Deprecated, for removal: This API element is subject to removal in a future version.May cause performance issues, deadlocks and hangs.getClass()Answers the unique instance of java.lang.Class which represents the class of the receiver.inthashCode()Answers an integer hash code for the receiver.final voidnotify()Causes one thread which iswaiting on the receiver to be made ready to run.final voidCauses all threads which arewaiting on the receiver to be made ready to run.toString()Answers a string containing a concise, human-readable description of the receiver.final voidwait()Causes the thread which sent this message to be made not ready to run pending some change in the receiver (as indicated bynotifyornotifyAll).final voidwait(long time) Causes the thread which sent this message to be made not ready to run either pending some change in the receiver (as indicated bynotifyornotifyAll) or the expiration of the timeout.final voidwait(long time, int frac) Causes the thread which sent this message to be made not ready to run either pending some change in the receiver (as indicated bynotifyornotifyAll) or the expiration of the timeout.
-
Method Details
-
println
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 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 if
println()had been called onSystem.out. -
print
Writes a string representation of the specified object to the standard output.The effect is as if
print(obj)had been called onSystem.out.- Parameters:
obj- the object to print, may benull
-
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:
- a string containing the line read from the standard input, not
including any line separator characters. Returns
nullif an end of stream has been reached without having read any characters. - Throws:
IOError- if an I/O error occurs
-
readln
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 callingreadln.If necessary, this method first sets up charset decoding, as described in above in the class specification.
- Parameters:
prompt- the prompt string, may benull- Returns:
- a string containing the line read from the standard input, not
including any line separator characters. Returns
nullif an end of stream has been reached without having read any characters. - Throws:
IOError- if an I/O error occurs
-