类 In
- java.lang.Object
-
- edu.princeton.cs.algs4.In
-
public final class In extends java.lang.ObjectInput. This class provides methods for reading strings and numbers from standard input, file input, URLs, and sockets.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).
-
-
构造器概要
构造器 构造器 说明 In()Initializes an input stream from standard input.In(java.io.File file)Initializes an input stream from a file.In(java.lang.String name)Initializes an input stream from a filename or web page name.In(java.net.Socket socket)Initializes an input stream from a socket.In(java.net.URL url)Initializes an input stream from a URL.In(java.util.Scanner scanner)Initializes an input stream from a givenScannersource; use withnew Scanner(String)to read from a string.
-
方法概要
修饰符和类型 方法 说明 voidclose()Closes this input stream.booleanexists()Returns true if this input stream exists.booleanhasNextChar()Returns true if this input stream has more input (including whitespace).booleanhasNextLine()Returns true if this input stream has a next line.booleanisEmpty()Returns true if input stream is empty (except possibly whitespace).static voidmain(java.lang.String[] args)Unit tests theIndata type.java.lang.StringreadAll()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.java.lang.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.java.lang.String[]readAllStrings()Reads all remaining tokens from this input stream and returns them as an array of strings.booleanreadBoolean()Reads the next token from this input stream, parses it as aboolean(interpreting either"true"or"1"astrue, and either"false"or"0"asfalse).bytereadByte()Reads the next token from this input stream, parses it as abyte, and returns thebyte.charreadChar()Reads and returns the next character in this input stream.doublereadDouble()Reads the next token from this input stream, parses it as adouble, and returns thedouble.static double[]readDoubles()已过时。Replaced byStdIn.readAllDoubles().static double[]readDoubles(java.lang.String filename)已过时。Replaced bynew In(filename).readAllDoubles().floatreadFloat()Reads the next token from this input stream, parses it as afloat, and returns thefloat.intreadInt()Reads the next token from this input stream, parses it as aint, and returns theint.static int[]readInts()已过时。Replaced byStdIn.readAllInts().static int[]readInts(java.lang.String filename)已过时。Replaced bynew In(filename).readAllInts().java.lang.StringreadLine()Reads and returns the next line in this input stream.longreadLong()Reads the next token from this input stream, parses it as along, and returns thelong.shortreadShort()Reads the next token from this input stream, parses it as ashort, and returns theshort.java.lang.StringreadString()Reads the next token from this input stream and returns it as aString.static java.lang.String[]readStrings()已过时。Replaced byStdIn.readAllStrings().static java.lang.String[]readStrings(java.lang.String filename)已过时。Replaced bynew In(filename).readAllStrings().
-
-
-
构造器详细资料
-
In
public In()
Initializes an input stream from standard input.
-
In
public In(java.net.Socket socket)
Initializes an input stream from a socket.- 参数:
socket- the socket- 抛出:
java.lang.IllegalArgumentException- if cannot opensocketjava.lang.IllegalArgumentException- ifsocketisnull
-
In
public In(java.net.URL url)
Initializes an input stream from a URL.- 参数:
url- the URL- 抛出:
java.lang.IllegalArgumentException- if cannot openurljava.lang.IllegalArgumentException- ifurlisnull
-
In
public In(java.io.File file)
Initializes an input stream from a file.- 参数:
file- the file- 抛出:
java.lang.IllegalArgumentException- if cannot openfilejava.lang.IllegalArgumentException- iffileisnull
-
In
public In(java.lang.String name)
Initializes an input stream from a filename or web page name.- 参数:
name- the filename or web page name- 抛出:
java.lang.IllegalArgumentException- if cannot opennameas a file or URLjava.lang.IllegalArgumentException- ifnameisnull
-
In
public In(java.util.Scanner scanner)
Initializes an input stream from a givenScannersource; use withnew 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 scanner- 抛出:
java.lang.IllegalArgumentException- ifscannerisnull
-
-
方法详细资料
-
exists
public boolean exists()
Returns true if this input stream exists.- 返回:
trueif this input stream exists;falseotherwise
-
isEmpty
public boolean isEmpty()
Returns true if input stream is empty (except possibly whitespace). Use this to know whether the next call toreadString(),readDouble(), etc will succeed.- 返回:
trueif this input stream is empty (except possibly whitespace);falseotherwise
-
hasNextLine
public boolean hasNextLine()
Returns true if this input stream has a next line. Use this method to know whether the next call toreadLine()will succeed. This method is functionally equivalent tohasNextChar().- 返回:
trueif this input stream has more input (including whitespace);falseotherwise
-
hasNextChar
public boolean hasNextChar()
Returns true if this input stream has more input (including whitespace). Use this method to know whether the next call toreadChar()will succeed. This method is functionally equivalent tohasNextLine().- 返回:
trueif this input stream has more input (including whitespace);falseotherwise
-
readLine
public java.lang.String readLine()
Reads and returns the next line in this input stream.- 返回:
- the next line in this input stream;
nullif no such line
-
readChar
public char readChar()
Reads and returns the next character in this input stream.- 返回:
- the next
charin this input stream - 抛出:
java.util.NoSuchElementException- if the input stream is empty
-
readAll
public java.lang.String readAll()
Reads and returns the remainder of this input stream, as a string.- 返回:
- the remainder of this input stream, as a string
-
readString
public java.lang.String readString()
Reads the next token from this input stream and returns it as aString.- 返回:
- the next
Stringin this input stream - 抛出:
java.util.NoSuchElementException- if the input stream is empty
-
readInt
public int readInt()
Reads the next token from this input stream, parses it as aint, and returns theint.- 返回:
- the next
intin this input stream - 抛出:
java.util.NoSuchElementException- if the input stream is emptyjava.util.InputMismatchException- if the next token cannot be parsed as anint
-
readDouble
public double readDouble()
Reads the next token from this input stream, parses it as adouble, and returns thedouble.- 返回:
- the next
doublein this input stream - 抛出:
java.util.NoSuchElementException- if the input stream is emptyjava.util.InputMismatchException- if the next token cannot be parsed as adouble
-
readFloat
public float readFloat()
Reads the next token from this input stream, parses it as afloat, and returns thefloat.- 返回:
- the next
floatin this input stream - 抛出:
java.util.NoSuchElementException- if the input stream is emptyjava.util.InputMismatchException- if the next token cannot be parsed as afloat
-
readLong
public long readLong()
Reads the next token from this input stream, parses it as along, and returns thelong.- 返回:
- the next
longin this input stream - 抛出:
java.util.NoSuchElementException- if the input stream is emptyjava.util.InputMismatchException- if the next token cannot be parsed as along
-
readShort
public short readShort()
Reads the next token from this input stream, parses it as ashort, and returns theshort.- 返回:
- the next
shortin this input stream - 抛出:
java.util.NoSuchElementException- if the input stream is emptyjava.util.InputMismatchException- if the next token cannot be parsed as ashort
-
readByte
public byte readByte()
Reads the next token from this input stream, parses it as abyte, and returns thebyte.To read binary data, use
BinaryIn.- 返回:
- the next
bytein this input stream - 抛出:
java.util.NoSuchElementException- if the input stream is emptyjava.util.InputMismatchException- if the next token cannot be parsed as abyte
-
readBoolean
public boolean readBoolean()
Reads the next token from this input stream, parses it as aboolean(interpreting either"true"or"1"astrue, and either"false"or"0"asfalse).- 返回:
- the next
booleanin this input stream - 抛出:
java.util.NoSuchElementException- if the input stream is emptyjava.util.InputMismatchException- if the next token cannot be parsed as aboolean
-
readAllStrings
public java.lang.String[] readAllStrings()
Reads all remaining tokens from this input stream and returns them as an array of strings.- 返回:
- all remaining tokens in this input stream, as an array of strings
-
readAllLines
public java.lang.String[] readAllLines()
Reads all remaining lines from this input stream and returns them as an array of strings.- 返回:
- all remaining lines in this input stream, as an array of strings
-
readAllInts
public int[] readAllInts()
Reads all remaining tokens from this input stream, parses them as integers, and returns them as an array of integers.- 返回:
- all remaining lines in this input stream, as an array of integers
-
readAllLongs
public long[] readAllLongs()
Reads all remaining tokens from this input stream, parses them as longs, and returns them as an array of longs.- 返回:
- all remaining lines in this input stream, as an array of longs
-
readAllDoubles
public double[] readAllDoubles()
Reads all remaining tokens from this input stream, parses them as doubles, and returns them as an array of doubles.- 返回:
- all remaining lines in this input stream, as an array of doubles
-
close
public void close()
Closes this input stream.
-
readInts
@Deprecated public static int[] readInts(java.lang.String filename)
已过时。Replaced bynew In(filename).readAllInts().Reads all integers from a file and returns them as an array of integers.- 参数:
filename- the name of the file- 返回:
- the integers in the file
-
readDoubles
@Deprecated public static double[] readDoubles(java.lang.String filename)
已过时。Replaced bynew In(filename).readAllDoubles().Reads all doubles from a file and returns them as an array of doubles.- 参数:
filename- the name of the file- 返回:
- the doubles in the file
-
readStrings
@Deprecated public static java.lang.String[] readStrings(java.lang.String filename)
已过时。Replaced bynew In(filename).readAllStrings().Reads all strings from a file and returns them as an array of strings.- 参数:
filename- the name of the file- 返回:
- the strings in the file
-
readInts
@Deprecated public static int[] readInts()
已过时。Replaced byStdIn.readAllInts().Reads all integers from standard input and returns them an array of integers.- 返回:
- the integers on standard input
-
readDoubles
@Deprecated public static double[] readDoubles()
已过时。Replaced byStdIn.readAllDoubles().Reads all doubles from standard input and returns them as an array of doubles.- 返回:
- the doubles on standard input
-
readStrings
@Deprecated public static java.lang.String[] readStrings()
已过时。Replaced byStdIn.readAllStrings().Reads all strings from standard input and returns them as an array of strings.- 返回:
- the strings on standard input
-
main
public static void main(java.lang.String[] args)
Unit tests theIndata type.- 参数:
args- the command-line arguments
-
-