简体   繁体   English

在Web应用程序中找不到属性文件的路径?

[英]Path not found for a property file in Web application?

My Property file location is WebRoot/WEB-INF/Test/property files/Question.properties 我的属性文件位置是WebRoot/WEB-INF/Test/property files/Question.properties

Now I want to load the property file using FileInputStream. 现在我想使用FileInputStream加载属性文件。 So I wrote the below code. 所以我写了下面的代码。

Properties pr = new Properties();
pr.load(new FileInputStream("WebRoot/WEB-INF/Test/property files/Question.properties"));

By the above code I got java.io.FileNotFoundException .I am using myeclipse for my development. 通过上面的代码,我得到了java.io.FileNotFoundException 。我正在使用myeclipse进行开发。 Can any one suggest the path to read my properties. 任何人都可以建议阅读我的属性的路径。

You are opening it relative to the current directory. 您将相对于当前目录打开它。 Do you know what the current directory is? 你知道当前目录是什么吗? Try creating a: 尝试创建一个:

File f = new File("WebRoot/WEB-INF/Test/property files/Question.properties");

Then printing or debug the absolute path of the File object. 然后打印或调试File对象的绝对路径。 This will tell you what file you are actually trying to open (ie full path). 这将告诉您实际尝试打开的文件(即完整路径)。

However, if you are trying to open a 'resource' bundled with your web app (as suggested by /WEB-INF/ being in the path) this is probably not a good way to do it. 但是,如果您尝试打开与您的Web应用程序捆绑在一起的“资源”(如/ WEB-INF /在路径中所示),这可能不是一个好方法。 One alternative is to build your 'resource' into one of your application's .jar files. 另一种方法是将“资源”构建到应用程序的.jar文件中。

See here, for a related answer: Refer to a web page inside a jar file 请参阅此处,获取相关答案: 请参阅jar文件中的网页

use the method InputStream java.lang.Class.getResourceAsStream(String name) instead, this will work when you run your class within eclipse and outside eclipse. 使用方法InputStream java.lang.Class.getResourceAsStream(String name),这将在你在eclipse和eclipse之外运行你的类时工作。 and use the same path as what you have mentioned in your code and append "/" at the front. 并使用与代码中提到的相同的路径,并在前面添加“/”。 Hope this helps ! 希望这可以帮助 !

Do one thing right click on Question.properties file click on the properties get the Location of the file. 做一件事右键单击Question.properties文件,单击属性获取文件的位置。

 Properties pr = new Properties();
 pr.load(new FileInputStream("Location of the file"));

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM