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

发布于 2024-11-18 22:39:20 字数 3281 浏览 3 评论 0原文

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

文本文件:

rId8,image2
rId13,image5
rId7,image1
rId12,image4
rId17,image8
rId15,image7
rId9,image3
rId14,image6

这是我迄今为止的 Java 代码,它应该遍历目录并存储每个HashMap 中的值,打印出 HashMap,然后清除它(以便可以从目录中的另一个“rels.txt”文件存储一组新值)。

Java 代码:

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

这是我收到的错误/输出,我不太确定哪里出了问题。

错误/输出

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}

通过这段代码,我本质上只是希望能够为项目的另一部分调用 HashMap,并且当我引用其中一个 rId 值时,我可以获得相应的图像值。有谁知道我可能做错了什么,或者甚至有更有效的解决方案?提前致谢。

工作 Java 代码感谢 Friedrik:

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

So I am trying to traverse a directory and find all the instances of a file called "rels.txt", and for each instance, I want to go into the file and load up the individual values from the file into a HashMap in Java. Below are the contents of one of the "rels.txt" files I am working with:

Text File:

rId8,image2
rId13,image5
rId7,image1
rId12,image4
rId17,image8
rId15,image7
rId9,image3
rId14,image6

Here is the Java code I have thus far that is supposed to traverse the directory and store each of the values in the HashMap, print out the HashMap, then clear it (so that a new set of values can be stored from another "rels.txt" file within the directory).

Java Code:

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

This is the error/output I am getting and I am not quite sure where I am going wrong.

Error/Output

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}

With this code, I essentially just want to be able to call the HashMap for another part of my project and when I reference one of the rId values, I can get the corresponding image value. Does anyone have any ideas as to what I may be doing wrong or maybe even a more efficient solution? Thanks in advance.

Working Java Code Thanks to Friedrik:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

葬﹪忆之殇 2024-11-25 22:39:20

由于分隔符是“,”,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)

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

Since the delimiter is "," scan.next() reads "image2\nrId13" together. It considers the return as a character, not a delimiter.

Another way to do this, delete the "useDelimiter(",")" of the Scanner and use this loop :

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

just make sure that your file doesn't end by a return.

傲娇萝莉攻 2024-11-25 22:39:20

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

Assuming your file is exactly what you said, you can try changing the hasNextLine to hasNext

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文