打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
用java读取ini文件(支持中文)

2012-11-29

google出一个非常犀利的iniEditor: http://grepcode.com/file/repository.jboss.org/nexus/content/repositories/releases/org.rhq/rhq-core-util/3.0.0.EmbJopr4/org/rhq/core/util/IniEditor.java

 

我才写了百来行,人家的代码有上千行。我的只读,人家的还能修改,确实很强。

不过还没测试过中文,应该木有问题!

----

 

 

ini文件在日常工作中用得还是挺广泛的。在目前我手上的一个小项目之中,也用到了ini文件。而且里面存储了中文。

在网上也找了别人的代码,比如有个“由月”大神写的,我觉得写得就挺好的。

但是发现不支持中文。于是乎就自己写了一个简单的读取ini文件的方法。也在此共享出来。

欢迎大家提意见!

 

Known Issues:

1. 调用的java source file必须是UTF-8格式的。我在Eclipse3.7中,发现默认的格式为CP1252. 不支持中文。因此调用打印的结果也是乱码。

2. 第一行是section名字的话,发现无法读出来。直接略过。【我自己也不知道怎么解决。】

 

PS:进公司才发现,原来GoAgent的原创者phus就坐在我旁边,好崇拜,好幸运啊!哈哈!

向他学习!

 

Java代码  
  1. import java.io.BufferedReader;  
  2. import java.io.File;  
  3. import java.io.FileReader;  
  4. import java.io.IOException;  
  5. import java.util.ArrayList;  
  6. import java.util.HashMap;  
  7. import java.util.Iterator;  
  8. import java.util.List;  
  9. import java.util.Map;  
  10. import java.util.Set;  
  11.   
  12. /** 
  13.  * Simple reader methods for ini formated file using java 
  14.  * Using UTF-8 encoding. 
  15.  * Support DBCS, such as Chinese, Japanese 
  16.  * @author Wenjun Yang 
  17.  * @email  yang.rangerwolf@gmail.com 
  18.  * 2011-10-30    
  19.  * 
  20.  */  
  21. public class IniReader {  
  22.                     // section        item     value  
  23.     private static Map<String, HashMap<String, String>> sectionsMap = new HashMap<String, HashMap<String, String>>();  
  24.                     //      item    value  
  25.     private static HashMap<String, String> itemsMap = new HashMap<String, String>();  
  26.       
  27.     private static String currentSection = "";  
  28.   
  29.     /** 
  30.      * Load data from target file 
  31.      * @param file target file. It should be in ini format 
  32.      */  
  33.     private static void loadData(File file) {  
  34.         BufferedReader reader = null;  
  35.         try {  
  36.             reader = new BufferedReader(new FileReader(file));  
  37.             String line = null;  
  38.             while ((line = reader.readLine()) != null) {  
  39.                 line = line.trim();  
  40.                 if("".equals(line)) continue;  
  41.                 if(line.startsWith("[") && line.endsWith("]")) {  
  42.                     // Ends last section  
  43.                     if(itemsMap.size() > 0 && !"".equals(currentSection.trim())) {  
  44.                         sectionsMap.put(currentSection, itemsMap);  
  45.                     }  
  46.                     currentSection = "";  
  47.                     itemsMap = null;  
  48.                       
  49.                     // Start new section initial  
  50.                     currentSection = line.substring(1, line.length() -1);  
  51.                     itemsMap = new HashMap<String, String>();   
  52.                 } else {  
  53.                     int index = line.indexOf("=");  
  54.                     if(index != -1) {  
  55.                         String key = line.substring(0,index);  
  56.                         String value = line.substring(index + 1, line.length());  
  57.                         itemsMap.put(key, value);  
  58.                     }  
  59.                 }  
  60. //              System.out.println(line);  
  61.             }  
  62.             reader.close();  
  63.         } catch (Exception e) {  
  64.             e.printStackTrace();  
  65.         } finally {  
  66.             if (reader != null) {  
  67.                 try {  
  68.                     reader.close();  
  69.                 } catch (IOException e1) {  
  70.                     e1.printStackTrace();  
  71.                 }  
  72.             }  
  73.         }  
  74.     }  
  75.       
  76.       
  77.     public static String getValue(String section, String item, File file) {  
  78.         loadData(file);  
  79.           
  80.         HashMap<String, String> map = sectionsMap.get(section);  
  81.         if(map == null) {  
  82.             return "No such section:" + section;  
  83.         }  
  84.         String value = map.get(item);  
  85.         if(value == null) {  
  86.             return "No such item:" + item;  
  87.         }  
  88.         return value;  
  89.     }  
  90.       
  91.     public static String getValue(String section, String item, String fileName) {  
  92.         File file = new File(fileName);  
  93.         return getValue(section, item, file);  
  94.     }  
  95.       
  96.     public static List<String> getSectionNames(File file) {  
  97.         List<String> list = new ArrayList<String>();  
  98.         loadData(file);  
  99.         Set<String> key = sectionsMap.keySet();  
  100.         for (Iterator<String> it = key.iterator(); it.hasNext();) {  
  101.             list.add(it.next());  
  102.         }  
  103.         return list;  
  104.     }  
  105.       
  106.     public static Map<String, String> getItemsBySectionName(String section, File file) {  
  107.         loadData(file);  
  108.         return sectionsMap.get(section);  
  109.     }  
  110. }  

 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
个人写的文本替换小程序
java面试题集二
JAVA程序员面试32问
字符串操作工具 StringTools
Java_功能实现_WordCount
android 使用软引用异步加载图片
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服