File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes How to insert text  into an existing(really big) file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to insert text  into an existing(really big) file" Watch "How to insert text  into an existing(really big) file" New topic
Author

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

Cindy Jones
Ranch Hand

Joined: Oct 08, 2002
Posts: 66
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

Joined: Sep 24, 2003
Posts: 1608
Use a buffer which is the same size (or larger depending on your required optimal solution) as your pattern to recognise.


Tony Morris
Java Q&A (FAQ, Trivia)
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

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

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
Cindy Jones
Ranch Hand

Joined: Oct 08, 2002
Posts: 66
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19759
    
  20

Wow, that reply really took a long time!


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 861
This woken zombie is about to turn 5. Happy birthday Zombie.


http://www.jamonapi.com/ - a fast, free open source performance tuning api.
JavaRanch Performance FAQ
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18977
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to insert text into an existing(really big) file