gpt4 book ai didi

java - file.getAbsolutePath 如何工作?

转载 作者:行者123 更新时间:2023-12-01 10:26:03 26 4
gpt4 key购买 nike

这是代码:

String filename = "sql.txt";
File file = new File(filename);
String path = file.getAbsolutePath();
System.out.println(path);

我的文本文件位于 E 盘,但当我打印出路径时,它位于 C 盘。为什么会出现这种情况?

最佳答案

您提供的路径既不是绝对的也不是规范的。

String filename = "sql.txt";

因此,路径名仅作为当前用户目录返回。

查看 getAbsolutePath() 的文档File中的方法类:

If this abstract pathname is already absolute, then the pathname string is simply returned as if by the getPath() method. If this abstract pathname is the empty abstract pathname then the pathname string of the current user directory, which is named by the system property user.dir, is returned. Otherwise this pathname is resolved in a system-dependent way. On UNIX systems, a relative pathname is made absolute by resolving it against the current user directory. On Microsoft Windows systems, a relative pathname is made absolute by resolving it against the current directory of the drive named by the pathname, if any; if not, it is resolved against the current user directory.

关于java - file.getAbsolutePath 如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35356512/

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