问题描述
- 按节点取出.properties中的数据问题 有点小挑战
-
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"));
}
}
解决方案二:
按行读取文件,然后自己解析吧
解决方案三:
ResourceBundle resourceBundle = ResourceBundle.getBundle(fileName);
return resourceBundle.getString(key);
- @param name src下properties文件路径与名称
- @param key 要取值的健
- @return 返回属性文件的值
时间: 2024-10-21 09:16:11