Java:无法加载属性文件。为什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10040265/
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
Java: Can not load a properties file. Why?
提问by Jim
I am trying to load a properties file.
I have the properties file in the same directoryas the class that I am trying to load it.
Example:
我正在尝试加载属性文件。
我的属性文件与我尝试加载它的类位于同一目录中。
例子:
package com.classes.internal;
public class ClassA {
private static String PFILE = "config.properties";
private static void methodA(){
//do stuff
Properties properties = null;
try{
properties = new Properties();
properties.load(new FileInputStream(PFILE));
//properties.load(new ClassA().getClass().getResourceAsStream(PFILE)); --> Does not work either
}catch(Exception e){
e.printStackTrace();
}
}
Also config.properties
file is in com\classes\internal dir
另外config.properties
文件在COM \类\内部DIR
But I get a FileNotFoundException
or java.lang.NullPointerException
(if using the commented out line instead of the first)
但是我得到一个FileNotFoundException
或java.lang.NullPointerException
(如果使用注释掉的行而不是第一个)
What am I doing wrong here? What am I missunderstanding?
我在这里做错了什么?我误解了什么?
回答by FrankieTheKneeMan
The file needs to be in the directory you're executing from, not the directory where the class file is.
该文件需要位于您正在执行的目录中,而不是类文件所在的目录中。
So if you have the directory structure
所以如果你有目录结构
Project/com/classes/internal
项目/com/classes/内部
and you run the command
然后你运行命令
Project$ java com.classes.internal.SomeClass
Your JVM will look for the "config.properties" file in the "Project" directory.
您的 JVM 将在“项目”目录中查找“config.properties”文件。
回答by ice
I think your program work directory not in com\classes\internal. Try to pass this relative path:
我认为您的程序工作目录不在 com\classes\internal 中。尝试传递这个相对路径:
com\classes\internal\config.properties
or absolute path. You can obtain current working directory like this
或绝对路径。您可以像这样获取当前工作目录
String currentDir = new File(".").getAbsolutePath();
回答by Andrew Thompson
properties.load(this.getClass().getResourceAsStream(
"/com/classes/internal/" + PFILE));
回答by ThiamTeck
The line below will load file relative to working directory (in FrankieTheKneeMan example, the "Project" folder. If you are running your code in Tomcat, it is quite likely from $Tomcat/bin/):
下面的行将加载相对于工作目录的文件(在 FrankieTheKneeMan 示例中,“Project”文件夹。如果您在 Tomcat 中运行您的代码,它很可能来自 $Tomcat/bin/):
properties.load(new FileInputStream(PFILE));
And for the line below, it is loading file relative to your classpath. So "config.properties" refer to directory that contain your "com" folder, instead of "com/classes/internal/".
对于下面的行,它正在加载相对于您的类路径的文件。所以“config.properties”指的是包含“com”文件夹的目录,而不是“com/classes/internal/”。
properties.load(new ClassA().getClass().getResourceAsStream(PFILE));
So you need to decide on whether to load from classpath, or from working directory.
所以你需要决定是从类路径加载,还是从工作目录加载。
回答by viktor
Example. I'm using eclipse project structure, I hope you get the picture anyway:
例子。我正在使用 eclipse 项目结构,无论如何我希望你能得到图片:
//File in Project/src/com/classes/internal/config.properties
InputStream in = ClassA.class.getResourceAsStream("config.properties");
Properties p = new Properties();
try {
p.load(in);
} catch(IOException e) {
e.printStackTrace();
}
In case you decide to put your file outside package:
如果您决定将文件放在包外:
//File in Project/src/config.properties
InputStream in = ClassA.class.getClassLoader().getResourceAsStream("config.properties");
Note that getResourceAsStream
file has to be included in your classpath.
请注意,getResourceAsStream
文件必须包含在您的类路径中。
回答by Ajay Kelkar
Create a configpackage in the source folder directly and place the config.propertiesfile in it. On building the application, it will go to /WEB-INF/classes/config/config.properties
直接在源文件夹中创建一个配置包,并将config.properties文件放入其中。在构建应用程序时,它将转到 /WEB-INF/classes/config/config.properties
You can then use the following code to access the properties file -
然后您可以使用以下代码访问属性文件 -
Properties prop = new Properties();
String propFileName = "/config/config.properties";
InputStream input = null;
input = ClassA.class.getResourceAsStream(propFileName);
prop.load(input);