创建文件后,路径上多了’./’。

huangapple go评论74阅读模式
英文:

After created a file it got extra './ ' on path

问题

这是翻译好的部分:

我创建了一个空的“File”并从一个jar文件中提取了值/内容。这个jar文件在Linux上运行。

String filename = "base_script";
File targetFile = new File(filename + ".sh");
String pathStr = null;

// 空文件
targetFile.createNewFile();

if (targetFile.exists()) {
    InputStream link = (getClass().getResourceAsStream(this.userScriptPath));
    Files.copy(link,
               targetFile.getAbsoluteFile().toPath(),
               java.nio.file.StandardCopyOption.REPLACE_EXISTING);
    pathStr = targetFile.getAbsolutePath();
}

这是文件路径:“./base_script.sh”

这是文件的绝对路径:“apps/MyApps/./base_script.sh”

我的问题是为什么绝对路径上有额外的“./”?

英文:

I created an empty File and store the extracted value/content from a jar. The jar is running on linux.

String filename ="base_script";
File targetFile = new File( filename + ".sh");
String pathStr=null;

//empty file 
targetFile.createNewFile();

if(targetFile.exists()) {
		InputStream link = (getClass().getResourceAsStream(this.userScriptPath));
		Files.copy(link,
				   targetFile.getAbsoluteFile().toPath(),
				   java.nio.file.StandardCopyOption.REPLACE_EXISTING);
		pathStr = targetFile.getAbsolutePath();
}

This is the file path ./base_script.sh

And this is file absolute path apps/MyApps/./base_script.sh

My question is why there's an extra ./ on the absolute path?

答案1

得分: 0

这部分内容已翻译如下:

"不清楚你为什么在名称中使用了“./”,因为你在定义值时没有使用它。无论如何,这将解析路径的真实名称,不包含任何路径中的“./”或“..”:

pathStr = targetFile.toPath().toRealPath().toString()"
英文:

It's not clear why you have "./" in the name as you define the value without it. Anyway this will resolve the path to the real name without any "./" or ".." in a path:

pathStr = targetFile.toPath().toRealPath().toString()

huangapple
  • 本文由 发表于 2020年8月7日 15:07:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63296868.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定