Why stdout decoding fails when adding carriage return?

The following java code does exactly what is expected:

1      String s = "♪♬♪♪♬♪♪♬♪♪♬♪♪♬♪♪♬♪";
2      for(int i=0; i < s.length(); i++)
3      {
4         System.out.print(s.substring(i,i+1));
5         //System.out.print("\r");
6         Thread.currentThread().sleep(500);
7      }

But when I try to add a carriage return by commenting out line 5, it prints. Why is this and how can I fix it?

(I also tried using "\ u240d" to return a carriage - the same thing).

EDIT: The output goes to bash on Mac OS X.

+3
source share
3 answers

Java does not know what your source file is UTF-8.

If you compile

javac -encoding utf8 MyClass.java

and run

java -Dfile.encoding=utf8 MyClass

he will work.

(Does anyone know why UTF-8 is not the default?)

+1
source

, s.length(), , 18. java - utf-16, String.substring char. 0x1d000 - char. / somthing like icu project - UCharacterIterator

ps: ,

+4

, , .

, . , Java char 16 , char Unicode , String.substring .

, , Java , . , . , , , .

, println("\r"), , . , , . 0x26, 0x6C, , 0x26, 0x10, 0x6C, 0x10, .

+3

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


All Articles