Reference a Text File with relative path in Java with the IntelliJ IDEA

You need to start your path from src. "src/your/path/file.txt". See my answer here


If you have a multi-project setup, it may not be obvious what the "root" directory is for a relative path. In that case, open the Terminal tab in your IntelliJ and see what directory it lands in. That is the "root" directory. Your relative path should start there.


When you try to open a file, it takes your current working path. For example this working tree:

Project
|->src
|   |-->MyClass.java
|   |-->MyFile1.txt
|->res
   |->files
     |-->MyFile2.txt

You can use

new File("MyFile1.txt");

for MyFile1, or

new File("./res/files/MyFile2.txt");

for MyFile2.