Using JSch, is there a way to tell if a remote file exists without doing an ls?

前端 未结 6 451
轻奢々
轻奢々 2020-12-15 21:42

Using JSch, is there a way to tell if a remote file exists without doing an ls and looping through the files to find a name match?

Thanks

6条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-15 21:48

    You can also do something like this:

    try {
        channelSftp.lstat(name);
    } catch (SftpException e){
        if(e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE){
        // file doesn't exist
        } else {
        // something else went wrong
            throw e;
        }
    }
    

    If you do an lstat on something that doesn't exist you get an SftpExecption with an id of 2, otherwise you get information about the file.

提交回复
热议问题