简体   繁体   English

Java getClassLoader()。getResourceAsStream(文件名)

[英]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.

相关问题 getClassLoader().getResourceAsStream() 如何在 Java 中工作 - How getClassLoader().getResourceAsStream() works in java java jar getClass().getClassLoader().getResourceAsStream 返回空指针异常 - java jar getClass().getClassLoader().getResourceAsStream return nullpointerexception Java OutputStream 等价于 getClass().getClassLoader().getResourceAsStream() - Java OutputStream equivalent to getClass().getClassLoader().getResourceAsStream() getClassLoader().getResourceAsStream(resource) 在 java 中返回 null 11 - getClassLoader().getResourceAsStream(resource) return null in java 11 install4j6:class.getClassLoader()。getResourceAsStream(fileName)返回Null - install4j6: class.getClassLoader().getResourceAsStream(fileName) returns Null getClassLoader().getResourceAsStream() 在模块化 java 项目(openjdk 11)中不起作用? - getClassLoader().getResourceAsStream() not work in modular java project (openjdk 11)? getClass()。getClassLoader()。getResourceAsStream()正在缓存资源 - getClass().getClassLoader().getResourceAsStream() is caching the resource getClass()。getClassLoader()。getResourceAsStream引发NullPointerException - getClass().getClassLoader().getResourceAsStream is throwing a NullPointerException 使用class.getClassLoader()。getResourceAsStream时抛出java.lang.NullPointerException - java.lang.NullPointerException throws while using class.getClassLoader().getResourceAsStream class.getClassLoader getResourceasStream在jar中返回null - class.getClassLoader getResourceasStream returns null in jar
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM