Hi, I'm looking at using RandomAccessFile where I have a large (1.5GB) text file containing a regular grid of data. At the top of the file it tells you where the top left corner of the grid is (geographical X Y coordinates) and also the distance in metres between the regularly spaced data points (this is geographical data). There than follows a large grid of data values, a simple example looks something like this: top_left_coord: 123, 543 spacing_dist_m: 50 -134.6 345.9 -12.78 1231.0 768.54 34.9 -12.05 66.72 236.57 366.0 -99.9 -264.08 -72.77 -547.5 8.9 -7.2 14.02 -22.0 -145.6 88.92 which represents a grid like: . . . . . . . . . . . . . . . . . . . . The problem is I need to dip into this file and select a (probably square) portion of it from any location. The difficulty is knowing where to start reading using RAF. I don't know much about bytes etc as I am self-taught programmer working mainly on web stuff, but I'm guessing each number in the grid will be a different number of bytes long because of how they vary in the file and because the file's text. Any ideas? Perhaps I need to convert these values to real numbers and create a binary version of the file...or perhaps I could index it in some way, but I don't know where to start and hoping you can give me some pointers please?!
Just answering my own post! I decided that any amount of indexing of such a text file would be unlikely to work in this context. I have now followed the option of converting the file into a binary format. This means I can now calculate the exact location of a particular entry in the file because all entries are 8 bytes long (double). Sorted
It might be better to store the data you have as floats instead of text. The RandomAccessFile class has a writeFloat(float) method which writes floats to a file using 4 bytes per float. You could use the Float.parseFloat(String ) method to convert a string into a float. To access a number in the middle of the file you have to use the RandomAccessFile.seek(long) method to move the file pointer to the required location. If all the data in your file was written as floats and you wanted to access the nth float you would just have to do
Just remember to take care of the exceptions readFloat() throws. Go through the RandomAccesFile javadocs before you start off.. Hope this helps Sridhar
Your efforts are much appreciated all the same Sridhar - at least you have validated the way I'm doing it now so I'm confident it's a valid approach. Have chosen doubles to be on the safe side so far, but suspect given the range of data values I can expect a float would suffice so it should make things a bit smaller/faster by using float. cheers
SCJP 1.4, www.gsi3d.org.uk
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop