public final class In extends 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 Computer Science: An Interdisciplinary Approach by Robert Sedgewick and Kevin Wayne.
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()
Initializes an input stream from standard input.
|
In(File file)
Initializes an input stream from a file.
|
In(Scanner scanner)
Initializes an input stream from a given
Scanner source; use with
new Scanner(String) to read from a string. |
In(Socket socket)
Initializes an input stream from a socket.
|
In(String name)
Initializes an input stream from a filename or web page name.
|
In(URL url)
Initializes an input stream from a URL.
|
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Closes this input stream.
|
boolean |
exists()
Returns true if this input stream exists.
|
boolean |
hasNextChar()
Returns true if this input stream has more input (including whitespace).
|
boolean |
hasNextLine()
Returns true if this input stream has a next line.
|
boolean |
isEmpty()
Returns true if input stream is empty (except possibly whitespace).
|
static void |
main(String[] args)
Unit tests the
In data type. |
String |
readAll()
Reads and returns the remainder of this input stream, as a string.
|
double[] |
readAllDoubles()
Reads all remaining tokens from this input stream, parses them as doubles,
and returns them as an array of doubles.
|
int[] |
readAllInts()
Reads all remaining tokens from this input stream, parses them as integers,
and returns them as an array of integers.
|
String[] |
readAllLines()
Reads all remaining lines from this input stream and returns them as
an array of strings.
|
long[] |
readAllLongs()
Reads all remaining tokens from this input stream, parses them as longs,
and returns them as an array of longs.
|
String[] |
readAllStrings()
Reads all remaining tokens from this input stream and returns them as
an array of strings.
|
boolean |
readBoolean()
Reads the next token from this input stream, parses it as a
boolean
(interpreting either "true" or "1" as true,
and either "false" or "0" as false). |
byte |
readByte()
Reads the next token from this input stream, parses it as a
byte,
and returns the byte. |
char |
readChar()
Reads and returns the next character in this input stream.
|
double |
readDouble()
Reads the next token from this input stream, parses it as a
double,
and returns the double. |
static double[] |
readDoubles()
Deprecated.
Replaced by
StdIn.readAllDoubles(). |
static double[] |
readDoubles(String filename)
Deprecated.
Replaced by
new In(filename).readAllDoubles(). |
float |
readFloat()
Reads the next token from this input stream, parses it as a
float,
and returns the float. |
int |
readInt()
Reads the next token from this input stream, parses it as a
int,
and returns the int. |
static int[] |
readInts()
Deprecated.
Replaced by
StdIn.readAllInts(). |
static int[] |
readInts(String filename)
Deprecated.
Replaced by
new In(filename).readAllInts(). |
String |
readLine()
Reads and returns the next line in this input stream.
|
long |
readLong()
Reads the next token from this input stream, parses it as a
long,
and returns the long. |
short |
readShort()
Reads the next token from this input stream, parses it as a
short,
and returns the short. |
String |
readString()
Reads the next token from this input stream and returns it as a
String. |
static String[] |
readStrings()
Deprecated.
Replaced by
StdIn.readAllStrings(). |
static String[] |
readStrings(String filename)
Deprecated.
Replaced by
new In(filename).readAllStrings(). |
public In()
public In(Socket socket)
socket - the socketIllegalArgumentException - if cannot open socketIllegalArgumentException - if socket is nullpublic In(URL url)
url - the URLIllegalArgumentException - if cannot open urlIllegalArgumentException - if url is nullpublic In(File file)
file - the fileIllegalArgumentException - if cannot open fileIllegalArgumentException - if file is nullpublic In(String name)
name - the filename or web page nameIllegalArgumentException - if cannot open name as
a file or URLIllegalArgumentException - if name is nullpublic In(Scanner scanner)
Scanner source; use with
new Scanner(String) to read from a string.
Note that this does not create a defensive copy, so the scanner will be mutated as you read on.
scanner - the scannerIllegalArgumentException - if scanner is nullpublic boolean exists()
true if this input stream exists; false otherwisepublic boolean isEmpty()
readString(),
readDouble(), etc will succeed.true if this input stream is empty (except possibly whitespace);
false otherwisepublic boolean hasNextLine()
readLine() will succeed.
This method is functionally equivalent to hasNextChar().true if this input stream has more input (including whitespace);
false otherwisepublic boolean hasNextChar()
readChar() will succeed.
This method is functionally equivalent to hasNextLine().true if this input stream has more input (including whitespace);
false otherwisepublic String readLine()
null if no such linepublic char readChar()
char in this input streamNoSuchElementException - if the input stream is emptypublic String readAll()
public String readString()
String.String in this input streamNoSuchElementException - if the input stream is emptypublic int readInt()
int,
and returns the int.int in this input streamNoSuchElementException - if the input stream is emptyInputMismatchException - if the next token cannot be parsed as an intpublic double readDouble()
double,
and returns the double.double in this input streamNoSuchElementException - if the input stream is emptyInputMismatchException - if the next token cannot be parsed as a doublepublic float readFloat()
float,
and returns the float.float in this input streamNoSuchElementException - if the input stream is emptyInputMismatchException - if the next token cannot be parsed as a floatpublic long readLong()
long,
and returns the long.long in this input streamNoSuchElementException - if the input stream is emptyInputMismatchException - if the next token cannot be parsed as a longpublic short readShort()
short,
and returns the short.short in this input streamNoSuchElementException - if the input stream is emptyInputMismatchException - if the next token cannot be parsed as a shortpublic byte readByte()
byte,
and returns the byte.
To read binary data, use BinaryIn.
byte in this input streamNoSuchElementException - if the input stream is emptyInputMismatchException - if the next token cannot be parsed as a bytepublic boolean readBoolean()
boolean
(interpreting either "true" or "1" as true,
and either "false" or "0" as false).boolean in this input streamNoSuchElementException - if the input stream is emptyInputMismatchException - if the next token cannot be parsed as a booleanpublic String[] readAllStrings()
public String[] readAllLines()
public int[] readAllInts()
public long[] readAllLongs()
public double[] readAllDoubles()
public void close()
@Deprecated public static int[] readInts(String filename)
new In(filename).readAllInts().filename - the name of the file@Deprecated public static double[] readDoubles(String filename)
new In(filename).readAllDoubles().filename - the name of the file@Deprecated public static String[] readStrings(String filename)
new In(filename).readAllStrings().filename - the name of the file@Deprecated public static int[] readInts()
StdIn.readAllInts().@Deprecated public static double[] readDoubles()
StdIn.readAllDoubles().@Deprecated public static String[] readStrings()
StdIn.readAllStrings().public static void main(String[] args)
In data type.args - the command-line arguments