How to write a path to go one level up and then down to another directory

I am trying to open a file in a Java program (say Program.java ) using a relative path.

I have two directories:

  ProjectWork\Business\Scenarios\SC01.txt ProjectWork\SourceCode\Program.java 

Now, from Program.java , I want to write a relative path to access SC01.txt :

  String path = // <-- not sure how to write the path File scenario = new File (path); 

The path should be one level to the ProjectWork directory, and then go to Scenarios\SC01.txt .

+7
source share
2 answers

From what you are saying, you should set the path to:

 ../Business/Scenarios/SC01.txt 

../ rise one level the rest is the relative path to ProjectWork

In a Java file, when you use a relative path without another argument, the file is mapped to the System user.dir property, which corresponds to the working directory.

+16
source
 String path="firstpath" +File.separator +".." +File.separator +"secondpath"; 
+2
source

All Articles