Forums Register Login

Using RandomAccessFile on text file

+Pie Number of slices to send: Send
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?!
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
How tragic....
You sorted it out just before I posted ...sigh...

Sridhar
+Pie Number of slices to send: Send
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
rubbery bacon. rubbery tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1371 times.
Similar Threads
Can I change the current position in Stream??
Reading the first 10 bytes of a file
seek() vs skipBytes()
Using RandomAccessFile on huge file
connection to db file
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 11:16:54.