If it is placed in the webapp class path, just use:
InputStream input = servletContext.getResourceAsStream("file.txt");
If it is placed in the global classpath, use:
InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream("file.txt");
If it is placed in webcontent just use:
InputStream input = new FileInputStream(servletContext.getRealPath("file.txt"));
The examples assume that they are placed in the root. Of course, you can use a relative path rather than the root root or webcontent root, for example. path/to/file.txt . You can get ServletContext in Struts at ServletActionContext#getServletContext() .
Edit: You edited your question as follows:
EDIT: Thanks guys for the answer. For me this worked:
String path=GetPointsOfInterestAction.class.getResource("../../../resources/visitor_attractions.txt")
Could you explain why
This is actually not the βrightβ way, but it is also doable. You only need to make sure that you know the relative path of the file, not the actual path of the GetPointsOfInterestAction class. Of course, this class is located in another package, so you basically need to turn the directory back (as in regular disk file systems: cd ../ , etc.). Again, this is not the most elegant way. You must use one of the first two of the above methods, with the resource name resources/visitor_attractions.txt .
Balusc
source share