我想要在代码中读取名为FileofNames.txt的文件。但是当我运行这段代码时,我得到一个NullPointerException。在Intellij中放置文本文件的位置
URL url=ApacheCommonIOExample.class.getResource("src/Files/FileofNames.txt");
System.out.println(url.getPath());
所以我的问题是,我可以把文件放在intellij中,以便我可以从任何java源代码中读取它。 Image of my directory 。目前我的文本文件在src/Files中,我想从src/org/java.practice/FileName.java中读取它。
这个方法奏效了,非常感谢你!但是当我尝试运行此代码时,它会再次打印,您能告诉我为什么吗? 'URL url = ApacheCommonIOExample.class.getResource(“/ FileofNames”); File file = new File(url.getPath()); (file.exists()) System.out.print(“exists”);如果(file.exists()) System.out.print(“exists”); else System.out.print(“try again”);' –