package projectmanager import java io BufferedReader import java io Fi

 1
 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
package projectmanager;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class IniReader {
private final Pattern _section = Pattern.compile("\\s*\\[([^]]*)\\]\\s*");
private final Pattern _keyValue = Pattern.compile("\\s*([^=]*)=(.*)");
final private Map<String, Map<String, String>> _entries = new HashMap<>();
public IniReader(String path) {
try {
try(BufferedReader br = new BufferedReader(new FileReader(path))) {
String line;
String section = null;
while((line = br.readLine()) != null ) {
Matcher m = _section.matcher(line);
if(m.matches())
section = m.group(1).trim();
else if(section != null) {
m = _keyValue.matcher(line);
if(m.matches()) {
String key = m.group(1).trim();
String value = m.group(2).trim();
Map<String, String> kv = _entries.get(section);
if(kv == null)
_entries.put(section, kv = new HashMap<>());
kv.put(key, value);
}
}
}
}
}catch(IOException io) {
System.out.println("Ошибка чтения config.ini");
System.exit(0);
}
}
public String get(String section, String key) {
Map<String, String> kv = _entries.get(section);
return kv.get(key);
}
}