简体   繁体   中英

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. 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. 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() . Use print() instead, and flush() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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