@@ -21,7 +21,7 @@ public class IniFileReader
21
21
{
22
22
private Hashtable paramTable ;
23
23
private String conf_filename ;
24
-
24
+
25
25
/**
26
26
* @param conf_filename config filename
27
27
*/
@@ -30,7 +30,7 @@ public IniFileReader(String conf_filename) throws FileNotFoundException, IOExcep
30
30
this .conf_filename = conf_filename ;
31
31
loadFromFile (conf_filename );
32
32
}
33
-
33
+
34
34
/**
35
35
* get the config filename
36
36
* @return config filename
@@ -39,7 +39,7 @@ public String getConfFilename()
39
39
{
40
40
return this .conf_filename ;
41
41
}
42
-
42
+
43
43
/**
44
44
* get string value from config file
45
45
* @param name item name in config file
@@ -53,12 +53,12 @@ public String getStrValue(String name)
53
53
{
54
54
return null ;
55
55
}
56
-
56
+
57
57
if (obj instanceof String )
58
58
{
59
59
return (String )obj ;
60
60
}
61
-
61
+
62
62
return (String )((ArrayList )obj ).get (0 );
63
63
}
64
64
@@ -75,7 +75,7 @@ public int getIntValue(String name, int default_value)
75
75
{
76
76
return default_value ;
77
77
}
78
-
78
+
79
79
return Integer .parseInt (szValue );
80
80
}
81
81
@@ -92,11 +92,11 @@ public boolean getBoolValue(String name, boolean default_value)
92
92
{
93
93
return default_value ;
94
94
}
95
-
96
- return szValue .equalsIgnoreCase ("yes" ) || szValue .equalsIgnoreCase ("on" ) ||
95
+
96
+ return szValue .equalsIgnoreCase ("yes" ) || szValue .equalsIgnoreCase ("on" ) ||
97
97
szValue .equalsIgnoreCase ("true" ) || szValue .equals ("1" );
98
98
}
99
-
99
+
100
100
/**
101
101
* get all values from config file
102
102
* @param name item name in config file
@@ -106,77 +106,101 @@ public String[] getValues(String name)
106
106
{
107
107
Object obj ;
108
108
String [] values ;
109
-
109
+
110
110
obj = this .paramTable .get (name );
111
111
if (obj == null )
112
112
{
113
113
return null ;
114
114
}
115
-
115
+
116
116
if (obj instanceof String )
117
117
{
118
118
values = new String [1 ];
119
119
values [0 ] = (String )obj ;
120
120
return values ;
121
121
}
122
-
122
+
123
123
Object [] objs = ((ArrayList )obj ).toArray ();
124
124
values = new String [objs .length ];
125
125
System .arraycopy (objs , 0 , values , 0 , objs .length );
126
126
return values ;
127
127
}
128
-
129
- private void loadFromFile (String conf_filename ) throws FileNotFoundException , IOException
130
- {
131
- //问题说明 使用中发现原来客户端打jar包后,在另一个项目中引用,另一个项目打jar包后运行时找不到客户端配置文件
132
- String name ;
133
- String value ;
134
- Object obj ;
135
- ArrayList valueList ;
136
- InputStream is =null ;
137
- this .paramTable = new Hashtable ();
138
-
139
- try
140
- {
141
- Properties props ;
142
- try {
143
- is = Thread .currentThread ().getContextClassLoader ().getResourceAsStream (conf_filename );
144
- props = new Properties ();
145
- props .load (is );
146
- } catch (Exception ex ) {
147
- is = new FileInputStream (conf_filename );
148
- props = new Properties ();
149
- props .load (is );
150
- }
151
- Iterator <Map .Entry <Object , Object >> it = props .entrySet ().iterator ();
152
- while (it .hasNext ()) {
153
- Map .Entry <Object , Object > entry = it .next ();
154
- name = entry .getKey ().toString ();
155
- value = entry .getValue ().toString ();
156
- obj = this .paramTable .get (name );
157
- if (obj == null )
158
- {
159
- this .paramTable .put (name , value );
160
- }
161
- else if (obj instanceof String )
162
- {
163
- valueList = new ArrayList ();
164
- valueList .add (obj );
165
- valueList .add (value );
166
- this .paramTable .put (name , valueList );
167
- }
168
- else
169
- {
170
- valueList = (ArrayList )obj ;
171
- valueList .add (value );
172
- }
173
- }
128
+
129
+ private void loadFromFile (String confFilePath ) throws IOException {
130
+ InputStream in = null ;
131
+ try {
132
+ // 从类路径加载
133
+ in = classLoader ().getResourceAsStream (confFilePath );
134
+ //System.out.println("loadFrom...class path done");
135
+ readToParamTable (in );
136
+ } catch (Exception ex ) {
137
+ ex .printStackTrace ();
138
+ } finally {
139
+ try {
140
+ if (in != null ) in .close ();
141
+ //System.out.println("loadFrom...finally...in.close(); done");
142
+ } catch (Exception ex ) {
143
+ ex .printStackTrace ();
144
+ }
145
+ }
146
+ }
147
+
148
+ private void readToParamTable (InputStream in ) throws IOException {
149
+ this .paramTable = new Hashtable ();
150
+ String line ;
151
+ String [] parts ;
152
+ String name ;
153
+ String value ;
154
+ Object obj ;
155
+ ArrayList valueList ;
156
+ InputStreamReader inReader = null ;
157
+ BufferedReader bufferedReader = null ;
158
+ try {
159
+ inReader = new InputStreamReader (in );
160
+ bufferedReader = new BufferedReader (inReader );
161
+ while ((line = bufferedReader .readLine ()) != null ) {
162
+ line = line .trim ();
163
+ if (line .length () == 0 || line .charAt (0 ) == '#' ) {
164
+ continue ;
174
165
}
175
- finally
176
- {
177
- if (is != null ) {
178
- is .close ();
179
- }
166
+ parts = line .split ("=" , 2 );
167
+ if (parts .length != 2 ) {
168
+ continue ;
180
169
}
170
+ name = parts [0 ].trim ();
171
+ value = parts [1 ].trim ();
172
+ obj = this .paramTable .get (name );
173
+ if (obj == null ) {
174
+ this .paramTable .put (name , value );
175
+ } else if (obj instanceof String ) {
176
+ valueList = new ArrayList ();
177
+ valueList .add (obj );
178
+ valueList .add (value );
179
+ this .paramTable .put (name , valueList );
180
+ } else {
181
+ valueList = (ArrayList ) obj ;
182
+ valueList .add (value );
183
+ }
184
+ }
185
+ } catch (Exception ex ) {
186
+ ex .printStackTrace ();
187
+ } finally {
188
+ try {
189
+ if (bufferedReader != null ) bufferedReader .close ();
190
+ if (inReader != null ) inReader .close ();
191
+ //System.out.println("readToParamTable...finally...bufferedReader.close();inReader.close(); done");
192
+ } catch (Exception ex ) {
193
+ ex .printStackTrace ();
194
+ }
195
+ }
196
+ }
197
+
198
+ private static ClassLoader classLoader () {
199
+ ClassLoader loader = Thread .currentThread ().getContextClassLoader ();
200
+ if (loader == null ) {
201
+ loader = ClassLoader .getSystemClassLoader ();
181
202
}
203
+ return loader ;
204
+ }
205
+
182
206
}
0 commit comments