Java 在类路径中定位文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2666875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 10:33:54  来源:igfitidea点击:

locating file in a classpath

java

提问by Gandalf StormCrow

I'm trying to read in file content, ex :

我正在尝试读取文件内容,例如:

public void myMethod(){
     FileInputStream fstream = new FileInputStream(fileLocation);
     BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
     String strLine;
     while ((strLine = br.readLine()) != null) {
....
....
.....
end while 
end method

And I have at the begining of the class body private String fileLocation;and at the end of a class I have a getter and setter for it. Now I'm trying inject this file location from spring inside bean from this class and I specify the init-method of this class. But I get error cannot find the specified file as if its not on a classpath but it is inside war file? I'm building the project with maven and I put file in src/main/resourcesThis is the error I get when trying to read file :

我在班级正文的开始和班级private String fileLocation;结束时都有一个 getter 和 setter。现在我正在尝试从这个类的 bean 中的 spring 注入这个文件位置,并指定这个类的 init 方法。但是我得到错误找不到指定的文件,好像它不在类路径上但它在战争文件中?我正在使用 maven 构建项目并将文件放入src/main/resources这是我在尝试读取文件时遇到的错误:

Error: src\main\resources\ids.txt (The system cannot find the path specified)

错误:src\main\resources\ids.txt(系统找不到指定的路径)

That is when I tried this :

那是我试过这个的时候:

FileInputStream fstream = new FileInputStream("src\main\resources\ids.txt");

how to reference the properly from the classpath?

如何从类路径中正确引用?

EDIT

编辑

When I edit my code according to @BalusC solution , here is how it looks but I still get nullerror :

当我根据@BalusC 解决方案编辑我的代码时,它看起来是这样的,但我仍然收到null错误:

ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 
   InputStream input = classLoader.getResourceAsStream("src/main/resources/ids.txt");
   BufferedReader br = new BufferedReader(new InputStreamReader(input));
   String strLine;
 while ((strLine = br.readLine()) != null) {
    ....
    ....
    .....
    end while 
    end method

采纳答案by BalusC

The Java IO API relies on the local disk file system, not on the classpath. Besides, using relative paths in Java IO stuff is recipe for portability trouble, don't rely on it. To allocate resources in the classpath you would normally use ClassLoader#getResource()or ClassLoader#getResourceAsStream().

Java IO API 依赖于本地磁盘文件系统,而不是类路径。此外,在 Java IO 中使用相对路径会导致可移植性问题,不要依赖它。要在类路径中分配资源,您通常会使用ClassLoader#getResource()ClassLoader#getResourceAsStream()

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream input = classLoader.getResourceAsStream("src/main/resources/ids.txt");

That said, you don't need that DataInputStreamline. You're actually not taking any benefit from it.

也就是说,你不需要那条DataInputStream线。你实际上并没有从中得到任何好处。

Update: if that doesn't work, then either the resource name is simply invalid or the file is actually not there in the classpath where you expect it to be. My cents on that the srcfolder is actually the rootof the classpath and not part of a package. Remove it from the name.

更新:如果这不起作用,那么要么资源名称无效,要么文件实际上不在您期望的类路径中。我认为该src文件夹实际上是类路径的根目录,而不是包的一部分。从名称中删除它。

Update 2: to get all root disk file system paths which are covered by the runtime classpath do:

更新 2:要获取运行时类路径涵盖的所有根磁盘文件系统路径,请执行以下操作:

for (URL root : Collections.list(Thread.currentThread().getContextClassLoader().getResources(""))) {
    System.out.println(root);
}

The resource name needs to be relative to either of them. That it is been placed in /WEB-INF/classesduring the build is normal. It is covered by the classpath. Your problem lies somewhere else. Are you sure that the resource name is correct? Are you sure that you're running the code you think you are running?

资源名称需要与其中任何一个相关。/WEB-INF/classes在构建过程中放置它是正常的。它被类路径覆盖。你的问题出在别处。您确定资源名称正确吗?您确定正在运行您认为正在运行的代码吗?