Sample code for 30+ languages & platforms
Java

SFTP Read Text File

See more SFTP Examples

Demonstrates how to open a text file on the SSH server and read text.

Chilkat Java Downloads

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

public class ChilkatExample {

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

        SFtp sftp = new SFtp();

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

        // Connect to the SSH server.
        // The standard SSH port = 22
        // The hostname may be a hostname or IP address.
        String hostname = "sftp.example.com";
        int port = 22;
        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();
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Open a file for reading.
        String handle = "";
        try {
            handle = sftp.openFile("myTest.txt", "readOnly", "openExisting");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Assume the file we are reading contains the following text:
        // abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ
        // (in ANSI format -- i.e. one byte per char).

        // Read 26 bytes:
        String sText = "";
        try {
            sText = sftp.readFileText(handle, 26, "ansi");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Should print "abcdefghijklmnopqrstuvwxyz";
        System.out.println(sText);

        // Read the next 10 bytes.
        try {
            sText = sftp.readFileText(handle, 10, "ansi");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Should print "1234567890";
        System.out.println(sText);

        // Read the next 26 bytes.
        try {
            sText = sftp.readFileText(handle, 26, "ansi");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Should print "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        System.out.println(sText);

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

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