• Post Reply Bookmark Topic Watch Topic
  • New Topic

Random IO  RSS feed

 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a thread that came from a question in one of the general Java forums about replacing a string in a file. I suggested one solution might be to update the file in place and to test that out I made a little class that reads and writes and updates a text file with fixed length records.
As a teaser, here's the JUnit tests that helped me build it. These tests are in the order they were written and implemented. They don't show the history of refactoring of method granularity and names that went on through the process. I'd like to refactor this as well - get rid of all the quoted filename literals - but no time now.

For grins, think about what it would take to make this thing truly useful. A way to remove records. Hmmm, can't shift the file contents down, so we'd have to save empty slots. A way to track empty slots for future inserts. A way to track the order records were added, not just the order they're in now. An indexing scheme so we could find records by key. Maybe a way to store records physically in key order. Pretty soon you've invented VSAM!
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For that matter, you're probably halfway through the SCJD assignment at this point.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was reading about ternary search trees today and wondering if I could stick multiple indices on one of these files. Dangerous road, could really chew up a lot of time! Anyhow, here's my simple implementation to run those tests:
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!