Java: check symbolic link file existence

后端 未结 5 877
醉梦人生
醉梦人生 2021-01-11 13:58

We talk about java 1.6 here. Since symoblic link is not yet supported, how can examine the existence of them.

1: tell wheather the link file itself exists (return tr

5条回答
  •  不思量自难忘°
    2021-01-11 14:42

    #2 is easy: just use File.isFile etc., which will traverse the link. The hard part is #1.

    So if you cannot use java.nio.file, and want to avoid JNI etc., the only thing I found which works:

    static boolean exists(File link) {
        File[] kids = link.getParentFile().listFiles();
        return kids != null && Arrays.asList(kids).contains(link);
    }
    

    Not terribly efficient but straightforward.

提交回复
热议问题