[英]Java getClassLoader().getResourceAsStream(filename)
I have a Maven project. 我有一个Maven项目。 My entire code is in the folder {PROJECT_ROOT}/src/main/java. 我的整个代码在文件夹{PROJECT_ROOT} / src / main / java中。 When I write the code: 当我编写代码时:
InputStream input = VertxApp.class.getClassLoader().getResourceAsStream("file.txt");
String result = getStringFromInputStream(input);
System.out.println(result);
I see that the code looks for the file named "file.txt" in the folder: {PROJECT_ROOT}/src/main/resources. 我看到代码在文件夹{PROJECT_ROOT} / src / main / resources中查找名为“ file.txt”的文件。
My question is why. 我的问题是为什么。 Why doesn't it look for files in {PROJECT_ROOT}/src/main/java or in ${PROJECT_ROOT} for example? 例如,为什么它不在{PROJECT_ROOT} / src / main / java或$ {PROJECT_ROOT}中查找文件?
Most Maven projects follow the structure of 大多数Maven项目遵循以下结构
src/
main/
java/
resources/
You're intended to keep your non-code resources separate from your code. 您打算将非代码资源与代码分开。 The getResourceAsStream
method assumes this standard is followed, so it looks for your resource starting from the resources/
directory. getResourceAsStream
方法假定遵循此标准,因此它将从resources/
目录开始查找resources/
。
See Why are project layout resources are kept separate from Java sources? 请参阅为什么将项目布局资源与Java源代码分开? for some more detailed thoughts. 一些更详细的想法。
When you build a maven project or run it from an IDE supporting maven, the java files from src/main/java
are compiled to class files and put in PROJECT_ROOT/target/classes
. 当您构建maven项目或从支持maven的IDE运行它时,来自src/main/java
的Java文件将编译为类文件,并放入PROJECT_ROOT/target/classes
。
The files from src/main/resources
are copied to PROJECT_ROOT/target/classes
as well. 来自src/main/resources
的文件也被复制到PROJECT_ROOT/target/classes
。
When you call VertxApp.class.getClassLoader().getResourceAsStream("file.txt")
you get the classloader for the VertxApp
class and this classloader will try to load the given resource from the path where the class was loaded from. 当您调用VertxApp.class.getClassLoader().getResourceAsStream("file.txt")
您将获得VertxApp
类的类加载器, VertxApp
该类加载器将尝试从加载该类的路径中加载给定资源。
If your VertxApp
class is in the top package, the the class file will be in target/classes
and the resource will be searched there as well as you specify a relative path of "file.txt"
, therefore you must put it in src/main/resources
. 如果您的VertxApp
类位于顶层软件包中,则该类文件将位于target/classes
并且将在该目录中搜索资源,同时您指定相对路径"file.txt"
,因此必须将其放在src/main/resources
。
If the VertxApp
class is for example in a package named mypackage
then the class file would be in target/classes/mypackage
and you should have the file in src/main/resources/mypackage
so that it ends up in target/classes/mypackage
as well. 例如,如果VertxApp
类位于名为mypackage
的软件包中,则该类文件将位于target/classes/mypackage
并且您应该将文件包含在src/main/resources/mypackage
以便最终以以下形式出现在target/classes/mypackage
:好。
If you have the VertxApp
class in a package but want to keep the file in src/main/resources
(without a subdirectory), you must reference it as an absolute path like this: 如果您在软件包中包含VertxApp
类,但想要将文件保留在src/main/resources
(没有子目录),则必须将其作为绝对路径引用,如下所示:
VertxApp.class.getClassLoader().getResourceAsStream("/file.txt")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.