问题描述
我正在尝试使用相对路径在Java
程序(例如Program.java
)中打开文件.
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
现在,从Program.java
,我想编写一个访问SC01.txt
的相对路径:
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);
路径必须使我上一层到ProjectWork
目录,然后导航到Scenarios\SC01.txt
.
The path has to be such that I go one level up to the ProjectWork
directory and then navigate to Scenarios\SC01.txt
.
推荐答案
按照您的意思,您应该将路径设置为:
From what you are saying, you should set path to:
../Business/Scenarios/SC01.txt
../上一层然后剩下的就是针对ProjectWork的相对路径
../ to go up one levelthen the rest is the relative path against ProjectWork
在Java文件中,当您使用不带其他参数的相对路径时,该文件将与与工作目录匹配的系统属性user.dir
匹配.
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.
这篇关于如何编写路径,以上一级然后下一级进入另一个目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!