I found that in Eclipse Mars, if you can safely assume that the line you are replacing will be at least as long as the line you are erasing, just printing '\ r' (carriage return) allows your the cursor will return to the beginning of the line to overwrite all the characters that you see. I suppose if the new line is shorter, you can just distinguish between spaces.
This method is pretty convenient in eclipse for real-time progress percentage values, for example, in this piece of code that I pulled from one of my programs. This is part of the program for downloading media files from a website.
URL url=new URL(link); HttpURLConnection connection=(HttpURLConnection)url.openConnection(); connection.connect(); if(connection.getResponseCode()!=HttpURLConnection.HTTP_OK) { throw new RuntimeException("Response "+connection.getResponseCode()+": "+connection.getResponseMessage()+" on url "+link); } long fileLength=connection.getContentLengthLong(); File newFile=new File(ROOT_DIR,link.substring(link.lastIndexOf('/'))); try(InputStream input=connection.getInputStream(); OutputStream output=new FileOutputStream(newFile);) { byte[] buffer=new byte[4096]; int count=input.read(buffer); long totalRead=count; System.out.println("Writing "+url+" to "+newFile+" ("+fileLength+" bytes)"); System.out.printf("%.2f%%",((double)totalRead/(double)fileLength)*100.0); while(count!=-1) { output.write(buffer,0,count); count=input.read(buffer); totalRead+=count; System.out.printf("\r%.2f%%",((double)totalRead/(double)fileLength)*100.0); } System.out.println("\nFinished index "+INDEX); }
HesNotTheStig Aug 22 '15 at 6:06 2015-08-22 06:06
source share