java读取ini文件

来源:互联网 发布:unix和linux的区别 编辑:程序博客网 时间:2024/04/30 08:00

项目中要用到读取ini文件内容,百度加google后找到一篇个人认为很好的例子,地址http://rangerwolf.iteye.com/blog/1222882 。但是经过使用发现美中存在不足,1.还是存在中文乱码问题 。2,最后一个section的值获取不到。  后来debug了一下,发现貌似是逻辑判断的问题,个人改进了一番。

 

package fileIO;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;public class JavaRead_Ini2 {/** * 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 *  */// section item valueprivate static Map<String, HashMap<String, String>> sectionsMap = new HashMap<String, HashMap<String, String>>();// item valueprivate 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));  reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "gbk"));String line = null;while ((line = reader.readLine()) != null) {line = line.trim();if ("".equals(line))continue;if (line.startsWith("[") && line.endsWith("]")) {////if (itemsMap.size() > 0////&& !"".equals(currentSection.trim())) {////sectionsMap.put(currentSection, itemsMap);////}////currentSection = "";////////// Start new section initial////currentSection = line.substring(1, line.length() - 1);////itemsMap = new HashMap<String, String>();itemsMap = new HashMap<String, String>();// Ends last sectioncurrentSection = line.substring(1, line.length() - 1);sectionsMap.put(currentSection, itemsMap);currentSection = "";// Start new section initial} 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);}public static void main(String[] args) {HashMap<String, String> dataMap = (HashMap)JavaRead_Ini2.getItemsBySectionName("temp", new File("D:/Subtitle.ini"));Iterator iter = dataMap.entrySet().iterator();while(iter.hasNext()){Map.Entry entry = (Map.Entry)iter.next();System.out.println(entry.getKey()+":"+entry.getValue());}}}

 

前面打////的参考的源码,后来自己修改了一下

 

原创粉丝点击