Java IO: Why does the console display a numeric representation of a linear feed when reading from stdin?

Just to better understand what I heard in the lecture (about Java Input and Output-Stream), I made myself this tiny program:

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 a numerical representation of the character I just printed in stdout. It works mostly, but here is what I see in Eclipse:

Screeshot eclipse

"10" is the decimal ASCII representation of the string.

Good. I pressed the enter key to complete the iteration.

But why does this second meaning also appear?

I expect that only the first key will appear (actual character).

If the problem can be explained in some way, I would appreciate his / her answer.

@Sanket Makani Here is the corresponding contents of the text file "text.txt":

2
3
A
a

In Eclipse:

Screenshot

+4
3

, .

, SO, , .

Java

+3

A new line is part of your input stream, so System.in.read () sees this as well. If you only want to print the character codes of characters in a string, you need to filter out the characters in the string.

+1
source

Source: https://habr.com/ru/post/1678090/


All Articles