pro.properties文件配置如下
[Default]
team = happy team
[Project]
Dev = Harry
From = %team%
age = 20
Des = %Dev%, from %From%, %age% yeas old
用什麼辦法可以按節點將其中的數據取出?
范例:
getString(Project,Des)意思取出節點Project下的Des 結果為:Harry,from Happy Team ,20 years old
getInteger(Project,age)意思是取出節點Project下的age 結果為:20
求老師指點一下
代碼如下,先解析文件再保存:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class FileParser {
private HashMap<String,HashMap<String, String>> dataHashMap;
private String filePath;
private String patternString="%[A-Za-z]+%";
private Pattern pattern=Pattern.compile(patternString);
public FileParser(String filePath){
this.filePath=filePath;
dataHashMap=new HashMap<String,HashMap<String,String>>();
}
public String getString(String nodeKey,String valueKey){
Set<String> nodeKeys=dataHashMap.keySet();
for(String key:nodeKeys){
if(key.equals(nodeKey)){
HashMap<String,String> map=dataHashMap.get(key);
return map.get(valueKey);
}
}
return null;
}
public int getInteger(String nodeKey,String valueKey){
Set<String> nodeKeys=dataHashMap.keySet();
for(String key:nodeKeys){
if(key.equals(nodeKey)){
HashMap<String,String> map=dataHashMap.get(key);
return Integer.parseInt(map.get(valueKey));
}
}
return -1;
}
public void parse(){
try {
boolean isNewNode=false;
FileReader reader=new FileReader(filePath);
BufferedReader buf=new BufferedReader(reader);
String str=null;
String nodeKey=null;
while((str=buf.readLine())!=null){
if(str.startsWith("[")){ //新的節點
isNewNode=true;
nodeKey=str.substring(str.indexOf("[")+1,str.indexOf("]"));
HashMap<String,String> map=new HashMap<String,String>();
dataHashMap.put(nodeKey, map);
}
if(!isNewNode){
HashMap<String,String> detail=dataHashMap.get(nodeKey);
String detailKey=str.substring(0,str.indexOf("=")).trim();
String detailValue=str.substring(str.indexOf("=")+1).trim();
String newDetailValue=null;
if(detailValue.contains("%")){ //因為可能包含%
Matcher matcher=pattern.matcher(detailValue);
while(matcher.find()){
String matchString=matcher.group();
String keyString=matchString.replace("%","");
String valueString=null;
Set<String> keysSet=dataHashMap.keySet();
for(String key:keysSet){
HashMap<String,String> nodeValueMap=dataHashMap.get(key);
valueString=nodeValueMap.get(keyString);
if(valueString!=null)
break;
}
newDetailValue=detailValue.replaceFirst(matchString, valueString);
detailValue=newDetailValue;
matcher=pattern.matcher(detailValue);
}
}
detail.put(detailKey, detailValue);
}
isNewNode=false;
}
buf.close();
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("保存的數據為:"+dataHashMap);
}
/**
* @param args
*/
public static void main(String[] args) {
FileParser fileParser=new FileParser("D:\\pro.properties");
fileParser.parse();
System.out.println(fileParser.getString("Project","Des"));
System.out.println(fileParser.getInteger("Project", "age"));
}
}
結果如下: