简体   繁体   中英

jsp web application relative path

I want to open a file in Java Class in an AWS Java Web project via Eclipse.

I have my file in a folder called "res" in

I tried this

BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("res\\txtFile.txt"), "UTF-8"));

but not working!

I got

java.io.FileNotFoundException: res\\txtFile.txt (The system cannot find the path specified)

If the file is inside the weapp, you want ServletContext.getResourceAsStream or Class.getResourceAsStream. If it is somewhere else on the filesystem you should probably use an absolute path. A relative path like you used is resolved relative to the directory your appserver started from which might not be what you want.

I solved it !!

used this code to get the absolute path of project anywhere

  String AbsolutePath = new File("").getAbsolutePath();

then add the relative path you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM