Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading and Writing Large Files  RSS feed

 
Ramesh Pappala
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Iam trying to read a file of size 50MB by using a RandomAccessFile and iam trying to place that whole data in a String object. But iam getting an exception.


Program:

RandomAccessFile raf=new RandomAccessFile("one.txt","r");
//size of one.txt is 50MB
int len=(int)raf.length();
byte[] fileInData=new byte[len];
raf.read(fileInData);
String fileData=new String(fileInData);

Output:

"Exception in thread "main" java.lang.OutOfMemoryError"



Is there any option to change the memory allocated to JVM by the underlying OS, or else what i have to do.

Please give me reply.

Bye.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try java -X on the command line. That will display a list of special options for things like maximum heap size.

I'd look for an algorithm that didn't require the whole thing in memory, if possible. What are you doing with all those bytes once you have them?
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!