java ini_java读取ini格式的文件

/*** 去除ini文件中的注释,以";"或"#"开头,顺便去除UTF-8等文件的BOM头

*@paramsource

*@return

*/

private staticString removeIniComments(String source){

String result=source;if(result.contains(";")){

result= result.substring(0, result.indexOf(";"));

}if(result.contains("#")){

result= result.substring(0, result.indexOf("#"));

}returnresult.trim();

}public static MapreadIni(String filename){

Map> listResult=new HashMap<>();

Map result=newHashMap();

String globalSection= "global";

File file= newFile(filename);

BufferedReader reader=null;try{

reader=new BufferedReader(new InputStreamReader(newFileInputStream(file)));

String str= null;

String currentSection= globalSection; //处理缺省的section

List currentProperties = new ArrayList<>();boolean lineContinued = false;

String tempStr= null;//一次读入一行(非空),直到读入null为文件结束//先全部放到listResult中

while ((str = reader.readLine()) != null) {

str= removeIniComments(str).trim(); //去掉尾部的注释、去掉首尾空格

if("".equals(str)||str==null){continue;

}//如果前一行包括了连接符'\'

if(lineContinued == true){

str= tempStr +str;

}//处理行连接符'\'

if(str.endsWith("\\")){

lineContinued= true;

tempStr= str.substring(0,str.length()-1);continue;

}else{

lineContinued= false;

}//是否一个新section开始了

if(str.startsWith("[") && str.endsWith("]")){

String newSection= str.substring(1, str.length()-1).trim();//如果新section不是现在的section,则把当前section存进listResult中

if(!currentSection.equals(newSection)){

listResult.put(currentSection, currentProperties);

currentSection=newSection;//新section是否重复的section//如果是,则使用原来的list来存放properties//如果不是,则new一个List来存放properties

currentProperties=listResult.get(currentSection);if(currentProperties==null){

currentProperties= new ArrayList<>();

}

}

}else{

currentProperties.add(str);

}

}//把最后一个section存进listResult中

listResult.put(currentSection, currentProperties);

reader.close();

}catch(IOException e) {

e.printStackTrace();

}finally{if (reader != null) {try{

reader.close();

}catch(IOException e1) {

}

}

}//整理拆开name=value对,并存放到MAP中://从listResult中,看各个list中的元素是否包含等号“=”,如果包含,则拆开并放到Map中//整理后,把结果放进result中

for(String key : listResult.keySet()){

List tempList =listResult.get(key);//空section不放到结果里面

if(tempList==null||tempList.size()==0){continue;

}if(tempList.get(0).contains("=")){ //name=value对,存放在MAP里面

Map properties = new HashMap<>();for(String s : tempList){int delimiterPos = s.indexOf("=");//处理等号前后的空格

properties.put(s.substring(0,delimiterPos).trim(), s.substring(delimiterPos+1, s.length()).trim());

}

result.put(key, properties);

}else{ //只有value,则获取原来的list

result.put(key, listResult.get(key));

}

}returnresult;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值