简体   繁体   English

Java:程序无法从其中读取时列出txt文件的内容

[英]Java: Program not able to list contents of txt file when reading from it

I'm working on a program that uses JavaFx to display icons in a list. 我正在使用JavaFx在列表中显示图标的程序上工作。 I've made a static class used to look up specific ids from a txt document. 我制作了一个静态类,用于从txt文档中查找特定ID。 Originally, the static block would add the id and name of an item defined on each line, but since these issues arose, I've tried to find the source of the issue. 最初,静态块会在每行中添加定义的项的ID和名称,但是由于出现了这些问题,因此我尝试查找问题的根源。

Instead, I've just gone through the text file's content in the static block and have printed it out to the console. 相反,我只是浏览了静态块中文本文件的内容,并将其打印到控制台。

This is my code for reference: 这是我的代码供参考:

public class ItemIds {
    public static int UNDEFINED_ID = -1;
    private static HashMap<String, Integer> items;
    static {
        items = new HashMap<String, Integer>();
        System.out.println(new File("res/ids/item ids.txt").exists());
        try {
            //should print out every line in the text file
            Files.lines(Paths.get("res/ids/item ids.txt")).forEach(s -> {
                System.out.println(s);
            });
        } catch (IOException e) {
            System.out.println("Unable to read specified file.");
            e.printStackTrace();
        }
    }

    public static int getId(final String name) {
        final Integer id = items.get(name);
        return id != null ? id : UNDEFINED_ID;
    }
}

However, what I do get when this static class is initialized and the static block is invoked is quite odd. 但是,初始化此静态类并调用静态块时得到的结果很奇怪。 It lists every single line without error until it gets to line 10691, where it throws "Exception in thread "JavaFX Application Thread" java.lang.ExceptionInInitializerError". 它列出每一行都没有错误,直到到达10691行,然后在该行引发“ JavaFX应用程序线程中的异常” java.lang.ExceptionInInitializerError。

What makes this particularly weird, however, is that when I work with a smaller text document (with less entries), everything seems to work fine. 但是,这使我特别奇怪的是,当我使用较小的文本文档(条目较少)时,一切似乎都可以正常工作。 Since the file is comprised of almost 14000 lines, I have to delete ~4000 lines for it to be able to work. 由于该文件包含将近14000行,因此我必须删除〜4000行才能使用。

Any ideas on why it would be doing this? 关于为什么要这样做的任何想法? Any feedback is appreciated - thank you 任何反馈表示赞赏-谢谢

I am unable to reproduce this error. 我无法重现此错误。 I have created a file with 18K lines and you program just works fine with that. 我已经创建了一个包含18K行的文件,并且您的程序运行正常。 So, definitely consider reviewing your file and also the stack trace. 因此,绝对要考虑检查您的文件以及堆栈跟踪。

Now coming back to your exception ExceptionInInitializerError , the following is a possible: 现在回到异常ExceptionInInitializerError ,可能是以下情况:

ExceptionInInitializerError signals that an unexpected exception has occurred in a static initializer. ExceptionInInitializerError表示静态初始化程序中发生了意外的异常。 An ExceptionInInitializerError is thrown to indicate that an exception occurred during evaluation of a static initializer or the initializer for a static variable. 抛出ExceptionInInitializerError表示在评估静态初始化程序或静态变量的初始化程序期间发生了异常。

class ItemIds
{
  static
  {
     // if something does wrong -> ExceptionInInitializerError
  }
}

Because static variables are initialized in static blocks there is a potential for introducing errors too. 由于静态变量是在静态块中初始化的,因此也有可能引入错误。 An example: 一个例子:

class ItemIds
{
  static int v = D.foo();
}

=> =>

class ItemIds
{
  static int v;

  static
  {
    v = D.foo();
  }
}

So if foo() goes crazy then you can get a ExceptionInInitializerError. 因此,如果foo()变得疯狂,则可以获取ExceptionInInitializerError。

Have you presented your complete code in static block? 您是否以静态块形式展示了完整的代码?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM