1
package cn.com.cinda.rtx.common;
2
3
import java.io.BufferedReader;
4
import java.io.FileReader;
5
import java.io.IOException;
6
import java.util.HashMap;
7
import java.util.Properties;
8
import java.util.*;
9
import java.io.*;
10
11
12
public class IniReader
{
13
protected LinkedHashMap sections; //使用
14
15
public IniReader()
{
16
super();
17
sections = new LinkedHashMap();
18
}
19
20
public LinkedHashMap getSections()
{
21
return sections;
22
}
23
24
public LinkedHashMap getSection(String sectionName)
{
25
if (sections != null)
{
26
return (LinkedHashMap) sections.get(sectionName);
27
} else
{
28
return null;
29
}
30
}
31
32
public String get(String sectionName, String key)
{
33
Map section1 = (Map) sections.get(sectionName);
34
if (section1 == null)
{
35
return "";
36
}
37
return (String) section1.get(key);
38
}
39
40
41
public void load(String fileName) throws FileNotFoundException
{
42
InputStream is = null;
43
try
{
44
is = new FileInputStream(fileName);
45
load(is);
46
} catch (IOException e)
{
47
e.printStackTrace();
48
} finally
{
49
if (is != null)
{
50
try
{
51
is.close();
52
} catch (IOException e1)
{}
53
}
54
}
55
}
56
57
public void load(InputStream is) throws IOException
{
58
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
59
String line = null;
60
String lastSectionName = null;
61
while ((line = reader.readLine()) != null)
{
62
line = line.trim();
63
if (line.startsWith("#"))
{
64
continue;
65
}
66
if (line.startsWith("[") && line.endsWith("]"))
{
67
lastSectionName = line.trim().substring(1, line.length() - 1);
68
sections.put(lastSectionName, new LinkedHashMap()); //創建一個section
69
} else if (line.length() == 0)
{
70
continue;
71
} else
{
72
if (lastSectionName != null)
{
73
Map section = (Map) sections.get(lastSectionName);
74
int index = line.indexOf('=');
75
String key = index > 0 ? line.substring(0, index) : line; //如果沒有等號,直接做key和value
76
String value = index > 0 ? line.substring(index + 1) : line;
77
section.put(key, value);
78
}
79
}
80
}
81
}
82
83
public static void main(String[] args)
{
84
IniReader iniReader = new IniReader();
85
try
{
86
FileInputStream fileInputStream = new FileInputStream("/etc/portalconf/jmsconfig.property");
87
iniReader.load(fileInputStream);
88
Map sections = iniReader.getSection("serverconf");
89
java.util.Iterator it = sections.keySet().iterator();
90
while (it.hasNext())
{
91
String key = (String) it.next();
92
System.out.println(key + "=" + sections.get(key));
93
}
94
95
} catch (IOException e)
{
96
e.printStackTrace();
97
}
98
}
99
}
100
101
102

2

3

4

5

6

7

8

9

10

11

12



13

14

15



16

17

18

19

20



21

22

23

24



25



26

27



28

29

30

31

32



33

34



35

36

37

38

39

40

41



42

43



44

45

46



47

48



49



50



51

52



53

54

55

56

57



58

59

60

61



62

63



64

65

66



67

68

69



70

71



72



73

74

75

76

77

78

79

80

81

82

83



84

85



86

87

88

89

90



91

92

93

94

95



96

97

98

99

100

101

102
