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í)!
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Map;
- import java.util.Set;
-
- /**
- * Simple reader methods for ini formated file using java
- * Using UTF-8 encoding.
- * Support DBCS, such as Chinese, Japanese
- * @author Wenjun Yang
- * @email yang.rangerwolf@gmail.com
- * 2011-10-30
- *
- */
- public class IniReader {
- // section item value
- private static Map<String, HashMap<String, String>> sectionsMap = new HashMap<String, HashMap<String, String>>();
- // item value
- private static HashMap<String, String> itemsMap = new HashMap<String, String>();
-
- private static String currentSection = "";
-
- /**
- * Load data from target file
- * @param file target file. It should be in ini format
- */
- private static void loadData(File file) {
- BufferedReader reader = null;
- try {
- reader = new BufferedReader(new FileReader(file));
- String line = null;
- while ((line = reader.readLine()) != null) {
- line = line.trim();
- if("".equals(line)) continue;
- if(line.startsWith("[") && line.endsWith("]")) {
- // Ends last section
- if(itemsMap.size() > 0 && !"".equals(currentSection.trim())) {
- sectionsMap.put(currentSection, itemsMap);
- }
- currentSection = "";
- itemsMap = null;
-
- // Start new section initial
- currentSection = line.substring(1, line.length() -1);
- itemsMap = new HashMap<String, String>();
- } else {
- int index = line.indexOf("=");
- if(index != -1) {
- String key = line.substring(0,index);
- String value = line.substring(index + 1, line.length());
- itemsMap.put(key, value);
- }
- }
- // System.out.println(line);
- }
- reader.close();
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (reader != null) {
- try {
- reader.close();
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- }
- }
- }
-
-
- public static String getValue(String section, String item, File file) {
- loadData(file);
-
- HashMap<String, String> map = sectionsMap.get(section);
- if(map == null) {
- return "No such section:" + section;
- }
- String value = map.get(item);
- if(value == null) {
- return "No such item:" + item;
- }
- return value;
- }
-
- public static String getValue(String section, String item, String fileName) {
- File file = new File(fileName);
- return getValue(section, item, file);
- }
-
- public static List<String> getSectionNames(File file) {
- List<String> list = new ArrayList<String>();
- loadData(file);
- Set<String> key = sectionsMap.keySet();
- for (Iterator<String> it = key.iterator(); it.hasNext();) {
- list.add(it.next());
- }
- return list;
- }
-
- public static Map<String, String> getItemsBySectionName(String section, File file) {
- loadData(file);
- return sectionsMap.get(section);
- }
- }
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請
點擊舉報。