delphi 生成并替换txt里的内容  
官方Delphi 学习QQ群: 682628230(三千人)
频道

delphi 生成并替换txt里的内容


准备好你的文本文件 
text.txt${Name} 路过 

新建一个扩展名为".rc"的文件(resource),内容为: 
test.rcTest_Text File "test.txt" 
格式为:资源名 类型 文件名(没加路径就是当前路径) 

工程里添加该".rc"文件 
Delphi菜单->Project->Add to Project... (文件类型选为“*.rc”) 

上面的步骤完成后,可以在工程代码中看到: 

Delphi(Pascal) code 
program Project1;
{$R 'test.res' 'test.rc'}
//...

文本资源已经加到当前工程中 

如下代码就不再加说明了,你自己查查帮助看看 

Delphi(Pascal) code 
procedure TForm1.Button1Click(Sender: TObject);
var
  S: string;
begin
  with TResourceStream.Create(HInstance, 'Test_Text', 'File') do try
    if Size <= 0 then Exit;
    SetLength(S, Size);
    Read(S[1], Size);
    with TStringList.Create do try
      Text := StringReplace(S, '${Name}', 'Zswang',
        [rfReplaceAll, rfIgnoreCase]); // 替换字符
      SaveToFile('c:\temp\temp.txt'); // 保存为文件
    finally
      Free;
    end;
  finally
    Free;
  end;
end;


输出 
c:\temp\temp.txt 

推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.17018294334412 seconds