国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開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

 

我才寫了百來行,人家的代碼有上千行。我的只讀,人家的還能修改,確實很強。

不過還沒測試過中文,應(yīng)該木有問題!

----

 

 

ini文件在日常工作中用得還是挺廣泛的。在目前我手上的一個小項目之中,也用到了ini文件。而且里面存儲了中文。

在網(wǎng)上也找了別人的代碼,比如有個“由月”大神寫的,我覺得寫得就挺好的。

但是發(fā)現(xiàn)不支持中文。于是乎就自己寫了一個簡單的讀取ini文件的方法。也在此共享出來。

歡迎大家提意見!

 

Known Issues:

1. 調(diào)用的java source file必須是UTF-8格式的。我在Eclipse3.7中,發(fā)現(xiàn)默認的格式為CP1252. 不支持中文。因此調(diào)用打印的結(jié)果也是亂碼。

2. 第一行是section名字的話,發(fā)現(xiàn)無法讀出來。直接略過?!疚易约阂膊恢涝趺唇鉀Q?!?/p>

 

PS:進公司才發(fā)現(xiàn),原來GoAgent的原創(chuàng)者phus就坐在我旁邊,好崇拜,好幸運??!哈哈!

向他學(xué)習(xí)!

 

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. }  

 

本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
個人寫的文本替換小程序
java面試題集二
JAVA程序員面試32問
字符串操作工具 StringTools
Java_功能實現(xiàn)_WordCount
android 使用軟引用異步加載圖片
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服