• Post Reply Bookmark Topic Watch Topic
  • New Topic

keyed input file

 
AC Miller
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working on a program in which certain objects have a variable that changes with each time step. I'd like to use an input text file that uses the tick number (or some other index) as a key for the value of that variable for that specific object at that time step. The file will not change at all during the program run and won't need to be written to -- just read from. RandomAccessFiles and HashMaps look like they could be useful, but I'm not sure if this is the best way. I'm fairly new to Java and, as a low-intermediate-level programmer, am not familiar with all of the tools Java has available. But -- with some suggestions from those who ARE familiar with what may be useful -- I'm willing to dive in once I know where to start looking.

Thanks in advance!
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use FileInputStream to read a file. Maybe you should start by reading The IO Java Tutorial.

In a few minutes you will be introduced to the IO API and soon you will be able to read that file you say.
[ May 27, 2006: Message edited by: Edwin Dalorzo ]
 
AC Miller
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the response.

I have looked at the I/O Tutorial, which is how RandomAccessFile came up. I guess my complication with FileInputStream is that I need to pick out a particular line in the text file. And the complication with RandomAccessFile is that I want to select that line not based on what line it is, but what tick value is associated with the variable on that line. I'm not sure how FileInputStream would let me say "Get the variable value for tick #426," instead it seems to be much more sequential in dealing with reading subsequent bytes.
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, it seems that RandomAccessFile is the way to go in this case that you describe. However, RandomAccessFile seeks a position based on an offset, and not on the particular contents of the data file.

In this case, there is no other way to to this, but to search the whole file for the record.

If the data file is too big, and if the records are not ordered, it could be a good idea to create an index file, where all the keys are stored, and you must also save the pointers to the place where these keys can be found in the data file.

As you say the file is read-only, there is not a cost in maintaining the index file updated and you will have to generate the index file only once: the fisrt time it is used.

That way, you search for the keys in the index file, which should be a smaller file, and then, when found, search the specific position of your record in the data file according to the pointer associated with the key.

On the other hand, if the data file is a read-only file, and if the records are all of fixed size, and if they are ordered, you could calculate the position of a particular key, based on the constant length of the records and the relative position of the key you are looking for, as long as there are not key holes in the data file.

Do any of these ideas sound useful?
[ May 27, 2006: Message edited by: Edwin Dalorzo ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!