String.replaceAll() is freezing the Server ? any other method to repl str?

Hi All,

I am having the entire file content as String which is of size 1Mb. I have having many files in a folder to repeat the following function.

String s = "getting entire content of file"

s = s.replaceAll("all","not all");

s = s.replaceAll("one","two");

s = s.replaceAll("three","four");

...........

I'm having many replaces. Each time a new String is created.

How can we do this using a Regular Expression ? I think / guess Regular Expression is also internally uses replaceAll method.

Can we do this by using a Java StringBuffer ? I'm not sure how to do this.

Can anybody help me on this ? Please

Thanks.

[684 byte] By [jkathira] at [2007-10-3 4:49:34]
# 1

hi :-)

i dont think this will make any difference but you can try this. I used this method prior to JDK1.4 because 1.3 doesnt contain the replaceAll() method. i just also got that from the net :-)

like what i said earlier, i dont think it will have an effect on what your doing

but you can give it a try then give some feedback.

public static String replace(String source, String pattern, String replace)

{

if (source!=null)

{

final int len = pattern.length();

StringBuffer sb = new StringBuffer();

int found = -1;

int start = 0;

while( (found = source.indexOf(pattern, start) ) != -1) {

sb.append(source.substring(start, found));

sb.append(replace);

start = found + len;

}

sb.append(source.substring(start));

return sb.toString();

}

else return "";

}

regards,

jie2eea at 2007-7-14 22:54:05 > top of Java-index,Java Essentials,Java Programming...
# 2

> Hi All,

>

> I am having the entire file content as String which

> is of size 1Mb. I have having many files in a folder

> to repeat the following function.

Whaattttt!!!!! A String of size 1 MB. What have you been smoking. You don't need another method what you need is a good jave book!!!!!!

What are you actually trying to do.

kilyasa at 2007-7-14 22:54:05 > top of Java-index,Java Essentials,Java Programming...
# 3

> Hi All,

>

> I am having the entire file content as String which

> is of size 1Mb. I have having many files in a folder

> to repeat the following function.

>

> String s = "getting entire content of file"

>

> s = s.replaceAll("all","not all");

> s = s.replaceAll("one","two");

> s = s.replaceAll("three","four");

> ...........

>

> I'm having many replaces. Each time a new String is

> created.

>

> How can we do this using a Regular Expression ? I

> think / guess Regular Expression is also internally

> uses replaceAll method.

>

> Can we do this by using a Java StringBuffer ? I'm

> not sure how to do this.

>

> Can anybody help me on this ? Please

>

> Thanks.

You shouldn't be reading the entire file in at once to begin with. Change the values as you read in the file before passing it on to it's destination, whether that be the console, another file, a database, etc.

kablaira at 2007-7-14 22:54:05 > top of Java-index,Java Essentials,Java Programming...
# 4

This looks to be a candidate for http://elliotth.blogspot.com/2004/07/java-implementation-of-rubys-gsub.html . This would allow you to just do one pass through the data. Your regular expression would then be "all|one|three" and your replacement could be implemented using a java.utl.Map.

For example

import e.util.*;

import java.util.*;

import java.util.regex.*;

public class Fred906

{

private static final Map<String, String> replacements = new HashMap<String, String>();

static

{

replacements.put("quick","QUICK");

replacements.put("over","OVER");

replacements.put("fox","CHICKEN");

}

public static void main(String[] args) throws Exception

{

String fileContent = "The quick \nbrown \nfox jumps over the lazy dog.";

Rewriter rewriter = new Rewriter("quick|over|fox")

{

public String replacement()

{

String newValue = replacements.get(group(0));

return Matcher.quoteReplacement((newValue == null) ? "ERROR" : newValue);

}

};

System.out.println(rewriter.rewrite(fileContent));

}

}

If this proves to be too slow then, depending on the form of patterns, one could use a KMP approach.

I am concerned that this sounds like a big macro expaansion and you might do better to use a JSP.

Message was edited by:

sabre150

Message was edited by:

sabre150

sabre150a at 2007-7-14 22:54:05 > top of Java-index,Java Essentials,Java Programming...