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

时间:2013-03-16 07:23:03

标签: java

java中System.in.read()的用途是什么? 请解释一下。

10 个答案:

答案 0 :(得分:26)

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

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");
        }
    }
}

答案 1 :(得分:22)

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

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

  • 为什么声明在文档显示为int时声明返回byte

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

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

注意:他们本可以使其成为short,但他们选择了int,可能是对C的历史意义的一角,getc()函数也返回int,但更重要的是因为short使用起来有点麻烦,(该语言无法指定short字面值,因此您必须指定{ {1}}字面意思并将其转换为int,)以及某些体系结构short的效果优于int

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

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

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

答案 2 :(得分:4)

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();

}

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;

简而言之,来自api:

  

从输入流中读取一些字节并将其存储到   缓冲阵列b。实际读取的字节数返回为   整数。此方法将阻塞,直到输入数据可用,结束   检测到文件,或抛出异常。

来自InputStream.html#read()

答案 3 :(得分:2)

为了补充已接受的答案,您还可以使用System.out.read()这样:

class Example {
    public static void main(String args[])
        throws java.io.IOException { // This works! No need to use try{// ...}catch(IOException ex){// ...}         

        System.out.println("Type a letter: ");
        char letter = (char) System.in.read();
        System.out.println("You typed the letter " + letter);
    }
}

答案 4 :(得分:2)

import java.io.IOException;

class ExamTest{

    public static void main(String args[]) throws IOException{
        int sn=System.in.read();
        System.out.println(sn);
    }
}

如果要获取char输入,则必须像这样进行转换:char sn=(char) System.in.read()

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

答案 5 :(得分:1)

System.in.read()standard input读取。

标准输入可用于在控制台环境中从用户获取输入,但由于此类用户界面没有编辑功能,因此标准输入的交互式使用仅限于教授编程的课程。

标准输入的大多数生产用途都在旨在在Unix命令行pipelines内工作的程序中。在这样的程序中,程序正在处理的有效负载来自标准输入,程序的结果被写入标准输出。在这种情况下,标准输入永远不会由用户直接写入,它是另一个程序的重定向输出或文件的内容。

典型的管道如下所示:

# list files and directories ordered by increasing size
du -s * | sort -n

sort从标准输入读取数据,实际上是du命令的输出。已排序的数据将写入标准输出sort,默认情况下最终会出现在控制台上,并且可以轻松地重定向到文件或其他命令。

因此,标准输入在Java中相对较少使用。

答案 6 :(得分:1)

这个例子应该有帮助吗?随着评论,当然&gt;:)

警告:此段/邮件中的人是一个过度频繁的词语

总体而言,我建议您使用Scanner课程,因为您可以输入较大的句子,我不完全确定System.in.read有这样的方面。如果可能的话,请指正。

public class InputApp {

// Don't worry, passing in args in the main method as one of the arguments isn't required MAN

    public static void main(String[] argumentalManWithAManDisorder){
        char inputManAger;

        System.out.println("Input Some Crap Man: ");
        try{
            // If you forget to cast char you will FAIL YOUR TASK MAN
            inputManAger = (char) System.in.read();
            System.out.print("You entererd " + inputManAger + " MAN");
        }
        catch(Exception e){
            System.out.println("ELEMENTARY SCHOOL MAN");
        }
    }
}

答案 7 :(得分:0)

它允许您从标准输入(主要是控制台)读取。 This SO question可能会对您有所帮助。

答案 8 :(得分:0)

System.in.read()是System.in类的读取输入方法,它是“标准输入文件”或传统操作系统中的0。

答案 9 :(得分:0)

system.in.read()方法读取一个字节并以整数形式返回,但是如果您输入1到9之间的no,它将返回48个以上的值,因为在ascii代码表中,1-9的ascii值为48 -57。  希望会有所帮助。

相关问题