Re: Console Output control

Discussion in 'Java' started by Chris Smith, Jul 31, 2003.

  1. Chris Smith

    Chris Smith Guest

    Roger Varley wrote:
    > I have a java program that runs from a Windows DOS prompt. It's reading in
    > an input file, re-formatting each record and writing out a new record. I
    > want to display something on the console to reassure my more impatient users
    > that the prgram is running and not frozen by displaying an incrementing
    > record count as each record is converted. So I want to print;
    > System.out.println("Records converted = ");
    > once at the start of the program and then print the record count. According
    > to the documentation System.out.print() will then continue to print where
    > the last print statement left off. After I've printed "1", I then want to to
    > back up the cursor by the number of digits I've just printed and overwrite
    > the "1" with "2" etc etc.
    > Is it possible to control the console cursor position and if so, how?

    You cannot do this in a portable way. Java's standard input and output
    are treated as generalized streams rather than a screen and keyboard
    device specifically. This reflects the fact that standard input,
    output, and error devices can be redirected from the command line, say,
    to a file or a printer, where these kinds of commands would not make

    The straight-forward Java answer is that if you actually want to
    communicate with the user in a polished way, you should use the UI
    classes to do so. If you don't care about being polished, then you can
    just keep appending to standard output. There is no in-between.

    Now, it turns out that you may be able to accomplish what you want, in a
    non-portable way. There are a set of (not particularly standardized)
    escape sequences that terminals will generally interpret as cursor
    movement and other kinds of non-stream manipulation. For example, if
    you send an escape followed by a bracket followed by the right sequence
    of letters and numbers, the terminal might move the cursor, or clear the
    screen, or something like that. Most terminals follow the general
    capabilities of DEC's VT100, plus some less portable extensions. You
    could look those up and try sending them, and see if Windows' DOS prompt
    understands them.

    There are also native libraries to provide more robust ways of
    accomplishing this: jcurses is one that I recall off-hand.

    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
    Chris Smith, Jul 31, 2003
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Finney
    Egor Bolonev
    Jun 30, 2003
  2. Egor Bolonev
    Egor Bolonev
    Jun 30, 2003
  3. Serge Terekhoff
    Serge Terekhoff
    Jun 30, 2003
  4. pratap
    James Kanze
    Jul 19, 2007
  5. Replies:
    James Kanze
    Mar 4, 2008