相对文件路径问题

问题描述:

我正在开发一个试图读取配置文件的portlet.我正在一个日食项目中开发它.我目前将配置文件放在WEB-INF文件夹(位于root/WEB-INF/中)中,其名为config.properties.如何在Java源代码中使用相对路径访问此文件? (位于root/src/package/mysource.java中)

I am developing a portlet that is trying to read in a config file. I am developing it in an eclipse project. I currently have the config file placed inside my WEB-INF folder (which is in root/WEB-INF/), and its called config.properties. How can I access this file using relative path in my java source code? (which is in root/src/package/mysource.java)

例如,

File myfile = new File("WHAT DO I PUT HERE/config.properties");

您能提供的任何帮助都会很棒!

Any help you can provide would be great!

我将使用servlet上下文及其getResourceAsStream()方法而不是File来加载该代码.像 this ....

I would load that using the servlet context and its getResourceAsStream() method instead of a File. Like this....

对不起,您说过portlet.您需要 javax.portlet.PortletContext 然后.

Sorry, you said portlet. You'll need javax.portlet.PortletContext then.

您可以从 PortletSession .您可以从 PortletRequest 创建PortletSession

You can get the PortletContext from the PortletSession. You can create a PortletSession from a PortletRequest.