3

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 BufferedReader. 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.

mmBs
  • 8,421
  • 6
  • 38
  • 46
r0b0
  • 49
  • 2
  • 6
  • What is not working as expected ? – AllTooSir Jun 20 '13 at 16:01
  • When i use reader.readLine(), it only read the first line – r0b0 Jun 20 '13 at 16:06
  • don't use the `ävailable()` method, it is useless. the InputStreams are not going to read a continuously updating file. once they reach the end of stream, they will stop returning results, even if the underlying file updates. – jtahlborn Jun 20 '13 at 16:07
  • It keeps reading the first line, or it just reads it once? – Bernhard Barker Jun 20 '13 at 16:07
  • You need to work with a FileChannel if you want to handle continuously updating data. – jtahlborn Jun 20 '13 at 16:10
  • @Dukeling it only read once – r0b0 Jun 20 '13 at 16:14
  • @jtahlborn Thanks, I read RandomAccessFile and I think it can be used, but it didn't appear as a clean solution to me, as working with current positions. I didn't understand the FileChannel, how it is used, it says something about locking. But again I am not very comfortable with manually putting lock on part of file, as I have recently started java and I suspect if I work with file pointers or locks I amy go wrong, so wanted to know if some standard lava library is there for such a standard task. – r0b0 Jun 20 '13 at 16:26
  • reading a file while another writes to it is not a standard task. – AlexWien Jun 20 '13 at 16:29
  • org.apache.commons.io.input.Tailer : http://stackoverflow.com/questions/557844/java-io-implementation-of-unix-linux-tail-f?rq=1 – kervin Apr 03 '15 at 15:28

1 Answers1

0

From How do you continuously read a file in Java?

The trick is to use a java.io.RandomAccessFile, and periodically check if the file length is greater that your current file position. If it is, then you read the data. When you hit the length, you wait. wash, rinse, repeat.

You will need to deal with mutual exclusion though (only one thing can read/write at a time or your data could be corrupted)

If you end up using a BufferedReader, it reads in one line at a time. If you want to read in more than one, you can use a loop like so:

BufferedReader br = new BufferedReader(new FileReader("file.txt"));
while ((sCurrentLine = br.readLine()) != null) 
{
    System.out.println(sCurrentLine);
}

This can cause an IOException fyi so wrap it in a try/catch or have your method throw one and deal with it else where.

Source: http://www.mkyong.com/java/how-to-read-file-from-java-bufferedreader-example/

Community
  • 1
  • 1
Daniel
  • 2,435
  • 5
  • 26
  • 40
  • this isn't goint to solve the OP's problem. Standard InputStreams/Readers can't be used to read files which are continuously being updated. – jtahlborn Jun 20 '13 at 16:11
  • @jtahlborn mmm, ok lemme see if I can give a better answer. it does address the documentation point of using BufferedReader though so I'll leave it – Daniel Jun 20 '13 at 16:12
  • I wonder what happens if reader doesn't have any lines to read currently, but it doesn't mean file is closed. – r0b0 Jun 20 '13 at 16:13
  • @jtahlborn ok, that should do it – Daniel Jun 20 '13 at 17:33
  • 1
    k, so now your answer is just a repeat of someone else's. – jtahlborn Jun 20 '13 at 18:23
  • @jtahlborn to get philosophical, aren't almost all of the answers on this site a repeat of someone else's? It's a valid answer that addresses the problem as well as another possible solution. The real issue is: if I can quote another answer as a valid answer, how is this question not a duplicate? – Daniel Jun 20 '13 at 18:29
  • if it's a repeat of another answer on SO, then the question should be closed as a duplicate. – jtahlborn Jun 20 '13 at 20:02
  • @jtahlborn it seems there's a logical course of action for you then ;) – Daniel Jun 20 '13 at 20:07