Kush Sahu Kush Sahu - 6 months ago 33
Java Question

How to get only directories and text files using SFTP channel of JSch?

Hi I am developing an application which connects to remote server and browses through different directories.

Here I want to show only directories and text files to user. Using SFTP channel in JSch, I can execute

ls
method. But this method can give me result for either in this format
"*"
or
"*.txt"
. Using
ls
separately I can get directories list and text file list. Since I am using it separately I have to use 2 different
ls
methods like:

sftpChannel.ls("*");
sftpChannel.ls("*.txt");


1st gives me all entries from which I have to loop and filter directories. In second, I get all text files.

How can I get directories list and text file list using minimum code. I don't want to loop twice. Thanks

Answer

Use ls(""). Then loop the returned entries, and select only those you want.

I.e. those that have LsEntry.getFilename() ending with ".txt" or LsEntry.getAttrs().isDir().

Comments