I want to do search and replace with the help of java.
I have TreeMap that contains file names and its contents. I have one another TreeMap that contains word and its meaning.
I want to search file contents and replace found word with its meaning.
Thanks
P.S: It is not an assignment. I used simple reading file and replace function. However, I need speedy way to do it. Any sample code or ref. will be appreciated.
This example will work for all classes implementing the map interface, not just a TreeMap:
public void searchAndReplace(Map<File, byte[]> fileToContentMap, Map<String, String> wordToDefinitionMap) {
// for each file
for (File f : fileToContentMap.keySet()) {
// get the file's contents
String contents = new String(fileToContentMap.get(f));
// replace the contents with definitions
for (String word : wordToDefinitionMap.keySet()) {
contents = contents.replaceAll(word, wordToDefinitionMap.get(word));
}
// make sure to update the contents in the array
fileToContentMap.put(f, contents.getBytes());
// note, remember to update the physical file as well
}
}