java - toRealPath 方法的行为(类 java.nio.file.Path)

标签 java path nio

直接来自this oracle java教程:

它提到了 Path 类的 toRealPath 方法:

If true is passed to this method and the file system supports symbolic links, this method resolves any symbolic links in the path.

如果 false 被传递给构造函数但路径实际上包含符号链接(symbolic link)怎么办? 程序在运行时会如何 react ?

提前致谢。

最佳答案

您可以只传递任何内容而不是 NOFOLLOW_LINKS,这样它就不会解析符号链接(symbolic link),而只是规范化路径(删除不必要的“..”)并将其设为绝对路径。

When not resolving symbolic links and the preceding name [to the ".."] is a symbolic link then the names are only removed if it guaranteed that the resulting path will locate the same file as this path.

(javadoc)

从 javadoc 获得的带有附加信息的方法的完整描述:

Returns the real path of an existing file. The precise definition of this method is implementation dependent but in general it derives from this path, an absolute path that locates the same file as this path, but with name elements that represent the actual name of the directories and the file. For example, where filename comparisons on a file system are case insensitive then the name elements represent the names in their actual case. Additionally, the resulting path has redundant name elements removed.

If this path is relative then its absolute path is first obtained, as if by invoking the toAbsolutePath method.

The options array may be used to indicate how symbolic links are handled. By default, symbolic links are resolved to their final target. If the option NOFOLLOW_LINKS is present then this method does not resolve symbolic links. Some implementations allow special names such as ".." to refer to the parent directory. When deriving the real path, and a ".." (or equivalent) is preceded by a non-".." name then an implementation will typically cause both names to be removed. When not resolving symbolic links and the preceding name is a symbolic link then the names are only removed if it guaranteed that the resulting path will locate the same file as this path.

来源:javadoc:java se 8:http://docs.oracle.com/javase/8/docs/api/java/nio/file/Path.html#toRealPath-java.nio.file.LinkOption...-

关于java - toRealPath 方法的行为(类 java.nio.file.Path),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18046595/

相关文章:

java - Jboss AMQ : Send persistent message from hawtio

web-services - 部分 URL : host, 端口、路径

java - "system-dependent default"线程池是什么?

java - netty如何为新请求分配工作线程?

java - 如何重构这段IO代码?

java - 在 Java 中清理字符串

java - 使用 hibernate 工具自动创建序列

java - 枚举:每个实例独有的方法

ruby - 使用 glob 模式查找某些文件

Java NIO 路径 : getting the base path from a full path?