Print a carriage return in java on windows on cons

2019-04-21 07:50发布

On my OS X machine, the following line gives me a nice and easy way to track the state of my loops:

for (int index = 0; index < 100; index++)
    for (int subIndex = index; subIndex < 100; subIndex++)
        System.out.print("\r" + index + "/" + subIndex + "       ");

But when I try to run the same thing on windows, it prints out newlines instead of a carriage return. How can I achieve the same simple method of tracking the process on windows?

2条回答
爷、活的狠高调
2楼-- · 2019-04-21 07:55

I had the statement and it worked in the command prompt

System.out.println("This is Java"+'\r'+"That");

and gives me output as

That is Java

That means it works perfectly.

Note: I run it in Windows 7 with JDK 7 and simple notepad.

It is the problem of eclipse, it will take \r as a new line character and will print

This is Java
That

as output

查看更多
祖国的老花朵
3楼-- · 2019-04-21 08:00

By "Windows" you mean cmd.exe? And you want to overwrite to the line you previously output, right? Unfortunately I think you need to invoke this Win32 API somehow in order to achieve it

Maybe other Java gurus can provide better answer ...

查看更多
登录 后发表回答