如何在 Eclipse Java 动态 Web 项目中使用 .properties 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19641962/
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
How to use a .properties file in Eclipse Java Dynamic Web Project?
提问by Dilma
I'm developing a Dynamic Web Project in Eclipse. I created a .properties file for store database details (Username, Password etc.). I added it by right clicking on the project and New -> File . I used the Java util package Properties class. But it does not working. I can not retrieve any property from the file. Here is the code I used,
我正在 Eclipse 中开发一个动态 Web 项目。我为商店数据库的详细信息(用户名、密码等)创建了一个 .properties 文件。我通过右键单击项目和 New -> File 来添加它。我使用了 Java util 包 Properties 类。但它不起作用。我无法从文件中检索任何属性。这是我使用的代码,
Properties prop = new Properties();
try {
prop.load(new FileInputStream("database.properties"));
String db = prop.getProperty("database");
String userName = prop.getProperty("dbuser");
String password = prop.getProperty("dbpassword");
} catch (IOException ex) {
ex.printStackTrace();
}
Is there something wrong or Is there any particular place where I should put properties file.
有什么问题吗,或者我应该把属性文件放在什么特定的地方。
采纳答案by user2821894
What you did is correct, ie right clicking the project and new--file.You have to Put your properties where you start your jvm from. Please look into the attached image. The properties file is marked in red. Look if your properties file is also located something like this. Also add this in your code to find out where to put your file:
你所做的是正确的,即右键单击项目和新文件。你必须把你的属性放在你开始你的 jvm 的地方。请查看所附图片。属性文件被标记为红色。看看你的属性文件是否也位于这样的位置。还要在您的代码中添加它以找出放置文件的位置:
System.out.println(new File(".").getAbsolutePath());
For more details please follow this link- FileNotFoundException when using java properties file
有关更多详细信息,请访问此链接-使用 java 属性文件时的 FileNotFoundException
回答by Ashok Gudise
Try to give absolute path or relative path to the proprty file, also check this propery file path has been add to source folders or not, if not it will not be copied to your classes folder. (Right cclick on project , check java build path under source tab.
尝试给出属性文件的绝对路径或相对路径,同时检查此属性文件路径是否已添加到源文件夹中,否则不会将其复制到您的类文件夹中。(右键单击项目,检查源选项卡下的 java 构建路径。
回答by nitind
You should instead be using the more portable java.util.Properties#load(InputStream)
with the result of javax.servlet.ServletContext#getResourceAsStream(String)
.
相反,您应该使用更便携java.util.Properties#load(InputStream)
的javax.servlet.ServletContext#getResourceAsStream(String)
.
回答by Dario
You should have .propertiesfile in same package as class that is using it.
您应该在与使用它的类相同的包中拥有.properties文件。
Or better, read properties file with getResourceAsStream
method (otherwise you can have some problem later when you'll have file in .war archive).
或者更好的是,使用getResourceAsStream
方法读取属性文件(否则,当您在 .war 存档中拥有文件时,您可能会遇到一些问题)。
InputStream inputStream =
getClass().getClassLoader().getResourceAsStream("database.properties");
回答by MadConan
Normally, you make sure the properties file is in the project runtime classpath (e.g. WEB-INF/classes) and then load it using either the System classloader or the property file handler's classloader, i.e. (Freehand typing from memory -- NOT COMPILED)
通常,您确保属性文件位于项目运行时类路径(例如 WEB-INF/classes)中,然后使用系统类加载器或属性文件处理程序的类加载器加载它,即(从内存中徒手输入 - 未编译)
try{
Properties p = new Properties();
InputStream in = MyPropertyHandler.getClass()
.getClassLoader()
.getResourceAsStream("com/package/props/database.properties");
p.load(in);
catch(IOException e){
e.printStackTrace(System.err);
}
I'm betting you aren't pointing at the correct location. Make sure you're properties file is in the correct place. Using that code, I believe it is looking for ${CURRENT_WORKING_DIR}/database.properties
, which is the case of a web app in eclipse is WEB-INF/classes
(i think).
我敢打赌你没有指向正确的位置。确保您的属性文件位于正确的位置。使用该代码,我相信它正在寻找${CURRENT_WORKING_DIR}/database.properties
Eclipse 中的网络应用程序的情况WEB-INF/classes
(我认为)。