1

I have built a dynamic web project MusicStore in Eclipse, and I want to access an xml file located in WebContent/_res/Songs.xml. The method I used to access it in a regular Java class[not a servlet] is:

URL url = this.getClass().getClassLoader().getResource("/");
String filePath = url.getFile();
String webAppLoc=new File(filePath).getParentFile().getParent();
String resLoc=webAppLoc+"/_res/Songs.xml";

It seems to me that this is very cumbersome. Is there a better, more efficient way? thanks!

Will
  • 539
  • 2
  • 8
  • 18

1 Answers1

0

Here you can try this to access file

public class Test {
    public static void main(String[] args){
        //This gives me path for the  current working directory.
        String fileName = System.getProperty("user.dir");

        //This gives me path for the file that is residing in folder called "xml_tutorial".
        File file = new File(fileName + "../../xml_tutorial/sample.xlsx" );
        System.out.println(file.getCanonicalPath());

 }
}
Will
  • 539
  • 2
  • 8
  • 18
Sheel
  • 847
  • 2
  • 8
  • 20
  • sorry, this method doesn't work. I am in an environment of dynamic webapp, the address I need is something like:`/Users/username/CodeProjects/Eclipse/MAC_EE/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/MusicStore` your method returns `/Applications/EclipseCollection/eclipse EE/Eclipse.app/Contents/MacOS` – Will Aug 04 '13 at 10:39