Delphi读取文本文件的最后一行


本文整理自网络,侵删。

 
unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;
 
type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
procedure TForm1.Button1Click(Sender: TObject);
var
  LFindHandle: THandle;
  LFindData: TWin32FindData;
  LSearchFullPath, LFileName: String;
  LFileList: TStringList;
  LLines: TStringList;
  I: Integer;
  LFile: TextFile;
  LLine, LLastLine: string;
begin
  // 对于大文件,速度很慢
  LFileList := TStringList.Create;
  LLines := TStringList.Create;
  LSearchFullPath := Edit1.Text;
 
  FillChar(LFindData, SizeOf(LFindData), 0);
  LFindHandle := FindFirstFile(PChar(LSearchFullPath), LFindData);
  if LFindHandle <> INVALID_HANDLE_VALUE then
  begin
    try
      repeat
        if LFindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY <> 0 then
        begin
          Continue;
        end;
 
          if LFindData.dwFileAttributes and FILE_ATTRIBUTE_HIDDEN <> 0 then
            Continue;
          LFileName := LFindData.cFileName;
          LFileList.Add(LFileName);
 
      until not FindNextFile(LFindHandle, LFindData);
    finally
      Windows.FindClose(LFindHandle);
    end;
  end;
 
  for I := 0 to LFileList.Count - 1 do
  begin
    AssignFile(LFile, LFileList.Strings[I]);
    Reset(LFile);
    LLastLine := '';
    while not Eof(LFile) do
    begin
      Readln(LFile, LLine);
      if Length(LLine) > 0 then
      begin
        LLastLine := LLine;
      end;
    end;
 
    if Length(LLastLine) > 0 then
      LLines.Add(LLastLine);
  end;
 
  LLines.SaveToFile('1.dat');
 
  LFileList.Free;
  LLines.Free;
end;
 
function TextSeek(var f: Text; position: Int64; FromPos: DWORD = FILE_BEGIN): Int64;
var pos64: Int64Rec absolute position;
    resHi: cardinal;
    resLow: Cardinal;
begin
  result := 0;
  with TTextRec(f) do
  begin
    if mode<>fmInput then
      exit;
    resHi := pos64.Hi;
    resLow := SetFilePointer(handle,pos64.Lo,@resHi,FromPos);
    BufEnd := 0; // flush internal reading buffer
    BufPos := 0;
 
    // 返回当前文件的位置
    if (resHi = INVALID_HANDLE_VALUE) and (resLow = INVALID_HANDLE_VALUE) then
      Result := -1
    else
      result := resHi shl 32 + resLow;
  end;
end;
 
procedure TForm1.Button2Click(Sender: TObject);
var
  LFindHandle: THandle;
  LFindData: TWin32FindData;
  LSearchFullPath, LFileName: String;
  LFileList: TStringList;
  LLines: TStringList;
  I: Integer;
  LEndIndex: Integer;
  LFile: TextFile;
  LLine, LLastLine: string;
begin
  LFileList := TStringList.Create;
  LLines := TStringList.Create;
  LSearchFullPath := Edit1.Text;
  FillChar(LFindData, SizeOf(LFindData), 0);
  LFindHandle := FindFirstFile(PChar(LSearchFullPath), LFindData);
  if LFindHandle <> INVALID_HANDLE_VALUE then
  begin
    try
      repeat
        if LFindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY <> 0 then
        begin
          Continue;
        end;
 
          if LFindData.dwFileAttributes and FILE_ATTRIBUTE_HIDDEN <> 0 then
            Continue;
          LFileName := LFindData.cFileName;
          LFileList.Add(LFileName);
 
      until not FindNextFile(LFindHandle, LFindData);
    finally
      Windows.FindClose(LFindHandle);
    end;
  end;
 
  for I := 0 to LFileList.Count - 1 do
  begin
    AssignFile(LFile, LFileList.Strings[I]);
    Reset(LFile);
    LLastLine := '';
 
    // 先去掉最后的空行
    LEndIndex := 1;
    while True do
    begin
      if TextSeek(LFile, -LEndIndex, FILE_END) < 0 then
        Break;
      
      Readln(LFile, LLine);
      if Length(LLine) > 0 then
        Break;
 
      Inc(LEndIndex);
    end;
 
    // 读取最后一行
    Inc(LEndIndex);
    while True do
    begin
      if TextSeek(LFile, -LEndIndex, FILE_END) < 0 then
        Break;
      Readln(LFile, LLine);
      if Length(LLine) = 0 then
        Break;
 
      LLastLine := LLine;
 
      Inc(LEndIndex);
    end;
 
    if Length(LLastLine) > 0 then
      LLines.Add(LLastLine);
 
    CloseFile(LFile);
  end;
 
  LLines.SaveToFile('1.dat');
 
  LFileList.Free;
  LLines.Free;
end;
 
end.

来源:https://blog.csdn.net/xiuzhentianting/article/details/48472969

相关阅读 >>

tstringlist 常用操作

Delphi三层开发小技巧:tclientdataset的delta妙用

Delphi 双击richedit高亮所有关键字

Delphi tstringlist stringlist的特殊用法

Delphi 递归遍历 treeview树节点

5种运行程序的方法具体应用实例

Delphi listview 导出excel txt vcf 单元

Delphi 获取webbrowser中的图片

Delphi datasnap 的http 调用返回json

Delphi 匹配中文的正则表达式

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...