-1

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.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Diego
  • 31
  • 4
  • Why? Because “that’s how most terminals work” (I quote as that’s what I’ve been told, but I’ve not dived into the reasoning/workings). You could, however, make use of the “backspace” character, [for example](https://stackoverflow.com/questions/15843202/how-to-show-percentage-progress-of-a-downloading-file-in-java-consolewithout-ui/15846788#15846788) – MadProgrammer Nov 04 '22 at 23:01
  • Try to run the same code not within Eclipse but directly in a CMD window and see if the result is different. – Queeg Nov 04 '22 at 23:07
  • 1
    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" – user16320675 Nov 04 '22 at 23:09
  • Does this answer your question? [difference between newLine() and carriage return("\r")](https://stackoverflow.com/questions/3307747/difference-between-newline-and-carriage-return-r) – skomisa Nov 05 '22 at 00:59
  • [Also see the javadoc for System.lineSeparator](https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/lang/System.html#lineSeparator()) – skomisa Nov 05 '22 at 01:01
  • Yes that's not going to work . You're going to need a special library. What you're using is a character from the set of line separators. You were probably thinking of a backspace character but that wouldn't work either as it only works on one character not the whole line, and is anyway not practicable. Console library needed – g00se Nov 05 '22 at 09:31

1 Answers1

0

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).

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Diego
  • 31
  • 4
  • While it's good that you have described a workaround as an answer to your own problem, it doesn't really properly address the actual question that you asked: _"Why does it System.out.print in a new line as if System.out.println() was used?"_ – skomisa Nov 07 '22 at 02:46