How to read a file by relative path
Answered
I want to read a file using relative path
Project
|->src
| |-->MyClass.java
| |-->MyFile1.txt
|->res
|->files
|-->MyFile2.txt
I have tried java.io new File("./res/files/MyFile2.txt), But this is not working in plugin project
Could you please suggest the code to read a file by relative path
Please sign in to leave a comment.
Plugin's resources can be accessed via:
or
Hi Jakub
Tried both
getClass().getResource("MyFile2.txt")
getClass().getResourceAsStream("MyFile2.txt") but getting null value
Tried by keeping name as "/res/files/MyFile2.txt"
Since your file is located in the files directory, your path should look like /files/MyFile2.txt
Can you provide a project with the minimal reproducible example?
The above package is located at project-> src
I have a file in project->resouces/html/codesharecatalouge.html
Can you provide the whole example project as a repository so I can verify it?
Hi Jakub, I cannot send the project as repository, but i can create new project and upload it in zip format
Hi Jakub,
Sorry, By any luck I cannot upload zip too
I am sharing the steps and code which i have done please try to verify
I have created a project as new->Project-> Intellij Platform Plugin
Then added action in plugin.xml which is located at project->resources->META_INF->plugin.xml
After creating the following file:
your action prints the following output in the third case (/files/MyFile.txt):
Thank you jacob
I was able to do now
For anyone experiencing the same problem. I solved it by using the using the Classloader of the thirdparty class containing the code above and setting it on the current thread. Something like the following: