简体   繁体   English

使用getResourceAsStream空读取配置文件

[英]Null Reading config file with getResourceAsStream

I have a class that reads a config file from the project root but it is not finding the file. 我有一个从项目根目录读取配置文件的类,但找不到该文件。

My read class is in com.br.xxx.util package 我的读取类在com.br.xxx.util包中

        String filename = "config.properties";
        input = getClass().getResourceAsStream(filename);

This code returns null even though "config.properties" is in my project root. 即使“ config.properties”在我的项目根目录中,此代码也返回null。 What am I doing wrong? 我究竟做错了什么?

If I use: 如果我使用:

InputStream input = new FileInputStream("C:\\Users\\xxxx\\git\\projectfolder\\project\\config.properties");
configFile.load(input);

It works, but I don't want to use a system path. 它有效,但是我不想使用系统路径。

config.properties must be in your classpath . config.properties必须在您的classpath when you deploy your project on tomcat, by default WEB-IN/classes is in classpath . 当您在tomcat上部署项目时,默认情况下WEB-IN/classesclasspath Simplest solution is to move config.properties to WEB-IN/classes . 最简单的解决方案是将config.properties移至WEB-IN/classes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 读取属性文件时 getResourceAsStream 返回 null - getResourceAsStream returns null when reading properties file getResourceAsStream 为现有文件返回 null - getResourceAsStream returns null for an existing file GetResourceAsStream返回null,文件存在 - GetResourceAsStream returning null, file exists 从getResourceAsStream读取文件省略换行符 - reading file from getResourceAsStream omits newline getResourceAsStream() 返回 null。 未加载属性文件 - getResourceAsStream() is returning null. Properties file is not loading getResourceAsStream()仅在jar文件中返回null - getResourceAsStream() returns null only when in jar file getResourceAsStream返回null,尽管被调用的文件与getResourceAsStream类在同一目录中被调用 - getResourceAsStream returning null despite called file being in same dir as class getResourceAsStream is called in 使用getResourceAsStream()方法从远程位置读取属性文件 - Reading a property file from a remote location using getResourceAsStream() method 为什么getResourceAsStream()和使用FileInputStream读取文件会返回不同长度的数组? - Why does getResourceAsStream() and reading file with FileInputStream return arrays of different length? getClassLoader()。getResourceAsStream方法为具有“.LIB”扩展名的文件返回null - getClassLoader().getResourceAsStream method returns null for a file with “.LIB” extension
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM