“并不是每个人都需要$(qunar)自己的粮食,$(flight.1)每个人都需要做自己穿的$(flight.2),我们说着别人发明的$(hotel),使用别人发明的数学......我们一直在$(tuan)别人的成果。使用人类的已有经验和知识$(travel.1)来进行,是一件$(travel.2)的事情”
word.conf
flight=也不是:衣服
qunar=种植
hotel=语言
tuan=使用
travel=发明创造:很了不起
解决方案:
package qunaer;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class Test2 {
public String readContextTxt(){
FileReader fr=null;
BufferedReader br=null;
StringBuilder sb = new StringBuilder();
String returnValue=null;
try {
fr = new FileReader("context.txt");
br = new BufferedReader(fr);
String line;
while((line=br.readLine())!=null){
sb.append(line);
}
returnValue=sb.toString();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
} finally{
try {
if(br != null){
br.close();
}
if(fr != null){
fr.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(returnValue);
return returnValue;
}
public Map<String,String> readWordConf(){
FileReader fr = null;
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
Map map = new HashMap();
try{
fr = new FileReader("word.conf");
br = new BufferedReader(fr);
String line;
while((line = br.readLine()) != null){
sb.append(line);
System.out.println(line);
String [] str = line.split("\\=");
map.put(str[0], str[1]);
}
}catch(Exception e){
e.printStackTrace();
}
return map;
}
public Map<String,String[]> getWordConf(Map<String,String> map){
Map<String,String[]> returnValue = new HashMap<String,String[]>();
for(Map.Entry<String, String> entry:map.entrySet()){
String key = entry.getKey();
String value = entry.getValue();
System.out.println("vvvvalue:"+value);
String [] str = value.split("\\:");
System.out.println("llllength:"+str.length);
returnValue.put(key, str);
}
System.out.println(returnValue);
return returnValue;
}
public String replaceValue(Map<String,String[]> map){
String txt = this.readContextTxt();
while(txt.indexOf("$(")!=-1){
int start = txt.indexOf("$(");
int end = txt.indexOf(")");
String sub = txt.substring(start+2, end);
String []arr = sub.split("\\.");
System.out.println("sub:"+sub+arr[0]);
String replace = null;
for(Map.Entry<String, String[]> entry:map.entrySet()){
String key = entry.getKey();
String []value = entry.getValue();
System.out.println("key:"+key+" value.length:"+value.length);
if(key.equals(arr[0])){
if(arr.length>1){//表示含有小数点
replace = value[Integer.parseInt(arr[1])-1];
}else{
replace = value[0];
}
System.out.println("sub:"+sub+" replace:"+replace);
txt = txt.replaceAll(sub, replace);
txt = txt.replaceFirst("\\$\\(", "");
txt = txt.replaceFirst("\\)", "");
break;
}
}
}
System.out.println(txt);
return txt;
}
public static void main(String []args){
Test2 t2 = new Test2();
Map map = t2.readWordConf();
Map<String,String[]> arrMap = t2.getWordConf(map);
String val = t2.replaceValue(arrMap);
}
}