Link to home
Start Free TrialLog in
Avatar of pncorp
pncorp

asked on

Java handling large text file

I'm trying to access a large text file and handle the data as if it were a string.  I've never had to access a file larger than a few megs, and this is over 100mb.  Any help is appreciated.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Why do you need to access it as a String. Are you sure you do?
Avatar of pncorp
pncorp

ASKER

It doesn't have to be a String, but I need to be able to create strings by using indexes like I would use to create a substring from a string.  
OK. That wouldn't require you to read the whole file into memory fortunately. Just use a RandomAccessFile
e.g. to read a String of 64 characters halfway into the file:
RandomAccessFile raf = new RandomAccessFile("x.txt", "r");
raf.seek(raf.length() / 2);
byte[] buffer = new byte[64];
raf.read(buffer);
String s = new String(buffer);

Open in new window

Avatar of pncorp

ASKER

So how would I create a string from charcter 1000 - 1050?
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pncorp

ASKER

Thanks, you're awesome!
:-)
:-)

Actually, you should use readFully, not read