java - file.getAbsolutePath 如何工作?

标签 java

这是代码:

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/

相关文章:

java - 两个相同或不同分隔符之间的子字符串(当分隔符多次出现时)

java - 使用缺少字段的 jackson 反序列化json

java - 如何循环数据偷偷使用调用中的数据

java - 类是否必须位于同一继承树上才能具有 Has-A 关系

java - 将子类存储在父类(super class)数组中并使用子类的方法(Java)

java - 是否有一种方法可以创建名称等于变量当前状态的对象

java - java如何区分字符是全角还是半角?

java - 这个 while(true) 循环如何退出?

java - 我应该在哪里放置构造函数的公共(public)代码?

java - 序列化 ArrayList<object> 二进制文件的数据结构