1

I'm new to eclipse and facing a small problem with loading a file. Basically what i want to do is load an xml file present in the same src folder in a java file that is also in the same folder.

file structure is like this project > src > example.xml, program.java

I want to load example.xml in program.java but whatever i do it keeps on saying wrong path specified. My code is

File xml = new File("/Project/src/exmaple.xml");

But its not working at all, can anyone help please?

EDIT: Thanks for the quick responses guys i solved it with the help from @Exupery and have also answered my won question so that its helpful for others too!

user9517536248
  • 355
  • 5
  • 24

3 Answers3

1

How stupid of me i just figured it out myself... so the corrected code is

File xml = new File("src/example.xml");

Thanks to @Exupery!

user9517536248
  • 355
  • 5
  • 24
0

If your path starts with a / then it is assumed that the path is absolute. Is the file located at /Project/src/exmaple.xml? If not, you need to provide the correct, absolute path or a relative path.

When running Eclipse projects, the relative path is usually starts in the root catalogue of your project, usually containing the src and target directories.

0

InputStream inputStream = YourClass.class.getResourceAsStream("image.jpg");

from This post

Community
  • 1
  • 1
onesixtyfourth
  • 744
  • 9
  • 30