This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Servlets and the fly likes Loading file from classes directory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Loading file from classes directory" Watch "Loading file from classes directory" New topic
Author

Loading file from classes directory

Kalai Selvan
Ranch Hand

Joined: Jul 07, 2004
Posts: 79
Hi,
I am having a helper class which is used by servlet. Now i am trying to load a xml file in my applications WEB-INF/classes dir by only giving the file name (since it is directly under classes folder). but it is referring to c:/documents and settings/userhome dir. can any one tell me how to get the xml file from classes directory.


Thanks and Regards,
Kalai.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

You can use ServletContext.getResource() or getResourceAsStream().
Give it a path relative to the root of your webapp.
IE:
getServletContext().getResourceAsStream("/WEB-INF/classes/myFile.xml");

http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletContext.html#getResourceAsStream(java.lang.String)


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Loading file from classes directory
 
Similar Threads
Problem in moving file
Populating xml from file system
how to read a file from an Action class?
log4j & log4j.xml
Reading a xml file