Java System.in.read()的用途是什么?

Java System.in.read()的用途是什么?,java,Java,System.in.read()在java中有什么用途? 请解释一下。从 标准输入可用于在控制台环境中从用户获取输入,但由于此类用户界面没有编辑设施,标准输入的交互使用仅限于教授编程的课程 标准输入的大多数生产用途都是在Unix命令行中工作的程序中使用的。在这种程序中,程序正在处理的有效载荷来自标准输入,程序的结果被写入标准输出。在这种情况下,标准输入永远不会由用户直接写入,而是另一个程序的重定向输出或文件内容 典型的管道如下所示: # list files and directories or

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

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

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

典型的管道如下所示:

# list files and directories ordered by increasing size
du -s * | sort -n
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);
    }
}
从标准输入读取其数据,这实际上是命令的输出。排序后的数据将写入标准输出的
排序
,默认情况下,该输出将在控制台上结束,并且可以轻松地重定向到文件或其他命令


因此,Java中很少使用标准输入。

这个示例可能会对您有所帮助

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

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

System
java.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。实际读取的字节数返回为 整数。此方法阻止,直到输入数据可用,结束 检测到文件,或引发异常


from

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

此示例应该有帮助吗?当然,还有评论>:)

警告:在本段落/帖子中,MAN是一个被过度使用的常用词

总的来说,我建议使用
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");
        }
    }
}

迟到两年半总比不迟到好,对吧?

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

  • 当文档说明它读取一个
    字节时,为什么要声明它返回一个
    int

  • 为什么它看起来会退回垃圾呢?(我键入
    '9'
    ,但它返回
    57

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

注意:他们本可以将其设置为
short
,但他们选择了
int
,这可能是对C具有历史意义的一个提示,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相同。这包括所有英语字母数字字符和流行符号。

为了补充已接受的答案,您也可以使用
System.out.read()
如下所示:

# list files and directories ordered by increasing size
du -s * | sort -n
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);
    }
}
import java.io.IOException;
班级考试{
公共静态void main(字符串args[])引发IOException{
int sn=System.in.read();
系统输出打印项次(序号);
}
}
如果要获取字符输入,必须按如下方式强制转换:
char sn=(char)System.in.read()

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

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

它从输入流中读取一个字节的数据。在这里,回答此类问题的人不应该支持这种行为,必须告诉谷歌,并在提问之前尝试解决方法here@HussainAkhtarWahid“标准输入”的概念(和实际使用)对于Java程序员来说可能非常陌生。
read
方法在Javadoc中有明显的解释,但是
System.in
的预期用途不是。Google System.in也没有透露太多信息。实际上,我是从Google获得的。我没有返回正确的值:例如,当我输入10时,它返回的值是正确的