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()
vsFileInputStream
精彩评论