简体   繁体   English

删除java中的符号链接

[英]Deleting a symbolic link in java

Is there any api available to delete a symbolic link using java.是否有任何 api 可用于使用 java.ini 删除符号链接? Files.delete(Path) didn't work out.Please post your suggestions. Files.delete(Path) 没有解决。请发表您的建议。

Files.delete(Path) is working perfectly on symbolic links. Files.delete(Path) 在符号链接上工作得很好。 You should have a other issue in your code.您的代码中应该有其他问题。

This code sample works (JAVA 8):此代码示例有效(JAVA 8):

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
...
String symLinkName = "/some/path/to/symlink";
try {               
    if(Files.exists(Paths.get(symLinkName))) {              
        Files.delete(Paths.get(symLinkName));
    }
} catch (IOException e) {
    log.info("Cannot delete symbolic link " + symLinkName);
    e.printStackTrace();
}

Remember that symbolic links are a UNIX concept and does not exist on Windows请记住,符号链接是一个 UNIX 概念,在 Windows 上不存在

I'll just go ahead and say that I think that我会继续说我认为

Remember that symbolic links are a UNIX concept and does not exist on Windows请记住,符号链接是一个 UNIX 概念,在 Windows 上不存在

is not correct, at least not completely.不正确,至少不完全正确。 Windows do have concept of symbolic and hard link which probably does differentiate in some parts from Unix concept. Windows 确实有符号和硬链接的概念,这可能在某些部分与 Unix 概念有所不同。 I could be completely wrong.我可能完全错了。

But this is not the main point of my reply, I actually ran into same kind of problem, I was not able to remove "symbolic link" which, in fact, was not even symbolic link.但这不是我回复的重点,我实际上遇到了同样的问题,我无法删除“符号链接”,实际上它甚至不是符号链接。 I created this symbolic link on Unix and transferred those directories to Windows machine and tried to remove that "symbolic link" with Java NIO, but it failed with java.nio.file.DirectoryNotEmptyException .我在 Unix 上创建了这个符号链接,并将这些目录转移到 Windows 机器上,并试图用 Java NIO 删除那个“符号链接”,但它失败了java.nio.file.DirectoryNotEmptyException If I create symbolic link on Windows as instructed in the link above, everything works fine from Java.如果我按照上面链接中的说明在 Windows 上创建符号链接,那么在 Java 中一切正常。

I know this is pretty specific scenario, but here's my 2 cents.我知道这是非常具体的情况,但这是我的 2 美分。

Some more information to symbolic links are foundhere .可以在此处找到有关符号链接的更多信息。 If you cant remove the link you can do it with a ProcessBuilder -class in Java and try it with your bash/cmd-command (depends on your used system).如果您无法删除链接,您可以使用 Java 中的ProcessBuilder类来完成,并使用您的 bash/cmd-command(取决于您使用的系统)进行尝试。

ProcessBuilder pb = new ProcessBuilder("<bash you use>", "rm", "<filepath>");
Process p = pb.start();

But normaly you can delete symbolic links with:但通常你可以删除符号链接:

Files.delete("path");

The directory needs to be empty.该目录必须为空。 if the Path is a symbolic link, then the link is deleted and not the target that it represents.如果 Path 是符号链接,则删除该链接而不是它所代表的目标。

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

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