简体   繁体   中英

Java:System.out.println method lost some lines when prints bytes array

I have a bytes array,and then I make a new String by the bytes array,when I debug and evaluate the string,the value is correct.But after I print it out,it lost some lines.

    byte[] bytes = {50, 48, 49, 57, 45, 49, 48, 45, 50, 50, 32, 49, 55, 58, 52, 56, 58, 48, 52, 44, 51, 53, 56, 32, 73, 78, 70, 79, 32, 91, 104, 116, 116, 112, 45, 110, 105, 111, 45, 56, 48, 56, 53, 45, 101, 120, 101, 99, 45, 49, 93, 40, 69, 120, 101, 99, 117, 116, 101, 67, 111, 109, 109, 97, 110, 100, 46, 106, 97, 118, 97, 58, 54, 48, 41, 45, 32, 76, 97, 115, 116, 32, 108, 111, 103, 105, 110, 58, 32, 84, 117, 101, 32, 79, 99, 116, 32, 50, 50, 32, 49, 55, 58, 51, 56, 58, 51, 55, 32, 50, 48, 49, 57, 32, 102, 114, 111, 109, 32, 49, 48, 46, 50, 53, 51, 46, 55, 46, 50, 54, 13, 13, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 95, 13, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 40, 95, 41, 13, 10, 32, 32, 95, 32, 32, 32, 95, 32, 95, 32, 95, 95, 32, 32, 95, 32, 32, 95, 95, 95, 32, 95, 95, 95, 32, 32, 95, 32, 95, 95, 32, 95, 95, 95, 13, 10, 32, 124, 32, 124, 32, 124, 32, 124, 32, 39, 95, 32, 92, 124, 32, 124, 47, 32, 95, 95, 47, 32, 95, 32, 92, 124, 32, 39, 95, 32, 96, 32, 95, 32, 92, 13, 10, 32, 124, 32, 124, 95, 124, 32, 124, 32, 124, 32, 124, 32, 124, 32, 124, 32, 40, 95, 124, 32, 40, 95, 41, 32, 124, 32, 124, 32, 124, 32, 124, 32, 124, 32, 124, 13, 10, 32, 32, 92, 95, 95, 44, 95, 124, 95, 124, 32, 124, 95, 124, 95, 124, 92, 95, 95, 95, 92, 95, 95, 95, 47, 124, 95, 124, 32, 124, 95, 124, 32, 124, 95, 124, 13, 10, 13, 10, 13, 10, 91, 122, 97, 105, 98, 101, 105, 64, 48, 49, 108, 49, 53, 98, 48, 48, 51, 32, 126, 93, 36, 32, 115, 115, 104, 32, 114, 111, 111, 116, 64, 49, 48, 46, 50, 51, 56, 46, 48, 46, 53, 52, 13, 10, 114, 111, 111, 116, 64, 49, 48, 46, 50, 51, 56, 46, 48, 46, 53, 52, 39, 115, 32, 112, 97, 115, 115, 119, 111, 114, 100, 58, 32, 13, 10, 76, 97, 115, 116, 32, 108, 111, 103, 105, 110, 58, 32, 84, 117, 101, 32, 79, 99, 116, 32, 50, 50, 32, 49, 54, 58, 53, 57, 58, 51, 48, 32, 50, 48, 49, 57, 32, 102, 114, 111, 109, 32, 49, 48, 46, 49, 55, 50, 46, 52, 57, 46, 49, 13, 13, 10, 91, 114, 111, 111, 116, 64, 122, 98, 110, 98, 117, 109, 97, 115, 117, 97, 116, 32, 126, 93, 35, 32, 98, 112, 114, 101, 115, 116, 111, 114, 101, 32, 45, 115, 32, 50, 48, 49, 57, 45, 49, 48, 45, 50, 50, 32, 49, 52, 58, 49, 48, 58, 53, 53, 32, 45, 101, 32, 50, 48, 49, 57, 45, 49, 48, 45, 50, 50, 32, 49, 52, 58, 49, 48, 58, 53, 53, 32, 45, 67, 32, 13, 32, 109, 97, 115, 116, 101, 114, 49, 32, 45, 68, 32, 109, 97, 115, 116, 101, 114, 49, 32, 47, 101, 116, 99, 47, 104, 111, 115, 116, 115, 13, 10, 91, 114, 111, 111, 116, 64, 122, 98, 110, 98, 117, 109, 97, 115, 117, 97, 116, 32, 126, 93, 35, 13, 10};
        String s = new String(bytes,"UTF-8");
        System.out.println(s);

when I'm debugging and evaluate the string s, it contains:

[root@zbnbumasuat ~]# bprestore -s 2019-10-22 14:10:55 -e 2019-10-22 14:10:55 -C
master1 -D master1 /etc/hosts

but when I print out it lost lines and only get the result below:

master1 -D master1 /etc/hosts

so could anyone help me to figure out what happened,is it a bug of jdk?

my jdk version is

java version "1.8.0_161" Java(TM) SE Runtime Environment (build 1.8.0_161-b12) Java HotSpot(TM) 64-Bit Server VM (build 25.161-b12, mixed mode)

I asked my friends to run the code,and he gets the same result. and when I run in some online java tools,the result is ok.

The "new line" should be

\r   (Mac) 
\n   (Linux) 
\r\n (Windows)

Because you are not using Mac, if the console meets a standalone \r, it will move the cursor to the beginning of line and overwrite previous line instead of creating new one.

You can check the result of this code:

System.out.println("------------------------");
System.out.println(new String(new byte[]{'a', 13, 'b'}));
System.out.println("------------------------");
System.out.println(new String(new byte[]{'a', 10, 'b'}));
System.out.println("------------------------");
System.out.println(new String(new byte[]{'a', 13, 10, 'b'}));
System.out.println("------------------------");

The result:

------------------------
b
------------------------
a
b
------------------------
a
b
------------------------

So, how to fix? just remove the standalone \r or replace it with \r\n (or \n)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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