0

I am reading and writing from console created by running my application from a batch file.

I am creating a small servlet and I am running into the problem that while I am typing, things are printed (such as things like clients connecting or disconnecting) on the end of where I am typing. It basically makes everything look funny and I have been trying to find a way to copy what I was typing, print out what occurred (ie. Client x connects!) in its place and paste the command I was typing below so that I can continue.

I think I got a part of it working (the part where what I was typing is replaced), but I cannot think of a way to copy what I was typing or put it on the next line so that I can continue typing to it.

This method is called whenever anything is to be printed out, it basically puts the time stamp on the output.

public void say(String s){
    Calendar calendar = Calendar.getInstance();
    Date now = calendar.getTime();
    Timestamp current = new Timestamp(now.getTime());
    SimpleDateFormat date = new SimpleDateFormat("HH:mm:ss");
    String time = date.format(current);
    //how it gets printed
    System.out.println("\r" + time + "> " + s);
}

The thread I am using to wait for input:

Thread thread = new Thread(){
    public void run(){
        Scanner scan = new Scanner(System.in);
        while(scan.hasNextLine()){
            String s = scan.nextLine();
            //do something with s?
        }
    }
};
thread.start();

Any help would be greatly appreciated!

1 Answers1

0

Maybe in the say method, before it prints anything out, it can check the io buffer or pipeline to see whether it has something (ex: new command you are typing) in it. If so, save it, do what you gotta do, and put it back in the buffer.

syzygy
  • 1,256
  • 2
  • 14
  • 26
  • Could you explain how I would go about checking a buffer for something? – user1563402 Aug 11 '12 at 01:36
  • After looking around for a bit, I couldn't find how to access the console buffer directly from java. Do you really need the system to output to the console? Perhaps it would be better to have it output to a log file, and you could monitor the log file on a separate console? – syzygy Aug 13 '12 at 05:51