英文:
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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论