Javaio and Carriage Return

Asked 2 years ago, Updated 2 years ago, 78 views

Hello. Thank you for always giving me a good answer! I study alone, but whenever I find it difficult to understand even if I search on Google, I am looking for a hash code. Thank you very much.

I'm studying io, but the output value is weird, so I'm here to ask.

package streamEx;

import java.io.IOException;

public class inputStreamTest2 {

    public static void main(String[] args) {
        byte[] input = new byte[5];

        int i = 0;
        int cnt = 0; // count
        int tot = 0; // total
        System.out.println ("Enter in the control window!");
        try {
            while((i= System.in.read(input)) != -1){ 
                cnt++; // Number of repeat statements
                tot += i;

                System.out.println("i: "+i);
                System.out.write(input, 0, i);

            }    }    System.out.println("cnt: "+cnt);

            System.out.println ("byte count read: "+tot");
            System.in.close();
            System.out.close();
        } } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

If I type AppleTest in Eclipse,

i: 5
Applei: 5
Test
i: 1

Is it printed like this?

when outputting through cmd
i: 5
Applei: 5
i: 1t

This is how it's printed.

In my opinion,

i: 5
Applei: 5
i: 1
Test

I thought it should be

And why is there a difference between the output of Eclipse and cmd?

I have a question about the memory structure for the array. If you look at the source, I declared a 5-byte array called input, but when I put the word "AppleTest", I typed 11byte including '/r'/n', but I wonder why it doesn't overflow and why it's all output. If you look at the description, you can see the input stream

"_____/n/rt test TELEPA"(_ is blank.)

It's input Output stream has

" i:5\nApllei:5Test\ri:1\n\n "

It says input is done as input[0] = "Apple", input[1] = "Test\r", input[2] ="\n" Are you saying it goes in like this?

Be careful not to catch a cold since it doesn't catch a cold~~~

io array carriage-return

2022-09-21 22:16

1 Answers

First, you need to understand the differences between \r and \n.

It's an expression of what happens when you change strings in an old typewriter.

When this turns into an electronic typewriter, it's modified so that it happens simultaneously to change the line and move it to the first line of the line.

First of all, Windows handles the above line feed and carriage return separately, and the Unix family expresses both at the same time as \n.

If you type AppleTest,

The size of the input array is 5,

If you output the top in order

i:5
Apple

This is printed first, followed by

i:5
Test\r

When combined with this output

i:5
Applei:5
Test

The cursor is located in front of T. Finally, the output is

i:1
\n

at random, eventually

i:5
Applei:5
i:1t

The position of the locer will output i:1 and the Tes will be overwritten and opened by \n.

I think Eclipse probably treats \r and \n the same way as Unix does. (If you change the settings somewhere, you can do the same thing as cmd.))


2022-09-21 22:16

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.