类 In


  • public final class In
    extends java.lang.Object
    Input. 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 given Scanner source; use with new Scanner(String) to read from a string.
    • 方法概要

      修饰符和类型 方法 说明
      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​(java.lang.String[] args)
      Unit tests the In data type.
      java.lang.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.
      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.
      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()
      已过时。
      static double[] readDoubles​(java.lang.String filename)
      已过时。
      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()
      已过时。
      Replaced by StdIn.readAllInts().
      static int[] readInts​(java.lang.String filename)
      已过时。
      Replaced by new In(filename).readAllInts().
      java.lang.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.
      java.lang.String readString()
      Reads the next token from this input stream and returns it as a String.
      static java.lang.String[] readStrings()
      已过时。
      static java.lang.String[] readStrings​(java.lang.String filename)
      已过时。
      Replaced by new In(filename).readAllStrings().
      • 从类继承的方法 java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 构造器详细资料

      • 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 open socket
        java.lang.IllegalArgumentException - if socket is null
      • In

        public In​(java.net.URL url)
        Initializes an input stream from a URL.
        参数:
        url - the URL
        抛出:
        java.lang.IllegalArgumentException - if cannot open url
        java.lang.IllegalArgumentException - if url is null
      • In

        public In​(java.io.File file)
        Initializes an input stream from a file.
        参数:
        file - the file
        抛出:
        java.lang.IllegalArgumentException - if cannot open file
        java.lang.IllegalArgumentException - if file is null
      • 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 open name as a file or URL
        java.lang.IllegalArgumentException - if name is null
      • In

        public In​(java.util.Scanner scanner)
        Initializes an input stream from a given 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 scanner
        抛出:
        java.lang.IllegalArgumentException - if scanner is null
    • 方法详细资料

      • exists

        public boolean exists()
        Returns true if this input stream exists.
        返回:
        true if this input stream exists; false otherwise
      • isEmpty

        public boolean isEmpty()
        Returns true if input stream is empty (except possibly whitespace). Use this to know whether the next call to readString(), readDouble(), etc will succeed.
        返回:
        true if this input stream is empty (except possibly whitespace); false otherwise
      • hasNextLine

        public boolean hasNextLine()
        Returns true if this input stream has a next line. Use this method to know whether the next call to readLine() will succeed. This method is functionally equivalent to hasNextChar().
        返回:
        true if this input stream has more input (including whitespace); false otherwise
      • hasNextChar

        public boolean hasNextChar()
        Returns true if this input stream has more input (including whitespace). Use this method to know whether the next call to readChar() will succeed. This method is functionally equivalent to hasNextLine().
        返回:
        true if this input stream has more input (including whitespace); false otherwise
      • readLine

        public java.lang.String readLine()
        Reads and returns the next line in this input stream.
        返回:
        the next line in this input stream; null if no such line
      • readChar

        public char readChar()
        Reads and returns the next character in this input stream.
        返回:
        the next char in 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 a String.
        返回:
        the next String in 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 a int, and returns the int.
        返回:
        the next int in this input stream
        抛出:
        java.util.NoSuchElementException - if the input stream is empty
        java.util.InputMismatchException - if the next token cannot be parsed as an int
      • readDouble

        public double readDouble()
        Reads the next token from this input stream, parses it as a double, and returns the double.
        返回:
        the next double in this input stream
        抛出:
        java.util.NoSuchElementException - if the input stream is empty
        java.util.InputMismatchException - if the next token cannot be parsed as a double
      • readFloat

        public float readFloat()
        Reads the next token from this input stream, parses it as a float, and returns the float.
        返回:
        the next float in this input stream
        抛出:
        java.util.NoSuchElementException - if the input stream is empty
        java.util.InputMismatchException - if the next token cannot be parsed as a float
      • readLong

        public long readLong()
        Reads the next token from this input stream, parses it as a long, and returns the long.
        返回:
        the next long in this input stream
        抛出:
        java.util.NoSuchElementException - if the input stream is empty
        java.util.InputMismatchException - if the next token cannot be parsed as a long
      • readShort

        public short readShort()
        Reads the next token from this input stream, parses it as a short, and returns the short.
        返回:
        the next short in this input stream
        抛出:
        java.util.NoSuchElementException - if the input stream is empty
        java.util.InputMismatchException - if the next token cannot be parsed as a short
      • readByte

        public byte readByte()
        Reads the next token from this input stream, parses it as a byte, and returns the byte.

        To read binary data, use BinaryIn.

        返回:
        the next byte in this input stream
        抛出:
        java.util.NoSuchElementException - if the input stream is empty
        java.util.InputMismatchException - if the next token cannot be parsed as a byte
      • readBoolean

        public 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).
        返回:
        the next boolean in this input stream
        抛出:
        java.util.NoSuchElementException - if the input stream is empty
        java.util.InputMismatchException - if the next token cannot be parsed as a boolean
      • 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 by new 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 by new 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 by new 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 by StdIn.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()
        已过时。
        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()
        已过时。
        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 the In data type.
        参数:
        args - the command-line arguments