System.in.read()有什么用?

gob*_*obi 13 java

System.in.read()在java中有什么用?
请解释一下.

小智 27

可能这个例子会对你有所帮助.

import java.io.IOException;

public class MainClass {

    public static void main(String[] args) {
        int inChar;
        System.out.println("Enter a Character:");
        try {
            inChar = System.in.read();
            System.out.print("You entered ");
            System.out.println(inChar);
        }
        catch (IOException e){
            System.out.println("Error reading from user");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 那是因为ASCII值'1'是49.如果`int inChar`改为`char inChar`,这将按预期工作. (6认同)
  • 我没有返回正确的值:例如,当我输入10时,它返回49. (2认同)

Mik*_*kis 24

迟到两年半比没有好,对吧?

int System.in.read()从输入流中读取下一个数据字节.但我相信你已经知道了,因为查找它是微不足道的.所以,你可能会问的是:

  • int当文档说它读取时,为什么声明它返回byte?

  • 为什么它似乎返回垃圾?(我输入'9',但它返回57.)

它返回一个int因为除了字节的所有可能值之外,它还需要能够返回一个额外的值来指示流结束.因此,它必须返回一种类型,它可以表达比byte罐更多的值.

注意:他们本可以做到这一点short,但是他们选择了int,可能作为对C具有历史意义的帽子的一角,其getc()功能也会返回int,但更重要的是因为short使用起来有点麻烦,(语言提供)没有指定short文字的方法,所以你必须指定一个int文字并将其强制转换为short,)加上某些体系结构int具有更好的性能short.

它似乎返回垃圾,因为当您将字符视为整数时,您正在查看的是该字符的ASCII (*)值.因此,'9'显示为57.但是如果你把它投射到一个角色,你得到'9',所以一切都很好.

可以这样想:如果你输入了字符"9",那么期望System.in.read()返回数字9是没有意义的,因为如果输入了数字,你会期望它返回'a'多少?显然,字符必须映射到数字.ASCII (*)是将字符映射到数字的系统.在这个系统中,字符'9'映射到数字57,而不是数字9.

(*)不一定是ASCII; 它可能是一些其他编码,如UTF-16; 但在绝大多数编码中,当然在所有流行的编码中,前127个值与ASCII相同.这包括所有英文字母数字字符和流行符号.


sub*_*odh 5

Systemjava.lang是包中的最后一个类

来自api源代码的代码示例

public final class System {

   /**
     * The "standard" input stream. This stream is already
     * open and ready to supply input data. Typically this stream
     * corresponds to keyboard input or another input source specified by
     * the host environment or user.
     */
    public final static InputStream in = nullInputStream();

}
Run Code Online (Sandbox Code Playgroud)

read()是抽象类的抽象方法InputStream

 /**
     * Reads the next byte of data from the input stream. The value byte is
     * returned as an <code>int</code> in the range <code>0</code> to
     * <code>255</code>. If no byte is available because the end of the stream
     * has been reached, the value <code>-1</code> is returned. This method
     * blocks until input data is available, the end of the stream is detected,
     * or an exception is thrown.
     *
     * <p> A subclass must provide an implementation of this method.
     *
     * @return     the next byte of data, or <code>-1</code> if the end of the
     *             stream is reached.
     * @exception  IOException  if an I/O error occurs.
     */
    public abstract int read() throws IOException;
Run Code Online (Sandbox Code Playgroud)

简而言之,从 api 来看:

从输入流中读取一定数量的字节并将它们存储到缓冲区数组 b 中。实际读取的字节数以整数形式返回。此方法会阻塞,直到输入数据可用、检测到文件结尾或引发异常。

来自InputStream.html#read()


Man*_*ara 5

import java.io.IOException;\n\nclass ExamTest{\n\n    public static void main(String args[]) throws IOException{\n        int sn=System.in.read();\n        System.out.println(sn);\n    }\n}\n\n
Run Code Online (Sandbox Code Playgroud)\n\n

如果你想获得字符输入,你必须像这样进行转换:char sn=(char) System.in.read()

\n\n

值字节以 0 到 255 范围内的 int 形式返回。但是,与其他语言\xe2\x80\x99 方法不同,System.in.read()一次仅读取一个字节。

\n