1

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?

Mazzy
  • 13,354
  • 43
  • 126
  • 207

1 Answers1

1

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.

Community
  • 1
  • 1
David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
  • The code you've linked to is bad - it assumes an encoding of ISO-8859-1, effectively. Reading files in more general encodings is possible, but much harder. – Jon Skeet Jul 08 '12 at 17:32
  • @JonSkeet I see why you say it might not be good I searched and found a similar question which you answered and added that to the post so the OP may see – David Kroukamp Jul 08 '12 at 17:41
  • 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? – Mazzy Jul 08 '12 at 17:46
  • @Mazzy see my edited post, why not watch the directory in which the log is for any changes and when a change occurs. make a temp copy of the log and read it in – David Kroukamp Jul 08 '12 at 17:50
  • So you're saying to make a copy of the file and read the copy right? – Mazzy Jul 08 '12 at 17:57
  • yes, as I dont see how if the error logger has not closed its lock on the file that it will allow you access to the file. – David Kroukamp Jul 08 '12 at 17:58
  • @DavidKroukamp but this create a problem: I have to monitor in pooling the file.if I monitor every 5 seconds the file this mean I have to create every 5 seconds a copy of errlog but this create a lots of copies of log file – Mazzy Jul 08 '12 at 18:05
  • The file watcher will only activate when a change in the directory occurs, i.e a file is created, deleted, or changed and if its the log file then you'll make a copy, read it in and on the next file change overwrite last copy of file – David Kroukamp Jul 08 '12 at 18:10