Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Read Text from a Remote SFTP File (Sequential)
See more SFTP Examples
Demonstrates the Chilkat SFtp.ReadFileText method, which reads up to a number of bytes from the current position of an open handle and decodes them to text. The arguments are the handle, the maximum number of bytes, and the charset. This example reads in chunks until end-of-file.
Background: Reading a file in bounded chunks keeps memory use predictable regardless of file size, which is why the loop reads a fixed amount and repeats until
Eof. A subtlety worth knowing: the byte limit is applied to the encoded bytes, so a chunk boundary could in principle fall in the middle of a multibyte character — accumulating the pieces and decoding the whole avoids any such issue in practice here since the text is reassembled in a StringBuilder.Chilkat Pascal (Lazarus/Delphi) Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.SFtp,
Chilkat.StringBuilder;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
sftp: TSFtp;
port: Integer;
password: string;
handle: string;
sbContent: TStringBuilder;
chunkSize: Integer;
reading: Boolean;
chunk: string;
begin
success := False;
// Demonstrates the SFtp.ReadFileText method, which reads up to a number of bytes from the current
// position of an open handle and decodes them to text. The 1st argument is the handle, the 2nd
// is the maximum number of bytes, and the 3rd is the charset.
sftp := TSFtp.Create;
// Connect, authenticate, and initialize the SFTP subsystem.
port := 22;
success := sftp.Connect('sftp.example.com',port);
if (success = False) then
begin
WriteLn(sftp.LastErrorText);
Exit;
end;
// 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.
password := 'mySshPassword';
success := sftp.AuthenticatePw('mySshLogin',password);
if (success = False) then
begin
WriteLn(sftp.LastErrorText);
Exit;
end;
success := sftp.InitializeSftp();
if (success = False) then
begin
WriteLn(sftp.LastErrorText);
Exit;
end;
// Open the remote file for reading.
handle := sftp.OpenFile('subdir/notes.txt','readOnly','openExisting');
if (sftp.LastMethodSuccess = False) then
begin
WriteLn(sftp.LastErrorText);
Exit;
end;
// Read the file in chunks until end-of-file, accumulating the text.
sbContent := TStringBuilder.Create;
chunkSize := 4096;
reading := True;
while reading do
begin
chunk := sftp.ReadFileText(handle,chunkSize,'utf-8');
if (sftp.LastReadFailed(handle)) then
begin
WriteLn(sftp.LastErrorText);
Exit;
end;
sbContent.Append(chunk);
// Stop once the end of the file has been reached.
reading := not sftp.Eof(handle);
end;
success := sftp.CloseHandle(handle);
if (success = False) then
begin
WriteLn(sftp.LastErrorText);
Exit;
end;
WriteLn(sbContent.GetAsString());
sftp.Disconnect();
sftp.Free;
sbContent.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.