在类路径中查找文件 [英] locating file in a classpath
问题描述
我正在尝试阅读文件内容,例如:
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
我在课堂开始时有 private String fileLocation;
并且在类的末尾我有一个getter和setter。现在我正在尝试从这个类中的bean内部注入此文件位置,并指定此类的init方法。但我得到错误找不到指定的文件,好像它不在类路径上,但它是在war文件中?我正在用maven构建项目,我把文件放在 src / main / resources
这是我在尝试读取文件时得到的错误:
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/resources
This is the error I get when trying to read file :
错误:src\main\resources\ids.txt
(系统找不到指定的路径
)
Error: src\main\resources\ids.txt (The system cannot find the path specified)
这是我试过的时间:
FileInputStream fstream = new FileInputStream("src\\main\\resources\\ids.txt");
如何正确引用类路径?
编辑
当我根据@BalusC解决方案编辑我的代码时,这是它的外观,但我仍然得到 null
错误:
When I edit my code according to @BalusC solution , here is how it looks but I still get null
error :
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
推荐答案
Java IO API依赖于本地磁盘文件系统,而不是类路径。此外,在Java IO中使用相对路径是可移植性问题的秘诀,不要依赖它。要在类路径中分配资源,通常使用 ClassLoader#getResource()
或 ClassLoader#getResourceAsStream()
。
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()
.
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream input = classLoader.getResourceAsStream("src/main/resources/ids.txt");
那就是说,你不需要 DataInputStream
线。你实际上并没有从中获得任何好处。
That said, you don't need that DataInputStream
line. You're actually not taking any benefit from it.
更新:如果这不起作用,则资源名称无效或者文件实际上不在您期望的类路径中。我在 src
文件夹中的分数实际上是类路径的 root ,而不是包的一部分。从名称中删除它。
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 src
folder is actually the root of the classpath and not part of a package. Remove it from the name.
更新2 :获取运行时类路径所涵盖的所有根磁盘文件系统路径:
Update 2: to get all root disk file system paths which are covered by the runtime classpath do:
for (URL root : Collections.list(Thread.currentThread().getContextClassLoader().getResources(""))) {
System.out.println(root);
}
资源名称必须相对于其中任何一个。在构建期间它被放置在 / WEB-INF / classes
中是正常的。它被类路径覆盖。你的问题出在其他地方。您确定资源名称是否正确?你确定你正在运行你认为正在运行的代码吗?
The resource name needs to be relative to either of them. That it is been placed in /WEB-INF/classes
during 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?
这篇关于在类路径中查找文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!