Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

path

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

I have two directories as follows:

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 has to be such that I go one level up to the ProjectWork directory and then navigate to Scenarios\SC01.txt.

like image 217
CodeBlue Avatar asked Apr 03 '12 19:04

CodeBlue


2 Answers

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

../Business/Scenarios/SC01.txt

../ to go up one level then the rest is the relative path against ProjectWork

In Java file when you use a relative path without another argument, the file is matched against the System property user.dir which matches the working directory.

like image 167
Guillaume Polet Avatar answered Sep 19 '22 02:09

Guillaume Polet


String path="firstpath" +File.separator +".." +File.separator +"secondpath";
like image 29
Saurabh Avatar answered Sep 21 '22 02:09

Saurabh