开发者

Where to place a .txt file so that a java servlet can read it during runtime [duplicate]

开发者 https://www.devze.com 2023-01-29 17:46 出处:网络
This question already has answers here: Where to place and how to read configuration resource files in servlet based application?
This question already has answers here: Where to place and how to read configuration resource files in servlet based application? (6 answers) Closed 6 years ago.

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开发者_StackOverflow 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:

  • getResourceAsStream() vs FileInputStream
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号