我在win平台上用java做一个服务器程序,有一些信息输出到dos的console上,但是用System.out.print输出信息控制不住,它总是滚动的,我想要在固定的行输出信息,请问是不是有其它什么类库可以用,在console上固定的输出信息???我现在的程序如下,它的信息只能滚动输出,我想固定输出,先谢谢了...我只有那么多分,我已经全给了...public class hjserver
{
public static void main(String args[])
{
resnew t1 =  new resnew();
event_move t2 =  new event_move();
t1.start();
t2.start();
}
}
class resnew extends Thread
{
public void run()
{
while(true)
{
try {
sleep(2000);
System.out.print("OK1");
}
catch(InterruptedException e)
{
}
}
}
}
class event_move extends Thread
{ public void run()
{
while(true)
{
try {
sleep(2000);
System.out.print("OK2");
}
catch(InterruptedException e)
{
}
}
}
}

解决方案 »

  1.   

    你所谓的固定输出,就是在一行一直输出对吧?
    你没有使用开发工具么?
    print本来就是在一行打印的。
      

  2.   

    System.out.print("");本身就是在一行输出
      

  3.   

    没明白我的意思,你看我的程序,有2个线程,每个线程有一个输出,但控制不住输出位置,
    程序运行后,它是这样输出的:OK1
    OK2
    OK1
    OK2
    OK1
    OK2
    OK1
    OK2
    OK1
    OK2
    OK1
    OK2
    我想让线程1只在第一行输出,而线程2只在第二行输出.
    OK1
    OK2
      

  4.   

    两行是做不到的,一行倒是可以用这个试试
    package print;import java.util.HashMap;
    import java.util.Map;public class PrintTest2 {
    public static void main(String[] args) {
    final Map<String, Integer> map = new HashMap<String, Integer>();
    final String thname[] = { "Test1", "Test2", "Test3" };
    Runnable r1 = new Runnable() { @Override
    public void run() {
    for (int i = 0; i < 10; i++) {
    synchronized (map) {
    map.put(thname[0], i);
    map.notify();
    }
    try {
    Thread.sleep(1000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    synchronized (map) {
    map.put(thname[0], 9999);
    map.notify();
    }
    }
    };
    Runnable r2 = new Runnable() { @Override
    public void run() {
    for (int i = 0; i < 10; i++) {
    synchronized (map) {
    map.put(thname[1], i);
    map.notify();
    }
    try {
    Thread.sleep(1000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    synchronized (map) {
    map.put(thname[1], 9999);
    map.notify();
    }
    }
    };
    Runnable r3 = new Runnable() {
    private int t1 = 0;
    private int t2 = 0; @Override
    public void run() {
    boolean flag1 = true;
    boolean flag2 = true;
    while (flag1 && flag2) {
    synchronized (map) {
    try {
    map.wait();
    } catch (InterruptedException e) {
    continue;
    }
    System.out.printf("\r %s %d,%s %d", thname[0], t1,
    thname[1], t2);
    if (flag1) {
    t1 = map.get(thname[0]);
    if (t1 == 9999) {
    flag1 = false;
    }
    }
    if (flag2) {
    t2 = map.get(thname[1]);
    if (t2 == 9999) {
    flag2 = false;
    }
    }
    }
    }
    }
    };
    Thread t1 = new Thread(r1, thname[0]);
    Thread t2 = new Thread(r2, thname[1]);
    Thread t3 = new Thread(r3, thname[2]);
    t1.start();
    t2.start();
    t3.start();
    }
    }
    只要在输出里一加'\n',就完了,乱了
      

  5.   

    算了,我还是用AWT做界面,把信息输出到txt控件上把...哎。。
      

  6.   

    简单,不要直接调用System.out.print,先用StringBuffer把数据缓存下来,执行完成后,再一次性LOG。
      

  7.   


    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;//import org.hibernate.*;
    //import org.hibernate.cfg.*;
    import po.*;
    public class hjserver
    { public static TextField txt1=new TextField(30);
    public static TextField txt2=new TextField(30);
    public static TextField txt3=new TextField(30); public static void main(String args[])
    {
    resnew t1 =  new resnew();
    event_move t2 =  new event_move();
    t1.start();
    t2.start();
    Panel p1=new Panel(); p1.setLayout(new GridLayout(3,1,0,10)); p1.add(txt1);
    p1.add(txt2);
    p1.add(txt3);
    Frame f = new Frame();
    f.add(p1);
    f.setLayout(new FlowLayout());
    f.setSize(400,300);
    f.setVisible(true);
    }
    }
    class resnew extends Thread
    {
    public void run()
    {
    while(true)
    {
    try {
    sleep(2000);
    hjserver.txt1.setText("OK1");
    }
    catch(InterruptedException e)
    {
    }
    }
    }
    }
    class event_move extends Thread
    { public void run()
    {
    while(true)
    {
    try {
    sleep(2000);
    hjserver.txt2.setText("OK2");
    }
    catch(InterruptedException e)
    {
    }
    }
    }
    }