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

11 comments
Comment actions Permalink

Plugin's resources can be accessed via:

getClass().getResource(String name)

or

getClass().getResourceAsStream(String name)
0
Comment actions Permalink

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"

0
Comment actions Permalink

Since your file is located in the files directory, your path should look like /files/MyFile2.txt

0
Comment actions Permalink
System.out.println(getClass().getResource("html/codesharecatalouge.html"));
System.out.println(getClass().getResource("/html/codesharecatalouge.html")) 
 Still it is giving null
0
Comment actions Permalink

Can you provide a project with the minimal reproducible example?

0
Comment actions Permalink
package com.dbs.codeshare.actions;


import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;

import org.jetbrains.annotations.NotNull;



public class CodeShareAssetsAction extends AnAction {
@Override
public void actionPerformed(@NotNull AnActionEvent anActionEvent) {
System.out.println("codeshare");

System.out.println(getClass().getResource("codesharecatalouge.html"));
System.out.println(getClass().getResource("html/codesharecatalouge.html"));
System.out.println(getClass().getResource("/html/codesharecatalouge.html"));



}
}

The above package is located at project-> src
I have a file in project->resouces/html/codesharecatalouge.html

0
Comment actions Permalink

Can you provide the whole example project as a repository so I can verify it? 

0
Comment actions Permalink

Hi Jakub, I cannot send the project as repository, but i can create new project and upload it in zip format

0
Comment actions Permalink

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 in the src I have created the following class

package com.extract.files;


import com.intellij.openapi.actionSystem.AnAction;

import com.intellij.openapi.actionSystem.AnActionEvent;

public class ExtractFiles extends AnAction{


    @Override

    public void actionPerformed(AnActionEvent anActionEvent) {

        System.out.println(getClass().getResource("MyFile.txt"));

        System.out.println(getClass().getResource("files/MyFile.txt"));

        System.out.println(getClass().getResource("/files/MyFile.txt"));

    }

}


Then added action in plugin.xml which is located at project->resources->META_INF->plugin.xml

<idea-plugin>

  <id>com.your.company.unique.plugin.id</id>

  <name>Plugin display name here</name>

  <version>1.0</version>

  <vendor email="support@yourcompany.com" url="http://www.yourcompany.com">YourCompany</vendor>




  <description><![CDATA[

      Enter short description for your plugin here.<br>

      <em>most HTML tags may be used</em>

    ]]></description>




  <change-notes><![CDATA[

      Add change notes here.<br>

      <em>most HTML tags may be used</em>

    ]]>

  </change-notes>




  <!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/build_number_ranges.html for description -->

  <idea-version since-build="173.0"/>




  <!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/plugin_compatibility.html

       on how to target different products -->

  <!-- uncomment to enable plugin in all products

  <depends>com.intellij.modules.lang</depends>

  -->




  <extensions defaultExtensionNs="com.intellij">

    <!-- Add your extensions here -->

  </extensions>




  <actions>

    <!-- Add your actions here -->

    <group id="com.extract.files.MainMenu" description="Extracting" text="Extract Files">

      <add-to-group group-id="MainMenu" anchor="last"/>

      <action class="com.extract.files.ExtractFiles" id="com.extract.files.ExtractFiles.Action" text="Extract"/>

    </group>

  </actions>




</idea-plugin>

Then I have added a file to resources/files/MyFile.txt

 

0
Comment actions Permalink

After creating the following file:

/resources/files/MyFile.txt

your action prints the following output in the third case (/files/MyFile.txt):

file:/Users/hsz/Library/Caches/JetBrains/IntelliJIdea2020.1/plugins-sandbox/plugins/DevKitProejct/classes/files/MyFile.txt
0
Comment actions Permalink

Thank you jacob
I was able to do now

0

Please sign in to leave a comment.