Delphi 2009 中 TStrings 与 TStream 的增强


本文整理自网络,侵删。

 
在 Delphi 2009 中,
TStrings 类(包括 TStringList 和包含 TStrings 属性的所有组件, 譬如 Memo.lines 属性、ListBox.Items 属性)、
TStream 类系列(包括 TFileStream、TMemoryStream 等) 的存取方法, 譬如:
LoadFromFile、SaveToFile、LoadFromStream、SaveToStream, 都有了选择 TEncoding 参数的重载.

本例效果图:

代码文件:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Memo2: TMemo;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    Button7: TButton;
    Button8: TButton;
    Button9: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
    procedure Button9Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

const
  FilePath = 'c:\temp\test.txt';

var
  str: string;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Memo1.ReadOnly := True;
  str := Memo1.Text;

  Button1.Caption := 'TStrings 默认存取';
  Button2.Caption := 'TStrings ASCII 存取';
  Button3.Caption := 'TStrings UTF8 存取';
  Button4.Caption := 'TFileStream 默认存取';
  Button5.Caption := 'TFileStream ASCII 存取';
  Button6.Caption := 'TFileStream UTF8存取';
  Button7.Caption := 'TMemoryStream 默认存取';
  Button8.Caption := 'TMemoryStream ASCII 存取';
  Button9.Caption := 'TMemoryStream UTF8 存取';
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  List: TStrings;
begin
  List := TStringList.Create;
  List.Text := str;
  List.SaveToFile(FilePath);
  List.LoadFromFile(FilePath);
  Memo2.Lines := List;
  List.Free;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  List: TStrings;
begin
  List := TStringList.Create;
  List.Text := str;
  List.SaveToFile(FilePath, TEncoding.ASCII);
  List.LoadFromFile(FilePath, TEncoding.ASCII);
  Memo2.Lines := List;
  List.Free;
end;

procedure TForm1.Button3Click(Sender: TObject);
var
  List: TStrings;
begin
  List := TStringList.Create;
  List.Text := str;
  List.SaveToFile(FilePath, TEncoding.UTF8);
  List.LoadFromFile(FilePath, TEncoding.UTF8);
  Memo2.Lines := List;
  List.Free;
end;

procedure TForm1.Button4Click(Sender: TObject);
var
  stream: TStream;
begin
  stream := TFileStream.Create(FilePath, fmCreate);
  Memo1.Lines.SaveToStream(stream);
  stream.Position := 0;
  Memo2.Lines.LoadFromStream(stream);
  stream.Free;
end;

procedure TForm1.Button5Click(Sender: TObject);
var
  stream: TStream;
begin
  stream := TFileStream.Create(FilePath, fmCreate);
  Memo1.Lines.SaveToStream(stream, TEncoding.ASCII);
  stream.Position := 0;
  Memo2.Lines.LoadFromStream(stream, TEncoding.ASCII);
  stream.Free;
end;

procedure TForm1.Button6Click(Sender: TObject);
var
  stream: TStream;
begin
  stream := TFileStream.Create(FilePath, fmCreate);
  Memo1.Lines.SaveToStream(stream, TEncoding.UTF8);
  stream.Position := 0;
  Memo2.Lines.LoadFromStream(stream, TEncoding.UTF8);
  stream.Free;
end;

procedure TForm1.Button7Click(Sender: TObject);
var
  stream: TMemoryStream;
begin
  stream := TMemoryStream.Create;
  Memo1.Lines.SaveToStream(stream);
  stream.SaveToFile(FilePath);
  stream.LoadFromFile(FilePath);
  stream.Position := 0;
  Memo2.Lines.LoadFromStream(stream);
  stream.Free;
end;

procedure TForm1.Button8Click(Sender: TObject);
var
  stream: TMemoryStream;
begin
  stream := TMemoryStream.Create;
  Memo1.Lines.SaveToStream(stream, TEncoding.ASCII);
  stream.SaveToFile(FilePath);
  stream.LoadFromFile(FilePath);
  stream.Position := 0;
  Memo2.Lines.LoadFromStream(stream, TEncoding.ASCII);
  stream.Free;
end;

procedure TForm1.Button9Click(Sender: TObject);
var
  stream: TMemoryStream;
begin
  stream := TMemoryStream.Create;
  Memo1.Lines.SaveToStream(stream, TEncoding.UTF8);
  stream.SaveToFile(FilePath);
  stream.LoadFromFile(FilePath);
  stream.Position := 0;
  Memo2.Lines.LoadFromStream(stream, TEncoding.UTF8);
  stream.Free;
end;

end.

相关阅读 >>

Delphi获取进程pid

Delphi提取网页中的图片

Delphi下ado的多线程编程

Delphi 10中实现android的广播接收器

Delphi的枚举类型

Delphi返回一个汉字的unicode编码

Delphi combobox1dropdown 生成动态下拉列表

Delphi版inf方式加载驱动

Delphi 7 简单语法

Delphi中的sha256算法

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



打赏

取消

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

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

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

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

评论

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