简体   繁体   English

使用printwriter时删除新行

[英]Remove new line when using printwriter

I am trying to print out any changes that is appended at the end of a log file, similar to tail log. 我试图打印出日志文件末尾附加的任何更改,类似于尾部日志。 But when printing it out with printwriter it will also print out a new line. 但是当用printwriter打印出来时,它也会打印出一条新线。 Instead of printing 而不是打印

test1 
test2

it prints: 它打印:

test1

test2

Code is as below. 代码如下。 I tried with pwriter.print(line) instead of println but nothing is printed. 我尝试使用pwriter.print(line)而不是println,但没有打印任何内容。 Is there any way to remove the carriage return. 有没有办法删除回车。

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();
}

Don't use println() . 不要使用println() Use print() instead, and flush() . 改为使用print()flush()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM