Sample code for 30+ languages & platforms
Java Requires Chilkat v11.0.0+

SFTP Read Directory Listing

See more SFTP Examples

Demonstrates how to download a directory listing and iterate over the files.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.SFtp;
import com.chilkatsoft.chilkat2.SFtpDir;
import com.chilkatsoft.chilkat2.SFtpFile;

public class ChilkatExample {

    public static void main(String[] args) {
        // This requires the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        // Important: It is helpful to send the contents of the
        // sftp.LastErrorText property when requesting support.

        SFtp sftp = new SFtp();

        // Set some timeouts, in milliseconds:
        sftp.setConnectTimeoutMs(5000);
        sftp.setIdleTimeoutMs(10000);

        // Connect to the SSH server.
        // The standard SSH port = 22
        // The hostname may be a hostname or IP address.
        String hostname = "www.my-sftp-server.com";
        int port = 22;
        String handle;
        try {
            sftp.connect(hostname, port);

            // Authenticate with the SSH server.  Chilkat SFTP supports
            // both password-based authenication as well as public-key
            // authentication.  This example uses password authenication.
            sftp.authenticatePw("myLogin", "myPassword");

            // After authenticating, the SFTP subsystem must be initialized:
            sftp.initializeSftp();

            // Open a directory on the server...
            // Paths starting with a slash are "absolute", and are relative
            // to the root of the file system. Names starting with any other
            // character are relative to the user's default directory (home directory).
            // A path component of ".." refers to the parent directory,
            // and "." refers to the current directory.
            handle = sftp.openDir(".");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Download the directory listing:

        SFtpDir dirListing = new SFtpDir();
        try {
            sftp.readDirListing(handle, dirListing);

            // Close the handle for the directory listing.
            sftp.closeHandle(handle);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Iterate over the files.
        SFtpFile fileObj = new SFtpFile();
        int i = 0;
        int n = dirListing.getNumFilesAndDirs();
        while (i < n) {
            try {
                dirListing.fileAt(i, fileObj);
            } catch (ChilkatException e) {
                System.out.println(e.getLastErrorText());
                return;
            }

            System.out.println(fileObj.getFilename());
            System.out.println(fileObj.getFileType());
            System.out.println("Size in bytes: " + fileObj.getSize32());
            System.out.println("----");

            i = i + 1;
        }

        System.out.println("Success.");
    }
}