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()
.
精彩评论