For example, instead of printing
/
-
\
/
on a new line make it so it stays on开发者_运维百科 one line and is an animation of a spinner?
Yes, print a \b
(backspace) to remove the last character. In a nutshell:
System.out.print('/');
System.out.print('\b');
System.out.print('-');
System.out.print('\b');
System.out.print('\\');
System.out.print('\b');
Note that this doesn't work in Eclipse console due to a bug. In the command console, however, it should work fine.
If you need text cursor positioning, the solution will need to be through JNI. You will need some C cursor positioning software, which will be non portable. Curses was a popular application some 15-20 years ago.
The question is do you really need this trip to the past?
Perhaps you should implement a command-line interface within your app. Then you could take complete control of the command-line's behavior. Libraries such as Clamshell-Cli may do the heavy lifting for you.
import java.io.*;
class Load_Animate
{
static byte anime;
static void animate(int i)
{
try
{
for(int j = 0 ; j<=100 ; j++)
{
switch(anime)
{
case 1:
System.out.print("\r[ \\ ] :" + j + "%");
break;
case 2:
System.out.print("\r[ | ] :" + j + "%");
break;
case 3:
System.out.print("\r[ / ] :" + j + "%");
break;
default:
anime = 0;
System.out.print("\r[ - ] :" + j + "%");
}
anime++;
Thread.sleep(i);
}
}
catch(InterruptedException e)
{
System.out.println(e);
}
}
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int i = Integer.parseInt(br.readLine());
animate(i);
}
}
Here is what I did I used "\r" which is a carriage return, basically pointing back to the first position in the line.
精彩评论