What is the path to files in root resources folder
I have a folder at the same level as "src" called "res", which I have marked as my resources root. In it, I have folders "data" and "graphics". The data folder contains a single file, "data.xlsx", and the graphics folder contains a single file, "sewers.png".
At one part of one of class files, I have the code:
dataFile = new XSSFWorkbook(new FileInputStream("/data/data.xlsx"));
This generates a filenotfoundexception. However, when I spell out the complete path (i.e. starting with "c:") it works and loads the file correctly. I was basing the above code on a youtube video where the person was doing a similar process in Eclipse. He had a different process to mark the resources root folder, but Java is Java so I figured the code to get at files in the resources root would be the same.
So I guess I'm not using the resources root correctly. Once I have a file in the resources root folder (or a folder inside of it) how do I get a path to that file?
Please sign in to leave a comment.
Once you have marked a folder as resource folder in IntelliJ, it is available in your code as via resource loader.
I think I'm more confused now, haha. Maybe my root folder is in the wrong place? Should it be inside the package that the project is in? Again, I was trying to interpret directions that were written using Eclipse, so this part might not be applicable.
This is my current project structure:
I am trying to load the file in res/data/data.xlsx from src/com.technetium.phobosproject/engine/Game.java
I tried following some of the examples on the link you gave but I'm not sure I understand it:
System.out.println(this.getClass().getResource("/data/data.xlsx"));This printed "null", which means it did not find the file.
Oh, one other thing... I checked the out folder, and I think it's doing it right:
The file data.xlsx is found in the graphics folder there.
When trying to use:
dataFile = new XSSFWorkbook(String.valueOf(getClass().getResource("/data/data.xlsx")));I get this among the error message:
Caused by: java.io.FileNotFoundException: file:\C:\Users\t74bd\IdeaProjects\Phobos%20Project\out\production\Phobos%20Project\data\data.xlsx (The filename, directory name, or volume label syntax is incorrect)
Here it is with full path to file shown: