java操作ini文件

取ini的配置的格式如下:

1
2
3
4
5
6
7
[section1]
key1=value1
 
[section2]
key2=value2
 
。。。。

其中可能一个Key对应多个value的情况。


根据网上其他代码进行修改。


读取文件:

[java]  view plain  copy
  1. import java.io.BufferedReader;  
  2. import java.io.File;  
  3. import java.io.FileReader;  
  4. import java.io.IOException;  
  5. import java.util.LinkedHashMap;  
  6. import java.util.Map;  
  7. import java.util.Set;  
  8.   
  9. public class IniReader {  
  10.   
  11.     protected Map<String, Map<String, String>> sections = new LinkedHashMap<String, Map<String, String>>();  
  12.     private transient String currentSecion;  
  13.     private transient Map<String, String> current;  
  14.       
  15.     private String[] notes = new String[]{";""#""//"};  
  16.   
  17.     public IniReader(String filename) throws IOException {  
  18.         BufferedReader reader = null;  
  19.         try {  
  20.             File file = new File(filename);  
  21.             if(file.exists() && file.isFile()){  
  22.                 reader = new BufferedReader(new FileReader(file));  
  23.                 read(reader);  
  24.                 reader.close();  
  25.             }  
  26.         }finally{  
  27.             if(reader != null){  
  28.                 reader.close();  
  29.             }  
  30.         }  
  31.     }  
  32.   
  33.     protected void read(BufferedReader reader) throws IOException {  
  34.         String line;  
  35.         while ((line = reader.readLine()) != null) {  
  36.             parseLine(line);  
  37.         }  
  38.     }  
  39.   
  40.     protected void parseLine(String line) {  
  41.         line = line.trim();  
  42.         if (line.matches("\\[.*\\]")) {  
  43.             currentSecion = line.replaceFirst("\\[(.*)\\]""$1");  
  44.             current = new LinkedHashMap<String, String>();  
  45.             sections.put(currentSecion.trim(), current);  
  46.         } else if (line.matches(".*=.*") && !line.startsWith("#")) {  
  47.             if (current != null) {  
  48.                 //去掉注释  
  49.                 for(String str : notes){  
  50.                     int num = line.indexOf(str);  
  51.                     if(num != -1){  
  52.                         line = line.substring(0, num);  
  53.                     }  
  54.                 }  
  55.                 if(line.length() > 0 && line.indexOf("=") != -1){  
  56.                     String[] kv = line.split("="2);  
  57.                     current.put(kv[0].trim(), kv[1].trim());  
  58.                 }  
  59.             }  
  60.         }  
  61.     }  
  62.   
  63.     public String getValue(String section, String name) {  
  64.         Map<String, String> sectionmap = sections.get(section);  
  65.   
  66.         if (sectionmap == null) {  
  67.             return null;  
  68.         }  
  69.   
  70.         String value = sectionmap.get(name);  
  71.         return value;  
  72.     }  
  73.       
  74.     public Set<String> sectionKeys(){  
  75.         return sections.keySet();  
  76.     }  
  77.       
  78.     public Map<String, String> getValues(String section){  
  79.         return sections.get(section);  
  80.     }  
  81.   
  82.     public boolean containsKey(String section, String key) {  
  83.         Map<String, String> m = sections.get(section);  
  84.         if(m != null){  
  85.             return m.get(key) == null;  
  86.         }  
  87.         return false;  
  88.     }  
  89.       
  90.     public Map<String, Map<String, String>> getSections(){  
  91.         return sections;  
  92.     }  
  93.   
  94. //  public static void main(String[] args) {  
  95. //      try {  
  96. //          IniReader r = new IniReader("C:/Users/dell/Desktop/aa.ini");  
  97.             String val = r.getValue("SET", "DBNAME");  
  98. //          Set<String> keys = r.sectionKeys();  
  99. //          for(String k : keys){  
  100. //              System.out.println("[" + k + "]");  
  101. //              Map<String, String> p = r.getValues(k);  
  102. //              for(Map.Entry<String, String> entry : p.entrySet()){  
  103. //                  System.out.println(entry.getKey() + "==" + entry.getValue());  
  104. //              }  
  105. //          }  
  106. //            
  107. //          Map<String, Map<String, String>> sections = r.getSections();  
  108. //          new IniWriter("C:/Users/dell/Desktop/bb.ini", sections);  
  109. //            
  110. //            
  111. //      } catch (IOException e) {  
  112. //          // TODO Auto-generated catch block  
  113. //          e.printStackTrace();  
  114. //      }  
  115. //  }  
  116.   
  117. }  

写文件:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. import java.io.BufferedWriter;  
  2. import java.io.File;  
  3. import java.io.FileWriter;  
  4. import java.io.IOException;  
  5. import java.util.Map;  
  6.   
  7. public class IniWriter {  
  8.       
  9.     private StringBuffer sb = new StringBuffer();  
  10.       
  11.     public static String LINE_SEPARATOR = System.getProperty("line.separator");  
  12.   
  13.     public IniWriter(String filename, Map<String, Map<String, String>> sections) throws IOException {  
  14.         BufferedWriter writer = null;  
  15.         try {  
  16.             writer = initWriter(filename);  
  17.             initIni(sections);  
  18.             writer.write(sb.toString());  
  19.         } finally{  
  20.             if(writer != null){  
  21.                 writer.close();  
  22.             }  
  23.         }  
  24.     }  
  25.       
  26.     //初始化流  
  27.     private BufferedWriter initWriter(String filename) throws IOException{  
  28.         File file = new File(filename);  
  29.         BufferedWriter writer = null;  
  30.         writer = new BufferedWriter(new FileWriter(file, false));  
  31.         return writer;  
  32.     }  
  33.       
  34.     //组装ini文件  
  35.     private void initIni(Map<String, Map<String, String>> sections){  
  36.         for(Map.Entry<String, Map<String, String>> e : sections.entrySet()){  
  37.             String k = e.getKey();  
  38.             Map<String, String> values = e.getValue();  
  39.             sb.append("[" + k + "]");  
  40.             sb.append(LINE_SEPARATOR);  
  41.             for(Map.Entry<String, String> ev : values.entrySet()){  
  42.                 sb.append(ev.getKey() + "=" + ev.getValue());  
  43.                 sb.append(LINE_SEPARATOR);  
  44.             }  
  45.         }  
  46.     }  
  47. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值