gpt4 book ai didi

java - 如何获取文件目录的绝对路径?

转载 作者:搜寻专家 更新时间:2023-10-30 20:59:48 24 4
gpt4 key购买 nike

如何获取包含指定文件的目录的绝对路径:

// current dir is "/home/me/dev"
File file = new File("./target/test.txt");
assert absolute(file).equals("/home/me/dev/target");

它是 Java 6。

最佳答案

你的意思是 methods in the documentation

File file = new File("./target/test.txt");
String dirPath = file.getAbsoluteFile().getParentFile().getAbsolutePath()
assert dirPath.equals("/home/me/dev/target");

关于java - 如何获取文件目录的绝对路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6116128/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com