Matt
Matt

Reputation: 57

writing a relative path to a text file in java

I'm working on this project, and I am accessing a lot of text files that I have put in a resources folder. I'm having a lot of trouble writing the relative path to these files. In the command line, I got from the directory containing the java file with a main method to the text file I want with "cd ../../../../../../../resources/Information/ConfluneceTotalLists/table.txt". I tried writing using this in the main method to get the file, but it did not work. How can I fix this? Also, when a java program is running, is the current directory the one containing the java file with the main method?

Upvotes: 0

Views: 1820

Answers (3)

Qnan
Qnan

Reputation: 3744

cd is a unix shell command, in java it is sufficient to specify the full path (relative or absolute) when you open the file. As for the current directory, it is usually the one you run the program from. In case you're using an IDE, it would usually be the main project folder.

Upvotes: 0

bmargulies
bmargulies

Reputation: 100196

When a java program runs, it inherits the working directory of the program that launched it. This has nothing to do with the Java source file or class file of the main class.

Upvotes: 0

Ina
Ina

Reputation: 4470

Use:

System.out.println(System.getProperty("user.dir")));

To find out what the directory your Java program is running from. Then use relative paths from this location.

Upvotes: 1

Related Questions