Reputation: 364
I wanna use relative path start from my src folder to get file from resource folder.
File myFile = new File("/src/main/resources/filefolder/file.xml");
When I try this:
File myFile = new File("../src/main/resources/filefolder/file.xml");
Or this:
File myFile = new File("../../../../src/main/resources/filefolder/file.xml");
I get:
java.io.FileNotFoundException: /srv/../src/main/resources/filefolder/file.xml(No such file or directory)
My file structure:
-src
-main
-java
-com.my.site
-task
-MyClass.java
-resources
Upvotes: 3
Views: 20032
Reputation: 10652
Just a little explanation... What you want to do is get a resource from the resource folder, that's a quite typical task.
What you do have to realize is, that the whole /src/main/etc. structure is there to structure your source code, in other words, the .java files (and resource files). In your compiled program, this structure will not longer exist in the same way, so relying on a src
directory is a very, very bad idea.
This structure is there for maven:
src/main
-> Everything that should be part of the "program" all the time
src/test
-> Everything that should only be part of the "program" when running automated tests
src/main/java
-> .java files that need to be there always
src/main/resources
-> Resources (images, templates, etc.) that need to be there always
src/test/java
-> .java files that are only needed during automated tests (for example, the automated tests themselves)
src/test/resources
-> Resources that are only used in automated tests, for example, files to compare results against, etc.
Where does this lead us? It leads to the simple fact, that, in the end, the whole src/main and src/test will simply not exist and indeed, java and resources will be added together (you can check this by looking at a .jar file - it can be opened with any Zip-Program). This is, why @LazerBanana is entirely correct: You do not try to navigate directories, you ask for resources and the resources are inside your classpath - and it does not matter, if they are in the resources or java folder (because that's just to keep your source code structured). In your class path, both folders come together.
Upvotes: 2
Reputation: 7211
I see you are asking about the relative path but seems like you want to just read the file from resources, I believe the most common way to read resources is by using the class loader, example:
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("filefolder/file.xml").getFile());
Upvotes: 5
Reputation: 2583
your path is correct, just add dot before src, to indicate that you are searching relative path to project root directory, in other case you'll search by absolute path
File myFile = new File("./src/resources/filefolder/file.xml");
if doesn't work - try this:
File myFile = new File("src/resources/filefolder/file.xml");
anyway you can run this to check what is your root dir:
public static void main(String[] args) {
System.out.println(new File("").getAbsolutePath());
}
Upvotes: 6