有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java getCanonicalPath返回不同平台之间的不同路径

代码中较低的方法之一是使用file.getCanonicalPath()函数获取临时文件夹的规范路径(文件被定义为File file = new File("/tmp"))。这适用于linux和windows操作系统,但在macOS上,此函数返回以下字符串-“/private/tmp”,即使我的主目录中有一个tmp文件夹,但我没有/private目录。 你知道这个“私有”目录是从哪里来的吗?为什么这个方法在macOS中不把我指向“/tmp”,即使它是可访问的

注意:如果我创建一个随机的、不存在的dir文件对象(File file = new File("/random")),它将很好地返回规范路径


共 (1) 个答案

  1. # 1 楼答案

    A symbolic link, also termed a soft link, is a special kind of file that points to another file, much like a shortcut in Windows or a Macintosh alias. Unlike a hard link, a symbolic link does not contain the data in the target file. It simply points to another entry somewhere in the file system.

    在macOS上/tmp被符号链接到/private/tmp。目录/private确实存在并且包含tmp