Mazzy
Mazzy

Reputation: 14189

reading a file from last line

I would like to read a file from last line using RandomAccessFile. Is this possible or do I have to use another class?

Beside this file changes during the time so the last line doesn't remain last forever. During the reading another, java program write on it. My question is: the program will see in the same time another java program write on the file, the changes?

Edit

Well suppose I have a server that write its faults in a error log file during it's running.another program reads every line.which should be the best way?

Upvotes: 1

Views: 1101

Answers (1)

David Kroukamp
David Kroukamp

Reputation: 36423

Yes reading a file from the bottom up is possible using RandomAccessFile:

as for the other part of your question:

Beside this file changes during the time so the last line doesn't remain last forever.During the reading another java program write on it.My question is: the program will see in the same time another java program write on the file, the changes?

I would propose a SSCCE in which you show what you are trying to accomplish and the problem

EDIT:

As Jon Skeets comment suggests, I found a link to a similar question answered by him: Quickly read the last line of a text file?

EDIT 2:

I think I got your second question, I'm not sure it's possible, as a single file cant be accessed by 2 different streams at the same time, one will just throw an error when trying to open the file. Ypu can however monitor if changes occur after the file has been read using Java.NIO Directory Watcher, Unless I misunderstood you.

Upvotes: 1

Related Questions