123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- package com.miniframe.tools;
- import java.io.BufferedReader;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.URL;
- import java.util.ArrayList;
- import java.util.Hashtable;
- public class IniFileUtil {
- private Hashtable<String, Object> paramTable;
- public IniFileUtil(String conf_filename) {
- try {
- this.loadFromFile(conf_filename);
- } catch (FileNotFoundException var3) {
- var3.printStackTrace();
- } catch (IOException var4) {
- var4.printStackTrace();
- }
- }
- public String getStrValue(String name) {
- if (this.paramTable == null) {
- return null;
- } else {
- Object obj = this.paramTable.get(name);
- if (obj == null) {
- return null;
- } else {
- return obj instanceof String ? (String)obj : (String)((ArrayList)obj).get(0);
- }
- }
- }
- public int getIntValue(String name, int default_value) {
- String szValue = this.getStrValue(name);
- return szValue == null ? default_value : Integer.parseInt(szValue);
- }
- public boolean getBoolValue(String name, boolean default_value) {
- String szValue = this.getStrValue(name);
- if (szValue == null) {
- return default_value;
- } else {
- return szValue.equalsIgnoreCase("yes") || szValue.equalsIgnoreCase("on") || szValue.equalsIgnoreCase("true") || szValue.equals("1");
- }
- }
- public String[] getValues(String name) {
- Object obj = this.paramTable.get(name);
- if (obj == null) {
- return null;
- } else {
- String[] values;
- if (obj instanceof String) {
- values = new String[]{(String)obj};
- return values;
- } else {
- Object[] objs = ((ArrayList)obj).toArray();
- values = new String[objs.length];
- System.arraycopy(objs, 0, values, 0, objs.length);
- return values;
- }
- }
- }
- private void loadFromFile(String conf_filename) throws FileNotFoundException, IOException {
- URL url = com.miniframe.core.IniFileReader.class.getClassLoader().getResource(conf_filename);
- InputStreamReader ipsr = null;
- try {
- ipsr = new InputStreamReader(url.openStream(), "UTF-8");
- BufferedReader buffReader = new BufferedReader(ipsr);
- this.paramTable = new Hashtable();
- String line;
- while((line = buffReader.readLine()) != null) {
- line = line.trim();
- if (line.length() != 0 && line.charAt(0) != '#') {
- String[] parts = line.split("=", 2);
- if (parts.length == 2) {
- String name = parts[0].trim();
- String value = parts[1].trim();
- Object obj = this.paramTable.get(name);
- if (obj == null) {
- this.paramTable.put(name, value);
- } else {
- ArrayList valueList;
- if (obj instanceof String) {
- valueList = new ArrayList();
- valueList.add(obj);
- valueList.add(value);
- this.paramTable.put(name, valueList);
- } else {
- valueList = (ArrayList)obj;
- valueList.add(value);
- }
- }
- }
- }
- }
- } catch (Exception var14) {
- var14.printStackTrace();
- } finally {
- if (ipsr != null) {
- ipsr.close();
- }
- }
- }
- }
|