Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a program that reads numbers from a .txt file. My problem is where to place this file, or how to reference it with a relative path to make this file accessible without using the absolute path.

like image 236
gangwerz Avatar asked Jun 10 '15 19:06

gangwerz


People also ask

How do I find the relative path in IntelliJ?

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.

How do I copy a relative path in IntelliJ?

Copy paths Press Ctrl+Shift+C to copy the absolute path to the current file. Choose Edit | Copy Path/Reference from the main menu. In the popup that opens, choose what part of the path you want to copy — filename, absolute, or relative path.


1 Answers

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.

like image 108
Fran Avatar answered Nov 12 '22 22:11

Fran