Java: check symbolic link file existence

后端 未结 5 910
醉梦人生
醉梦人生 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:46

    The following should give you a start:

    if (file.exists() && !file.isDirectory() && !file.isFile()) {
        // it is a symbolic link (or a named pipe/socket, or maybe some other things)
    }
    
    if (file.exists()) {
        try {
            file.getCanonicalFile();
        } catch (FileNotFoundException ex) {
            // it is a broken symbolic link
        }
    }
    

    EDIT : The above don't work as I thought because file.isDirectory() and file.isFile() resolve a symbolic link. (We live and learn!)

    If you want an accurate determination, you will need to use JNI to make the relevant native OS-specific library calls.

提交回复
热议问题