I need to read text file from the classpath in Java WAR application. How can I read it as InputStream. File is located in /WEB-INF/classes/ folder, but when I use following code, it just returns null.
InputStream input = servletContext.getClass().getClassLoader().getResourceAsStream("my_filename.txt");
Prefix it with a forward slash to denote the root of the classpath:
getResourceAsStream("/my_filename.txt")
Alternatively, you can use the serlvetContext.getResourceAsStream(..)
which looks for resources relative to the context root. So classes would be /WEB-INF/classes
.
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.