Android™
Android™
Flush an SFTP File to Stable Storage (Fsync)
See more SFTP Examples
Demonstrates the Chilkat SFtp.Fsync method, which requests that the server flush pending data for an open file to stable storage. The only argument is the handle. It uses the OpenSSH fsync@openssh.com extension and succeeds only when the server supports it.
Background: A successful write does not guarantee the data has reached disk — it may still sit in the server's write cache, where a crash or power loss would lose it.
Fsync forces it to durable storage, which matters for transactional or journal-style files where a committed record must survive a failure. Because it relies on a non-standard OpenSSH extension, expect it to fail against servers that do not implement it, and treat that as a capability check rather than a hard error where appropriate.Chilkat Android™ Downloads
// 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.Fsync method, which requests that the server flush pending data for an
// open file to stable storage. The only argument is the handle.
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;
}
String handle = sftp.openFile("subdir/journal.log","writeOnly","openOrCreate,appendData");
if (sftp.get_LastMethodSuccess() == false) {
Log.i(TAG, sftp.lastErrorText());
return;
}
// Write a critical record.
success = sftp.WriteFileText(handle,"utf-8","2026-07-23 transaction committed\n");
if (success == false) {
Log.i(TAG, sftp.lastErrorText());
return;
}
// Ask the server to flush the data to durable storage before continuing. Fsync uses the
// OpenSSH fsync@openssh.com extension and succeeds only when the server supports it.
success = sftp.Fsync(handle);
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, "Data flushed to stable storage.");
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."
}
}