Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 获得相对速度,然后潜得更深_Java - Fatal编程技术网

Java 获得相对速度,然后潜得更深

Java 获得相对速度,然后潜得更深,java,Java,我有一个包结构,比如 A |_B |_C |_D |_"myFile.txt" myMain.java 大体上,我想做一些类似的事情 Path currentRelativePath = Paths.get(""); 它给出了Main()所在的当前相对路径。类似于/home/myprojects/project1/src/的内容,我如何修改上面的内容,以获得一直到myFile.txt级别的相对路径 我尝试过以下方法: Path currentRelativePath = Pat

我有一个包结构,比如

A
|_B
|_C
  |_D
    |_"myFile.txt"
myMain.java
大体上,我想做一些类似的事情

Path currentRelativePath = Paths.get("");
它给出了Main()所在的当前相对路径。类似于/home/myprojects/project1/src/的内容,我如何修改上面的内容,以获得一直到myFile.txt级别的相对路径

我尝试过以下方法:

 Path currentRelativePath = Paths.get("" + "/B/C/D/");

但在每种情况下,它只获取“/B/C/D/”部分,而不是路径的开头

//Gets from the current working directory a path to B/C/D
Path path = Paths.get("B/C/D");
//String equal to "B/C/D"
path.toString();
//String equal to "/home/myprojects/project1/src/B/C/D"
path.toAbsolutePath().toString();
我认为你混淆了相对路径和绝对路径/home/myprojects/project1/src/是一条绝对路径,而不是像您在问题“当前相对路径”中所说的那样

 Path currentRelativePath = Paths.get("");
 Path finalPath = Paths.get(currentRelativePath.toString(),"/B/C/D/");
//Gets from the current working directory a path to B/C/D
Path path = Paths.get("B/C/D");
//String equal to "B/C/D"
path.toString();
//String equal to "/home/myprojects/project1/src/B/C/D"
path.toAbsolutePath().toString();