public final class In extends java.lang.Object
The Locale used is: language = English, country = US. This is consistent
with the formatting conventions with Java floating-point literals,
command-line arguments (via Double.parseDouble(String)
)
and standard output.
For additional documentation, see Section 3.1 of Introduction to Programming in Java: An Interdisciplinary Approach by Robert Sedgewick and Kevin Wayne.
The underlying mechanism is that of java.util.Scanner,
which breaks input into tokens delimited by whithespace.
Like Scanner
, reading a token also consumes preceding Java
whitespace, reading a full line consumes
the following end-of-line delimeter, while reading a character consumes
nothing extra.
Whitespace is defined in Character.isWhitespace(char)
. Newlines
consist of \n, \r, \r\n, and Unicode hex code points 0x2028, 0x2029, 0x0085;
see
Scanner.java (NB: Java 6u23 and earlier uses only \r, \r, \r\n).
Constructor and Description |
---|
In()
Create an input stream from standard input.
|
In(java.io.File file)
Create an input stream from a file.
|
In(java.util.Scanner scanner)
Create an input stream from a given Scanner source; use with
new Scanner(String) to read from a string.
|
In(java.net.Socket socket)
Create an input stream from a socket.
|
In(java.lang.String s)
Create an input stream from a filename or web page name.
|
In(java.net.URL url)
Create an input stream from a URL.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Close the input stream.
|
boolean |
exists()
Does the input stream exist?
|
boolean |
hasNextChar()
Is the input empty (including whitespace)? Use this to know
whether the next call to
readChar() will succeed. |
boolean |
hasNextLine()
Does the input have a next line? Use this to know whether the
next call to
readLine() will succeed. |
boolean |
isEmpty()
Is the input empty (except possibly for whitespace)? Use this
to know whether the next call to
readString() ,
readDouble() , etc will succeed. |
static void |
main(java.lang.String[] args)
Test client.
|
java.lang.String |
readAll()
Read and return the remainder of the input as a string.
|
double[] |
readAllDoubles()
Read all doubles until the end of input is reached, and return them.
|
int[] |
readAllInts()
Read all ints until the end of input is reached, and return them.
|
java.lang.String[] |
readAllStrings()
Read all strings (separated by whitespace)
until the end of input is reached,
and return them as an array of strings.
|
boolean |
readBoolean()
Read and return the next boolean, allowing case-insensitive
"true" or "1" for true, and "false" or "0" for false.
|
byte |
readByte()
Read and return the next byte.
|
char |
readChar()
Read and return the next character.
|
double |
readDouble()
Read and return the next double (delimited by whitespace).
|
static double[] |
readDoubles()
Deprecated.
Clearer to use
StdIn.readAllDoubles() |
static double[] |
readDoubles(java.lang.String filename)
Deprecated.
Clearer to use
new In(filename).
readAllDoubles() |
float |
readFloat()
Read and return the next float (delimited by whitespace).
|
int |
readInt()
Read and return the next int (delimited by whitespace).
|
static int[] |
readInts()
Deprecated.
Clearer to use
StdIn.readAllInts() |
static int[] |
readInts(java.lang.String filename)
Deprecated.
Clearer to use
new In(filename).
readAllInts() |
java.lang.String |
readLine()
Read and return the next line.
|
long |
readLong()
Read and return the next long (delimited by whitespace).
|
short |
readShort()
Read and return the next short (delimited by whitespace).
|
java.lang.String |
readString()
Read and return the next string (delimited by whitespace).
|
static java.lang.String[] |
readStrings()
Deprecated.
Clearer to use
StdIn.readAllStrings() |
static java.lang.String[] |
readStrings(java.lang.String filename)
Deprecated.
Clearer to use
new In(filename).
readAllStrings() |
public In()
public In(java.net.Socket socket)
public In(java.net.URL url)
public In(java.io.File file)
public In(java.lang.String s)
public In(java.util.Scanner scanner)
Note that this does not create a defensive copy, so the scanner will be mutated as you read on.
public boolean exists()
public boolean isEmpty()
readString()
,
readDouble()
, etc will succeed.public boolean hasNextLine()
readLine()
will succeed. Functionally
equivalent to hasNextChar()
.
public boolean hasNextChar()
readChar()
will succeed. Functionally
equivalent to hasNextLine()
.
public java.lang.String readLine()
public char readChar()
public java.lang.String readAll()
public java.lang.String readString()
public int readInt()
public double readDouble()
public float readFloat()
public long readLong()
public short readShort()
public byte readByte()
public boolean readBoolean()
public java.lang.String[] readAllStrings()
public int[] readAllInts()
public double[] readAllDoubles()
public void close()
public static int[] readInts(java.lang.String filename)
readAllInts()
public static double[] readDoubles(java.lang.String filename)
readAllDoubles()
public static java.lang.String[] readStrings(java.lang.String filename)
readAllStrings()
public static int[] readInts()
StdIn.readAllInts()
public static double[] readDoubles()
StdIn.readAllDoubles()
public static java.lang.String[] readStrings()
StdIn.readAllStrings()
public static void main(java.lang.String[] args)