Io 在Java中将文件输入扫描到HashMap中

Io 在Java中将文件输入扫描到HashMap中,io,hashmap,java.util.scanner,delimiter,Io,Hashmap,Java.util.scanner,Delimiter,因此,我试图遍历一个目录,找到一个名为“rels.txt”的文件的所有实例,对于每个实例,我想进入该文件,并将该文件中的各个值加载到Java的HashMap中。下面是我正在处理的一个“rels.txt”文件的内容: 文本文件: rId8,image2 rId13,image5 rId7,image1 rId12,image4 rId17,image8 rId15,image7 rId9,image3 rId14,image6 private void traverse(File directo

因此,我试图遍历一个目录,找到一个名为“rels.txt”的文件的所有实例,对于每个实例,我想进入该文件,并将该文件中的各个值加载到Java的HashMap中。下面是我正在处理的一个“rels.txt”文件的内容:

文本文件:

rId8,image2
rId13,image5
rId7,image1
rId12,image4
rId17,image8
rId15,image7
rId9,image3
rId14,image6
private void traverse(File directory) throws FileNotFoundException
    {
        //Get all files in directory
        File[] files = directory.listFiles();
        for (File file : files)
        {
            if (file.getName().equals("rels.txt"))
            {
                Scanner scan = new Scanner(file).useDelimiter(",");
                while (scan.hasNextLine())
                {
                    String id;
                    String image;

                    id = scan.next();
                    image = scan.next();
                    imageMap.put(id, image);
                }
                System.out.println(imageMap);
                imageMap.clear();
            }

            else if (file.isDirectory())
            {
                //It's a directory so (recursively) traverse it
                traverse(file);
            }
        }
    }
private void traverse(File directory) throws FileNotFoundException
        {
            //Get all files in directory
            File[] files = directory.listFiles();
            for (File file : files)
            {
                if (file.getName().equals("rels.txt"))
                {
                    Scanner scan = new Scanner(file);
                    while (scan.hasNextLine())
                    {
                        String[] line = scan.nextLine().split(",");
                       imageMap.put(line[0], line[1]);

                    }
                    System.out.println(imageMap);
                      imageMap.clear();
                }

                else if (file.isDirectory())
                {
                    //It's a directory so (recursively) traverse it
                    traverse(file);
                }
            }
        }
这里是我到目前为止所拥有的Java代码,它应该遍历目录并在HashMap中存储每个值,打印出HashMap,然后清除它(这样就可以从目录中的另一个“rels.txt”文件中存储一组新的值)

Java代码:

rId8,image2
rId13,image5
rId7,image1
rId12,image4
rId17,image8
rId15,image7
rId9,image3
rId14,image6
private void traverse(File directory) throws FileNotFoundException
    {
        //Get all files in directory
        File[] files = directory.listFiles();
        for (File file : files)
        {
            if (file.getName().equals("rels.txt"))
            {
                Scanner scan = new Scanner(file).useDelimiter(",");
                while (scan.hasNextLine())
                {
                    String id;
                    String image;

                    id = scan.next();
                    image = scan.next();
                    imageMap.put(id, image);
                }
                System.out.println(imageMap);
                imageMap.clear();
            }

            else if (file.isDirectory())
            {
                //It's a directory so (recursively) traverse it
                traverse(file);
            }
        }
    }
private void traverse(File directory) throws FileNotFoundException
        {
            //Get all files in directory
            File[] files = directory.listFiles();
            for (File file : files)
            {
                if (file.getName().equals("rels.txt"))
                {
                    Scanner scan = new Scanner(file);
                    while (scan.hasNextLine())
                    {
                        String[] line = scan.nextLine().split(",");
                       imageMap.put(line[0], line[1]);

                    }
                    System.out.println(imageMap);
                      imageMap.clear();
                }

                else if (file.isDirectory())
                {
                    //It's a directory so (recursively) traverse it
                    traverse(file);
                }
            }
        }
这是我得到的错误/输出,我不太确定哪里出了问题

错误/输出

Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at XMLTagParser.traverse(XMLTagParser.java:153)
    at XMLTagParser.traverse(XMLTagParser.java:198)
    at XMLTagParser.traverse(XMLTagParser.java:198)
    at XMLTagParser.<init>(XMLTagParser.java:27)
    at IPDriver.main(IPDriver.java:21)
{image26
rId19=image9
rId31, image15
rId33=image23
rId38, image29
rId21=image11
rId34, image12
rId27=image17
rId30, image28
rId16=image6
rId20, image14
rId32=image22
rId37, image2
rId17=image7
rId25, rId13=image3
rId18, image20
rId35=image25
, image8
rId26=image16
rId39, image24
rId7=image1
rId12, image21
rId14=image4
rId22, image10
rId29=image19
rId24, image13
rId28=image18
rId36, image27
rId15=image5
rId23}

假设您的文件正是您所说的,您可以尝试将
hasNextLine
更改为
hasNext

假设您的文件正是您所说的,您可以尝试将
hasNextLine
更改为
hasNext

,因为分隔符是“,”scan.next()一起读取“image2\nrId13”。它将返回视为一个字符,而不是分隔符

另一种方法是删除扫描仪的“useDelimiter(,”),并使用此循环:

            Scanner scan = new Scanner(file);
            while (scan.hasNextLine())
            {
                String[] line = scan.nextLine().split(",");
                imageMap.put(line[0], line[1]);
            }
            System.out.println(imageMap)
只需确保文件不会以返回结尾。

因为分隔符是“,”scan.next()同时读取“image2\nrId13”。它将返回视为一个字符,而不是分隔符

另一种方法是删除扫描仪的“useDelimiter(,”),并使用此循环:

            Scanner scan = new Scanner(file);
            while (scan.hasNextLine())
            {
                String[] line = scan.nextLine().split(",");
                imageMap.put(line[0], line[1]);
            }
            System.out.println(imageMap)

只需确保您的文件不会以返回结尾。

我明白您的意思,但如何在分隔符中添加额外的参数?(第一次使用它,抱歉lol)可能在正则表达式“[,][\n]”中。不完全确定。如果你愿意,我可以给你另一种方法。是的,当然,我愿意接受任何有效的建议。比我想做的要干净得多。谢谢你,伙计!我明白你的意思,但是我如何在分隔符中添加一个额外的参数呢?(第一次使用它,抱歉lol)可能在正则表达式“[,][\n]”中。不完全确定。如果你愿意,我可以给你另一种方法。是的,当然,我愿意接受任何有效的建议。比我想做的要干净得多。谢谢你,伙计!