Java search replace
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开发者_如何学Python 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.
Let me give you some general development advice that helps when a problem is overwhelming.
First of all, write down a list--an algorithm that you think will solve the problem. In your case try:
- for each file in treemap 1
- for each entry in that file
- for each line in the current file
- search for each term in the second treemap
- for each found term, replace with the definition.
Now, go into each one of those steps and enhance it. For the first one you should
- Iterate over the list of files in treemap 1
- open the next file
- start reading the contents
Do this for each step that you can't currently envision "Just Coding" until you can see just how you would do each step.
Once you are done with this, I suggest writing a Very Basic application that does one thing from your list--for instance, open a file. It's very helpful here to write a junit test or write a test in your main. Make sure that it opens the file and can read from it.
Once you are at this point, keep adding to your code--just add a little bit then recompile/retest. Retest every single line until you have coded for like 5 years, then go to every 2 or 3 lines.
Keep updating your tests to test your code. If you break an old test rewrite it, don't throw it away.
Iterate.
I hope this helps.
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
}
}
The basics could be:
- loop over the entrySet
- for each entry get the value
- Split that value into words
- for each word lookup that word in the dictionary
- if found add that word to your translated stringbuilder
- otherwise add the original
- add space or so
- setvalue on the map entry with stringbuilder.toString
I would not use a TreeMap to store the contents of all files+.
You need to read the contents anyway, so there is no advantage in first storing all of them+.
Read one file a time, process it and save it.
Have a look at the NIO package of Java. On the NIO examples page there is an example for searching in a list of files (Grep.java).
+ if not needed {elsewhere}
精彩评论