Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

jsch

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

like image 389
cagcowboy Avatar asked Aug 15 '12 11:08

cagcowboy


1 Answers

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.

like image 134
zelinka Avatar answered Oct 02 '22 19:10

zelinka