Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Java files  RSS feed

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have an error file that keeps updating itself.By updating what i mean is whenever there is an error the error is written into the file.I need to read the file and display the output.If it is a static file than it is easy.The data inside the file is dynamic.I need to display the latest contents of the file.Eg - First I read the 5 lines of the file then the error file is updated then i have to read the updated contents and display.
Does anyone have any idea about this.

Thanks

Mouli
 
Rancher
Posts: 42974
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the old contents are replaced when a new error is added, you need to keep track of the last time you displayed it. Next time you check, only if its lastModifiedTime is larger than the value remembered, you display it again.

If the error messages are added at the end of the file, you need to keep track of how many lines of the files you have displyed so far. Next time you check, see if there are now more lines. If so, you only display the ones from the line you remembered to the last one.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can check the file size now & then and get the part of the file added since last time with a RandomAccessFile starting one byte after the old size. One of the sherrifs had a neater solution to this a while back but I can't recall it right now. Maybe he'll come through for us again.
 
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
implementing a FileChangeListener is one option
http://www.javaworld.com/javaworld/javatips/jw-javatip125.html

Another option
http://forum.java.sun.com/thread.jspa?threadID=306361&messageID=1241692

Alternatively lastModified() method in File can be used to identify whether a file is changed.
[ November 23, 2005: Message edited by: jiju ka ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!