Sample code for 30+ languages & platforms
Android™

Write Text to a Remote SFTP File (Sequential)

See more SFTP Examples

Demonstrates the Chilkat SFtp.WriteFileText method, which encodes text using a charset and writes it at the current sequential write position of an open handle. The arguments are the handle, the charset, and the text.

Background: Chilkat tracks a write position per handle, so successive WriteFileText calls append naturally — the pattern for building a file a piece at a time, such as writing rows to a report. Opening with openOrCreate starts writing at the current end of the file. The charset determines the bytes actually stored, which matters whenever the text contains non-ASCII characters.

Chilkat Android™ Downloads

Android™
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    boolean success = false;

    //  Demonstrates the SFtp.WriteFileText method, which encodes text and writes it at the current
    //  sequential write position of an open handle.  The 1st argument is the handle, the 2nd is the
    //  charset, and the 3rd is the text.

    CkSFtp sftp = new CkSFtp();

    //  Connect, authenticate, and initialize the SFTP subsystem.
    int port = 22;
    success = sftp.Connect("sftp.example.com",port);
    if (success == false) {
        Log.i(TAG, sftp.lastErrorText());
        return;
        }

    //  Normally you would not hard-code the password in source.  You should instead obtain it
    //  from an interactive prompt, environment variable, or a secrets vault.
    String password = "mySshPassword";

    success = sftp.AuthenticatePw("mySshLogin",password);
    if (success == false) {
        Log.i(TAG, sftp.lastErrorText());
        return;
        }

    success = sftp.InitializeSftp();
    if (success == false) {
        Log.i(TAG, sftp.lastErrorText());
        return;
        }

    //  Open (creating/truncating) the remote file for writing.
    String handle = sftp.openFile("subdir/notes.txt","writeOnly","createTruncate");
    if (sftp.get_LastMethodSuccess() == false) {
        Log.i(TAG, sftp.lastErrorText());
        return;
        }

    //  Write text as UTF-8.  Successive writes continue from where the previous write ended.
    success = sftp.WriteFileText(handle,"utf-8","First line.\n");
    if (success == false) {
        Log.i(TAG, sftp.lastErrorText());
        return;
        }

    success = sftp.WriteFileText(handle,"utf-8","Second line.\n");
    if (success == false) {
        Log.i(TAG, sftp.lastErrorText());
        return;
        }

    success = sftp.CloseHandle(handle);
    if (success == false) {
        Log.i(TAG, sftp.lastErrorText());
        return;
        }

    Log.i(TAG, "Wrote text file.");

    sftp.Disconnect();

  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}