0

Possible Duplicate:
Write to same location in a console window with java

I was wondering if theres a way, in java, to replace a line of output you outputted to the terminal, such that you could do like a progress bar/counter type thing.

I'd like to do something akin to printing out "Records inserted 1/1000" and then "Records inserted 2/1000" over the top, replacing it so that only the most recent one shows.

Community
  • 1
  • 1
rekh127
  • 139
  • 1
  • 10
  • Indeed that looks like the same thing (a little more specific though), sorry, I guess I just didn't know what to search for. – rekh127 May 21 '12 at 20:24

2 Answers2

3

Print the \r character, which places the cursor at the beginning of the line. And then write the new line.

public static void main(String[] args) throws InterruptedException {

    System.out.print("test");

    Thread.sleep(3000);

    System.out.print('\r');

    System.out.print("lulz");

}
jn1kk
  • 5,012
  • 2
  • 45
  • 72
  • 2
    he doesn't want to print a new line below the last one, he wants to print the new line **over** the last line. – Luiggi Mendoza May 21 '12 at 17:00
  • @LuiggiMendoza This works perfectly. If you are the downvoter, shame on you. – jn1kk May 21 '12 at 17:01
  • 2
    I'm not the downvoter, but when I try this the output is `test(breakline)lulz`. OP needs something like the output from [this image](http://files.cyberciti.biz/uploads/tips/2006/09/prozilla-demo-screenshot.png) (where text is replaced to show the progress). – Luiggi Mendoza May 21 '12 at 17:08
  • @luiggiMendoza, are you on Linux/Mac? – jn1kk May 21 '12 at 17:09
  • On most OS's this will fail (will not over-write the original line) and is not a valid solution. The link given in the comment to the original question gives the valid solutions including using a third party console. – Hovercraft Full Of Eels May 21 '12 at 17:11
  • Windows and Linux environment. This doesn't work. Also, if it worked like @dragon66 states, it would be very inneficient to write the same line like 1000 times and even adding a blank space each time. – Luiggi Mendoza May 21 '12 at 17:13
  • Works on Windows 7. Easiest solution. Definitely not the most robust. Pick what is best for you. – jn1kk May 21 '12 at 17:14
  • You're just forgetting about interoperability with other OS. This is not a worth solution. – Luiggi Mendoza May 21 '12 at 17:24
  • @LuiggiMendoza. No I am not forgetting, that is why I asked you about what OS you are running. OP did not specify what platform, or system he was using. I presented the easiest solution which worked 100% in my cases. Like I said, pick what is best for you (the OP). – jn1kk May 21 '12 at 17:35
  • Remember that Java is cross-platform, so the Java solutions may be cross-platform too. – Luiggi Mendoza May 21 '12 at 17:36
  • @LuiggiMendoza, why are you talking to me like a 5 year old just learning Java? I am done on this topic, hopefully you are too. – jn1kk May 21 '12 at 17:38
  • This is exactly what I was hoping to find, thank you very much, I'd vote you up but I don't have enough reputation (I lurk much more than post >.>). @LuiggiMendoza This worked for me both on Windows and Linux. Did you accidentally do println() instead of print()? Don't mean to be rude, but it's an easy mistake to make. – rekh127 May 21 '12 at 21:03
  • @rekh127 If it is acceptable, just please accept the answer by clicking the check-mark next to the answer. Please remember the solution is highly system dependable, console escape codes might have to be used as well. – jn1kk May 21 '12 at 21:06
  • @LuiggiMendoza thats odd then. Luckily it's not a product but an inhouse tool, so if it's not working exactly right it shouldn't matter too much. (and performance of an output doesn't matter much when you're primarily waiting on disk or network I/O) – rekh127 May 22 '12 at 15:47
0

Just rewire the System.out pipe to go through a filter of your own. e.g. System.setOut(new MyStream(System.out));

https://docs.oracle.com/javase/8/docs/api/java/lang/System.html#setOut-java.io.PrintStream-

You then need to implement MyStream:

public class MyStream extends PrintStream {
    private PrintStream standardOut;

    public MyStream(PrintStream standardOut) {
        this.standardOut = standardOut;
    }

    ... Then here override the appropriate methods (e.g. `println()`, etc...) to correct the output and send it to `standardOut`.
}
mprivat
  • 21,582
  • 4
  • 54
  • 64
  • I don't see how this answers the question. I could do this but I don't know how I would make it so that the println would overwrite when I sent it to standardOut. – rekh127 May 21 '12 at 21:04
  • 1
    how would `System.out = ` work? `System.out` is final. – PixelMaster Jan 31 '18 at 14:44
  • @PixelMaster I guess it was supposed to be pseudocode or something. This does exist, but the syntax is different, yes: https://docs.oracle.com/javase/6/docs/api/java/lang/System.html#setOut%28java.io.PrintStream%29 – jabbink Jul 29 '19 at 13:06
  • System.setOut(new MyStream(System.out)); – mprivat Aug 03 '19 at 14:40