相对文件路径问题
我正在开发一个尝试读取配置文件的 portlet。我正在 eclipse 项目中开发它。我目前将配置文件放置在 WEB-INF 文件夹(位于 root/WEB-INF/ 中)中,其名称为 config.properties。如何使用 java 源代码中的相对路径访问此文件? (位于 root/src/package/mysource.java 中)
例如,
File myfile = new File("WHAT DO I PUT HERE/config.properties");
您可以提供的任何帮助都会很棒!
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)
For example,
File myfile = new File("WHAT DO I PUT HERE/config.properties");
Any help you can provide would be great!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于这是一个 portlet,您可能需要使用 PortletContext。
尽管有一个 getRealPath 方法,但我会避免使用它,因为它取决于门户应用程序的部署方式 - 不能保证资源将映射到文件系统上的文件。请改为使用
getResourceAsStream
(或者,如果必须的话,getResource
)。Since this is a portlet, you'll probably want to use the PortletContext.
Although there is a
getRealPath
method, I would avoid it as it is dependent on how the portal application is deployed - there is no guarantee that the resource is going to map to a file on the file system. Go instead forgetResourceAsStream
(or, if you must,getResource
).我将使用 servlet 上下文及其 getResourceAsStream() 方法而不是 File 来加载它。就像这个....
抱歉,你说门户组件。您需要 javax.portlet.PortletContext 那么。
您可以从 PortletSession 获取 PortletContext 。您可以从 PortletRequest 创建 PortletSession 。
I would load that using the servlet context and its getResourceAsStream() method instead of a File. Like this....
Sorry, you said portlet. You'll need javax.portlet.PortletContext then.
You can get the PortletContext from the PortletSession. You can create a PortletSession from a PortletRequest.