aspose file tools*
The moose likes Java in General and the fly likes Replace pattern  in huge 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 "Replace pattern  in huge file" Watch "Replace pattern  in huge file" New topic
Author

Replace pattern in huge file

Abhi Venu
Ranch Hand

Joined: Jul 09, 2009
Posts: 73
Hi All,


I need to read a large text file (90 MB) file da replace a pattern let be 'xyz' with 'PQR' .I need to create a new file with this change.

How can this be done in most efficent way in minimum time.

I did it in a way but it took 25 mnts Also i tried with BufferedInputStream and stringbuffer.But igot out of memory error.I dont want to increas the heap size and try it.

Please help me in the approach i should choose

Regards
abhi


A table, a chair, a bowl of fruit and a violin; what else does a man need to be happy?:Einstein
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

Please KeepItDown, not only in your post text but also in your subjects.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
H Paul
Ranch Hand

Joined: Jul 26, 2011
Posts: 455
    
    4
Am I beginner. And the only concept that stick to my head is 'Divide and Conquer'.

Technically, Is it feasible and/or advisable that we divide the big file into N small files and let each be taken by a thread.
When all threads are done. Merge the files...

This is like dividing the job into N persons rather than letting 1 person do it alone.






Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30512
    
150

Do you have to use Java? It might be more efficient at a UNIX command line.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
H Paul
Ranch Hand

Joined: Jul 26, 2011
Posts: 455
    
    4
The question now is why not?
Jon Avadis
Ranch Hand

Joined: Jul 20, 2011
Posts: 49

Maybee you can post your code. I did similar things with 30MB files in a matter of seconds, processing them line by line,
and i cant see why it would take this long for a 90MB file.


Knowledge Reigns Supreme
Abhi Venu
Ranch Hand

Joined: Jul 09, 2009
Posts: 73
Hi
Jon Can you post your code please. Also i would like to know how it is done in UNIX and if any windows eqiuvalent for the same.


Regards
Abhi
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

Why don't you post your code first? If Jon posts his code, you won't learn anything. As for how UNIX does it, there's a command line tool called sed that can help with this.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11308
    
  16

you could also write a one-line perl script to do it.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Replace pattern in huge file