Why System.out.print("\r"message) is printing new lines?

483 Views Asked by At

I want to update the console progress status by rewriting the same line, but each printing gets in a new line. I'm using Windows 10 pro, Eclipse IDE, Oracle Java 1.8 and source encoding is UTF-8.

My code:

String message;
ArrayList<String> fileList = new ArrayList();
fileList.add("file1");
fileList.add("file2");
fileList.add("file3");
for (String fileName : fileList) {
    message = "Procesing file number: " + fileName;
    System.out.print("\r" + message);
}

Console output is (3 different lines):

Procesing file number: file1
Procesing file number: file2
Procesing file number: file3

Why does it System.out.print in a new line as if System.out.println() was used? I have also tried "Character.toChars(13)" and "(char)13" with the very same outcome.

1

There are 1 best solutions below

1
Diego On

Solved, thanks to user16320675's comment:

Eclipse has a setting specifically for that: right-click on the console and select Preferences; or menu Window > Preferences > Run/Debug > Console - last check box: "Interpret Carriage Return (\r) as control character"

I have upvoted the comment (I cannot mark it as the best solution as it is a comment, not an answer).