• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow, that reply really took a long time!
 
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This woken zombie is about to turn 5. Happy birthday Zombie.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic