[英]parsing an ini file and storing the values in a hashmap using scanner class
i am having a problem with the following code 我对以下代码有问题
public static void main(String[] args) throws FileNotFoundException, IOException {
Scanner br = new Scanner(new FileReader("/home/esunmes/NetBeansProjects/random/src/random/something.config"));
HashMap map = new HashMap();
String line;
String temp2;
while (br.hasNextLine()) {
line = br.next();
Scanner scan = new Scanner(new FileReader("/home/esunmes/NetBeansProjects/random/src/random/inifile.config"));
while (scan.hasNextLine()) {
temp2 = (String) scan.next();
if (temp2.equals(line)) {
Scanner scn = new Scanner(temp2);
String string;
while (scn.hasNextLine() && ((string = scn.next()) != "\n")) {
String[] temp3 = string.split("//=");
if (temp3.length > 1) {
String key = temp3[0];
String value = temp3[1];
map.put(key, value);// TODO code application logic here
}
}
}
}
}
Set set = map.entrySet();
Iterator iter = set.iterator();
while (iter.hasNext()) {
Map.Entry maps = (Map.Entry) iter.next();
String key = (String) maps.getKey();
String value = (String) maps.getValue();
System.out.println("key:" + key + " value" + value);
}
}
the two config files are 1.inifile.config 这两个配置文件是1.inifile.config
section1
key1=1
key2=2
section2
key4=4
key5=5
section3
key6=6
key3=3
section4
key7=7
section5
key8=8
section6
key9=9
section7
key10=10
section8
key11=11
2.something.config 2.something.config
section1
section2
section3
section4
section8
the first config file has sample logs and the second one has the names of the sections to be extracted 第一个配置文件具有示例日志,第二个配置文件具有要提取的部分的名称
the maps should contain the key value pair but they dont and the maps are coming out to be empty.Can someone please take the time to analyse this ...its really important 映射应该包含键值对,但是它们不包含,并且映射出来是空的。有人可以花点时间分析一下吗...它真的很重要
There are several problems in your code: 您的代码中存在几个问题:
You can simply use scan, which you already created, why you create a new scanner from a String? 您可以简单地使用已经创建的扫描,为什么要从字符串创建新的扫描器呢?
It should be string.split("=")
, you don't need to escape for "="
, and even if it need escape, it should be \\\\
not //
. 它应该是string.split("=")
,您不需要为"="
进行转义,即使需要转义,也应为\\\\
而不是//
。
hasNext()
should be followed with next()
, hasNextLine()
should be followed with nextLine()
, you should not use hasNextLine()
then use next()
. hasNext()
应该是next()
, hasNextLine()
应该是nextLine()
,您不应该使用hasNextLine()
然后使用next()
。 So the code should be 所以代码应该是
Scanner br = new Scanner(new FileReader("file2"));
HashMap map = new HashMap();
String line;
String temp2;
while (br.hasNextLine()) {
line = br.nextLine();
Scanner scan = new Scanner(new FileReader("file1"));
while (scan.hasNextLine()) {
temp2 = (String) scan.nextLine();
if (temp2.equals(line)) {
//Scanner scn = new Scanner(temp2);
String string;
while (scan.hasNext() && ((string = scan.next()) != "\n")) {
String[] temp3 = string.split("=");
if (temp3.length > 1) {
String key = temp3[0];
String value = temp3[1];
map.put(key, value);// TODO code application logic
// here
}
}
}
}
}
Set set = map.entrySet();
Iterator iter = set.iterator();
while (iter.hasNext()) {
Map.Entry maps = (Map.Entry) iter.next();
String key = (String) maps.getKey();
String value = (String) maps.getValue();
System.out.println("key:" + key + " value" + value);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.