2

I'm trying to replace several strings in one method using a HashMap but currently I can just get it to work with the first string in Map.

Previously I was using a "replaceAll" method for each String, which is hard to maintain because I'm trying to replace a changing list of Strings.

Can you give me any ideas?

Here's my code:

private static void string_change(String args[])
 {
 try
 {
 File file = new File("input_file.txt");
 BufferedReader reader = new BufferedReader(new FileReader(file));
 String line = "", oldtext = "";
 while((line = reader.readLine()) != null)
     {
     oldtext += line + "\r\n";
 }
 reader.close();

 Map<String, String> ReplacementMap = new HashMap<String, String>();
 ReplacementMap.put("STRING1", "TEXT1");
 ReplacementMap.put("STRING2", "TEXT2");
 ReplacementMap.put("STRING3", "TEXT3");

 String originalString = oldtext;

 for (Map.Entry<String, String> entry : ReplacementMap.entrySet()) {
   StringBuilder builder =  new StringBuilder(originalString.replaceAll(entry.getKey(), entry.getValue()));

   String newtext = builder.toString();

 FileWriter writer = new FileWriter("output_file.txt");
 writer.write(newtext);
 writer.close();
 }

 }
 catch (IOException ioe)
 {
 ioe.printStackTrace();
 }
} 
1
  • So it only replaces "STRING1" with "TEXT1", and not "STRING2" and "STRING3"? Commented Sep 24, 2013 at 22:01

2 Answers 2

4

The problem is, that you try to write the file for every entry in the map. So only the last entry is beeing applied to the target file. The better solution would be to apply all replacements before you write the file.

File file = new File("input_file.txt");
BufferedReader reader = new BufferedReader(new FileReader(file));
StringBuffer buffer = new StringBuffer();
String line;
while((line = reader.readLine()) != null) {
    buffer.append(line);
    buffer.append("\r\n");
}
reader.close();

Map<String, String> replacementMap = new HashMap<String, String>();
replacementMap.put("STRING1", "TEXT1");
replacementMap.put("STRING2", "TEXT2");
replacementMap.put("STRING3", "TEXT3");

String toWrite = buffer.toString();
for (Map.Entry<String, String> entry : replacementMap.entrySet()) {
    toWrite = toWrite.replaceAll(entry.getKey(), entry.getValue()));
}

FileWriter writer = new FileWriter("output_file.txt");
writer.write(toWrite);
writer.close();
Sign up to request clarification or add additional context in comments.

2 Comments

Hey Thanks! It works perfectly! Do you think this code could perform better on large files than the string by string approach I was using before?
Yes, that increases performance. When using a loop the compiler can't automatically replace the string concatenation with the StringBuilder. StringBuilder performs better when appending large amounts of strings before you really need the value itself.
0

You're overwriting your output file each time through the loop. How about this?

String temp = originalString;
for (Map.Entry<String, String> entry : ReplacementMap.entrySet()) {
    temp = temp.replaceAll(entry.getKey(), entry.getValue()));
}
FileWriter writer = new FileWriter("output_file.txt");
writer.append(temp);
writer.close();

1 Comment

Thanks for your help! I've tried with your code but for some reason the output_file is blank. I'll try to tweak it a little bit to see if I can get the desired output.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.