在哪里放置一个.txt文件,以便一个java servlet可以在运行时读取它 [英] Where to place a .txt file so that a java servlet can read it during runtime
问题描述
谢谢 解决方案
在类路径(在那里你的Java代码是)或者将它的路径添加到类路径。
InputStream input = Thread.currentThread ).getContextClassLoader()的getResourceAsStream( file.txt的)。
或者,把它放在公共webcontent(也就是JSP文件所在的位置)。
InputStream input = getServletContext()。getResourceAsStream(file.txt);
至少不要使用 java.io.File
有相对路径,因为这将取决于当前的工作目录,它与启动应用程序的方式有所不同。
另见:
I am using eclipse to make a dynamic webpage. Its a simple program that relies on servlets to pass/retrieve data. One servlet of mine has to open a .txt file to read its content and send it to the client. However I get a FileNotFound Exception. I know its because I don't know how/where to place the txt file so that servlet can find that file at runtime. I am working on eclipse. Can you please provide some hints?
Thanks
Put it in the classpath (there where your Java code is) or add its path to the classpath.
InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream("file.txt");
Or, put it in public webcontent (there where your JSP files also are).
InputStream input = getServletContext().getResourceAsStream("file.txt");
You should at least not use java.io.File
with relative paths since that would be dependent on the current working directory which differs on the way how you start the application.
See also:
这篇关于在哪里放置一个.txt文件,以便一个java servlet可以在运行时读取它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!