使用printwriter时删除新行

时间:2011-08-28 05:08:09

标签: java

我正在尝试打印出日志文件末尾附加的任何更改,类似于尾部日志。但是当用printwriter打印出来时,它也会打印出一条新线。 而不是打印

test1 
test2

打印:

test1

test2

代码如下。我尝试使用pwriter.print(line)而不是println,但没有打印任何内容。有没有办法取消回车。

public class Lognow implements Runnable{
boolean execute = true;

InputStreamReader inputStreamReader;
BufferedReader bufferedReader;
PrintWriter pwriter;

public Lognow(){
PrintWriter pw = new PrintWriter(System.out, true);
try {

     inputStreamReader = new InputStreamReader(new FileInputStream ("D:/app/logi.txt"));
     bufferedReader = new BufferedReader (inputStreamReader);
     String line = bufferedReader.readLine();
     while(line!=null){          
         line = bufferedReader.readLine();
     }

     pwriter = pw;

} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
}

public void run() {
    while( execute ) {
        try {
            String str="";
            String line = bufferedReader.readLine();

            if(line!=null) {

                pwriter.println(line);

            }
            else {
                try {
                    Thread.sleep( 500 );
                }
                catch( InterruptedException ex )
                {
                    execute = false;
                }
            }


        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

public static void main(String[] args){
    Lognow lognow = new Lognow();
    lognow.run();
}

1 个答案:

答案 0 :(得分:4)

请勿使用println()。请改用print()flush()