• Post Reply Bookmark Topic Watch Topic
  • New Topic

Any good marking mechanism when reading in items from a file?  RSS feed

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all:
I have a txt file full of job ids, i need to do some processing with each id. After each id is done, I need to cross it off because if my tool crashes for some reason, I do not want to restart the jobs that have already been processed..


Any good suggestions on how to do this?
 
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One simple mechanism is: open a PrintWriter + FileWriter to write to a file. Every time you complete a job, write a new line to the file, containing the job ID. Be sure to flush() the writer after every line. (PrintWriter has a constructor that accepts a boolean which makes this easy.) If the program crashes, read each line in the file to see what jobs are already done. Maybe put them in a HashSet so you can easily determine whether a job is done or not.

If the job IDs are sequential, you could just store a single job ID in a file, and keep overwriting it with the latest ID. This is risky though... if the prgram crashes while the write is occurring, you could get a blank file, or an incomplete line. It's safer to just keep adding new files at the end. You can put in extra logic so that, if the file size exceeds a certain maximum, you close() the writer and start a new file with a new name. E.g. completed_jobs-1.log, completed_jobs-2.log, completed_jobs-3.log, etc.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!