?
Solved

Java handling large text file

Posted on 2010-04-08
9
Medium Priority
?
307 Views
Last Modified: 2013-12-14
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.
0
Comment
Question by:pncorp
  • 6
  • 3
9 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 30137922
Why do you need to access it as a String. Are you sure you do?
0
 
LVL 1

Author Comment

by:pncorp
ID: 30138118
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.  
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 30138226
OK. That wouldn't require you to read the whole file into memory fortunately. Just use a RandomAccessFile
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 86

Expert Comment

by:CEHJ
ID: 30138612
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

0
 
LVL 1

Author Comment

by:pncorp
ID: 30139608
So how would I create a string from charcter 1000 - 1050?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 30139810
Seek to 1000 and create a buffer of size 50
0
 
LVL 1

Author Closing Comment

by:pncorp
ID: 31712478
Thanks, you're awesome!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 30142641
:-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 30143067
:-)

Actually, you should use readFully, not read
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

598 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question