I have a program running which writes log to a file and I want to read it line by line. I tried using InputStream
, particularly DataInputStream
, using its available method. But then it doesn't have readLine
method, it is deprecated and it was suggested to wrap it in BufferedReader
to use readLine
. But when I use BufferedReader
it doesn't read all the lines, it somehow stop by reading one line.
public void read(DataInputStream ins) {
try {
while(true) {
if(ins.available() > 0) {
//BufferedReader reader = new BufferedReader(new InputStreamReader(ins));
//System.out.println(reader.readLine());
System.out.println(ins.readLine());
}
else {
Thread.sleep(200);
}
}
}
catch(Exception e) {
e.printStackTrace();
}
}
So, the code works with ins.readLine()
, but it is deprecated and according to java documentation it said using BufferedReade
r. But with reader.readLine()
it is not working as I expect.
Also, what other better java way to do this, as I am sure this is some standard problem. I read somewhere about Trailer class. But not able to test that.