We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › File - Random Access
Page Index Toggle Pages: 1
File - Random Access ? (Read 486 times)
File - Random Access ?
Mar 16th, 2008, 2:17am
 
Is there a way to go directly to the last element of a file ?

It's a huge text file of integer, one integer per line, more than 100 million lines.
Is there a fast way to find the last element of the file?
Right now I use a BufferedReader, and I scan the whole file line by line from start to end.
I have
BufferedReader br = new BufferedReader(new FileReader(f));
and I do
while (br.ready())
 br.readLine()
 ...

Is there a better way?
Re: File - Last element ?
Reply #1 - Mar 16th, 2008, 4:32pm
 
use RandomAccessFile from Java:
http://java.sun.com/j2se/1.4.2/docs/api/java/io/RandomAccessFile.html
Re: File - Random Access ?
Reply #2 - Mar 25th, 2008, 9:18am
 
Wow how long does that take?
Re: File - Random Access ?
Reply #3 - Mar 25th, 2008, 6:03pm
 
It takes almost 15 min to scan the file that way.

I'm now changing my code,  
from using a BufferedReader(FileReader()),
to using a RandomAccessFile().

That way I can get the #integers in the file without scanning the whole file,
----  ff = new File(fname);
----  raf = new RandomAccessFile(ff, "r");
----  count = (int)(raf.length()/4);

and I can quickly read the last element this way:
----  raf.seek(raf.length()-4);
----  n = raf.readInt()

It's not all done yet. I'm learning Java so everything I do takes longer than it should.
Page Index Toggle Pages: 1