简体   繁体   English

Java IO:为什么从stdin读取时,换行符的数字表示出现在控制台上?

[英]Java IO: Why does the numerical representation of line feed appear on the console when reading from stdin?

Just for getting a better understanding of what I've heard in a lecture (about Java Input- and Output-Stream) I've made myself this tiny program: 只是为了更好地理解我在讲座中听到的内容(关于Java输入和输出流),我自己做了这个小程序:

public static void main (String[] args) throws Exception {
    int input;
    FileOutputStream fos = new FileOutputStream("test.txt");

    // 95 is the underscore-char ( _ ). I use it
    //  for to finish it reception of input.
    while ((input = System.in.read()) != 95) {
        fos.write(input);
        out.println(input);
    }

    out.println("- Finished -");

    fos.close();
}

It prints the numerical representation of the character I've just typed to stdout. 它打印我刚刚输入到stdout的字符的数字表示。 It works basically but here is what I see in Eclipse: 它基本上有效,但这是我在Eclipse中看到的:

Screeshot Eclipse

"10" is the decimal ASCII representation of the line feed. “10”是换行符的十进制ASCII表示。

Okay. 好的。 I've pressed the enter-key for to finish the iteration. 我按下了回车键来完成迭代。

But why does this second value appear too? 但为什么第二个值也出现了呢?

I would expect only the first key (the actual character) to appear. 我希望只出现第一个键(实际字符)。

If somehow can explain the issue then I would appreciate his / her answer. 如果以某种方式可以解释这个问题,那么我会很感激他/她的回答。

@Sanket Makani Here's the corresponding content of the text-file "text.txt": @Sanket Makani这是文本文件“text.txt”的相应内容:

2
3
A
a

In Eclipse: 在Eclipse中:

截图

InputStreams can be used for binary formats, binary formats don't care about filtering new line characters. InputStreams可用于二进制格式,二进制格式不关心过滤新行字符。

You will either need to filter them yourself, or use a buffered reader/scanner and read line, then iterate along the characters in the string. 您需要自己过滤它们,或者使用缓冲读取器/扫描仪和读取线,然后沿着字符串中的字符进行迭代。

https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#read() https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#read()

Shows that it indeed does only read one character at a time, however the eclipse terminal may not be forwarding the data entered until you press enter. 显示它确实一次只读取一个字符,但是在按Enter键之前,eclipse终端可能不会转发输入的数据。

When that happens your loop is running twice. 当发生这种情况时,你的循环运行两次。

Scanner docs: https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html#nextLine-- 扫描仪文档: https//docs.oracle.com/javase/8/docs/api/java/util/Scanner.html#nextLine--

As you said so yourself, its treating the newline as part of your input as well. 正如你自己所说,它也将新行作为你输入的一部分。

Please refer to this question on SO, it might be helpful to what you wish to accomplish. 请参考SO上的这个问题,它可能对您希望完成的任务有所帮助。

How to read integer value from the standard input in Java 如何从Java中的标准输入读取整数值

The newline is part of your input stream, so System.in.read() sees it as well. 换行符是输入流的一部分,因此System.in.read()也可以看到它。 If you only want to print the character codes of characters within the line, you need to filter out the line feed characters. 如果您只想在行中打印字符的字符代码,则需要过滤掉换行符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM