Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 解析ini文件并使用scanner类将值存储在hashmap中_Java_Hashmap - Fatal编程技术网

Java 解析ini文件并使用scanner类将值存储在hashmap中

Java 解析ini文件并使用scanner类将值存储在hashmap中,java,hashmap,Java,Hashmap,我对以下代码有问题 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(); Stri

我对以下代码有问题

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);
    }
}
这两个配置文件是 1.ini文件.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

 section1
 section2
 section3
 section4
 section8
第一个配置文件包含示例日志,第二个配置文件包含要提取的节的名称

映射应该包含键值对,但它们不包含
而地图显示为空。有人能花点时间分析一下吗……这真的很重要

您的代码中有几个问题:

  • 扫描仪scn=新扫描仪(temp2)
您可以简单地使用已经创建的扫描,为什么要从字符串创建新的扫描程序

  • string.split(“//=”)
它应该是
string.split(“=”
),您不需要为
“=”
转义,即使它需要转义,也应该是
\\
而不是
/

  • hasNext()

所以代码应该是

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);
}