简体   繁体   English

JAVA如何找到符号链接指向的目标文件路径?

[英]JAVA how to find the target file path that a symbolic link points to?

Good morning all, I'm trying to find a way in Java to figure out the file path that a symbolic link is pointing to. 大家早上好,我正试图在Java中找到一种方法来找出符号链接所指向的文件路径。 We have a system that monitors a symbolic link folder, does some stuff based on the file name, deletes that symbolic link file. 我们有一个监视符号链接文件夹的系统,根据文件名做一些事情,删除那个符号链接文件。 What I need to do is figure out where each symbolic link file is pointing to so I can delete that file as well when the symbolic link. 我需要做的是找出每个符号链接文件指向的位置,这样我也可以在符号链接时删除该文件。 This is using Java This is on a RHEL system. 这是使用Java这是在RHEL系统上。

Any guidance to API topics would be greatly appreciated. 任何API主题的指导将不胜感激。 I'm hitting a wall on my searches. 我在搜索时遇到了障碍。

https://docs.oracle.com/javase/8/docs/api/java/nio/file/Path.html#toRealPath-java.nio.file.LinkOption...- https://docs.oracle.com/javase/8/docs/api/java/nio/file/Path.html#toRealPath-java.nio.file.LinkOption...-

Path toRealPath(LinkOption... options) throws IOException Returns the real path of an existing file. Path toRealPath(LinkOption ... options)throws IOException返回现有文件的实际路径。 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. 如果此路径是相对的,则首先获取其绝对路径,就像通过调用toAbsolutePath方法一样。

The options array may be used to indicate how symbolic links are handled. options数组可用于指示如何处理符号链接。 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. 如果存在选项NOFOLLOW_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. 当不解析符号链接且前面的名称是符号链接时,只有在保证结果路径将找到与此路径相同的文件时才会删除名称。

Parameters: options - options indicating how symbolic links are handled Returns: an absolute path represent the real path of the file located by this object Throws: IOException - if the file does not exist or an I/O error occurs SecurityException - In the case of the default provider, and a security manager is installed, its checkRead method is invoked to check read access to the file, and where this path is not absolute, its checkPropertyAccess method is invoked to check access to the system property user.dir 参数:options - 指示如何处理符号链接的选项返回:绝对路径表示此对象所在文件的实际路径抛出:IOException - 如果文件不存在或发生I / O错误SecurityException - 如果是安装默认提供程序和安全管理器,调用其checkRead方法以检查对文件的读访问权限,并且此路径不是绝对路径,调用其checkPropertyAccess方法以检查对系统属性user.dir的访问权限。

Use the toRealPath() method from nio. 使用nio中的toRealPath()方法。 The NO_FOLLOW_LINKS LinkOption does the opposite of what you are trying to do, so don't use it. NO_FOLLOW_LINKS LinkOption与您尝试执行的操作相反,因此请勿使用它。

Path realPath = aFile.toPath().toRealPath()

Path realPath = aPath().toRealPath()

One gotcha with this, is that, unlike toAbsolutePath , toRealPath throws an IOException since it actually has to interact with the file system to find the real path. 其中一个问题是,与toAbsolutePath不同, toRealPath抛出IOException因为它实际上必须与文件系统交互以找到真实路径。

java.nio.file.Files.readSymbolicLink可能就是您所需要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM