Retrieving a remote directory listing

Top  Previous  Next

There are two ways that you can retrieve a directory listing from the server. The method used will depend on largely how you want to process the results.

 

Getting a directory listing as a string

 

To get a directory listing returned as a string you may use the Sftp#getDirListingAsString method.

 

Example

 

// get directory listing as a string

String dirListing = sftp.getDirListingAsString();

 

 

Parsing entries in a directory listing

 

In some cases you will want to evaluate the entries returned in a directory listing. In this case you will want to use the Sftp#getDirListing method. This method returns a java.util.Enumeration of SftpFile. Each SftpFile instance represents a read-only entry in the directory listing and may be queried for additional information.

 

Example

 

// get directory listing

Enumeration listing = sftp.getDirListing();

 

// enumerate thru listing printing filename for each entry

while(listing.hasMoreElements()) {

 SftpFile file = (SftpFile)listing.nextElement();

 System.out.println("Filename: " + file.getFilename());

}





Home | Company | Products | Solutions | Purchase | Support | Services | Blog

© 2021 JSCAPE LLC