• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to insert text into an existing(really big) file

 
Cindy Jones
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a really big file. I want to search for a particular pattern within this file and replace that with another text. I tried the following:
1. Used StringBuffer to load file contents to do a replace. Causes out of memory error
2. Finding the filePointer for the pattern in a random access file and doing raf.writeChars . Overwrite exixting contents instead of inserting
3. Taking the portion before the pattern , the pattern and the portion after the pattern and putting in a separate file also wont work cos of the size issue.
Any suggestions??
Thanks!
Dhanya
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a buffer which is the same size (or larger depending on your required optimal solution) as your pattern to recognise.
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try increasing the amount of memory that's allocated to the JVM by using the JVM parameter -Xmx

(type java -X to get the help details for the -X** commands)

Realize that for several of those methods (such as StringBuffer) you're trying to read in the entire file and store it in a StringBuffer, and then write it back out, so the amount of memory the JVM has needs to be large enough that it can hold the entire file, so maybe try something like:

java -Xmx512m com.blah.myProgram

that will allocate 512 MB to the JVM.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If memory constraints are an issue, you may want to read in a line (or several lines) at a time. In pseudocode, I would do something like this:



HTH

Layne
 
Cindy Jones
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Rob Spoor
Sheriff
Pie
Posts: 20613
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, that reply really took a long time!
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This woken zombie is about to turn 5. Happy birthday Zombie.
 
Henry Wong
author
Marshal
Pie
Posts: 21446
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
steve souza wrote:This woken zombie is about to turn 5. Happy birthday Zombie.


Interestingly, this zombie was awoken by the original poster. I am assuming it was done for a reason.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic