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 获取打印机纸型的例子

Delphi复制文件夹内所有文件

Delphi中webbrowser的用法

Delphi 取得和设置当前系统的短日期格式

Delphi tdirectory.tfilterpredicate

Delphi整理二(object pascal语言)

Delphi 调用驱动

Delphi中实现全角转半角

Delphi手动创建dataset并插入值

Delphi xe5 android 开发实现手机打电话和发短信

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



打赏

取消

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

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

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

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

评论

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