如何在Delphi中将字符串保存到文本文件中? [英] How to save string into text files in Delphi?

查看:681
本文介绍了如何在Delphi中将字符串保存到文本文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建字符串并将其保存到 .txt 文件中最简单的方法是什么?

What is the easiest way to create and save string into .txt files?

推荐答案

使用 TStringList

uses
  Classes, Dialogs; // Classes for TStrings, Dialogs for ShowMessage

var
  Lines: TStrings;
  Line: string;
  FileName: string;
begin
  FileName := 'test.txt';
  Lines := TStringList.Create;
  try
    Lines.Add('First line');
    Lines.Add('Second line');
    Lines.SaveToFile(FileName);
    Lines.LoadFromFile(FileName);
    for Line in Lines do
      ShowMessage(Line);
  finally
    Lines.Free;
  end;
end;

SaveToFile LoadFromFile 可以在Delphi 2009及更高版本中使用其他编码来设置文本编码(Ansi,UTF-8,UTF-16,UTF-16大端字节序)。

Also SaveToFile and LoadFromFile can take an additional Encoding in Delphi 2009 and newer to set the text encoding (Ansi, UTF-8, UTF-16, UTF-16 big endian).

这篇关于如何在Delphi中将字符串保存到文本文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆