• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Delphi2009中TStrings与TStream的增强

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
在 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.

窗体文件:
object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 103
  ClientWidth = 686
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object Button1: TButton
    Left = 117
    Top = 8
    Width = 150
    Height = 25
    Caption = 'Button1'
    TabOrder = 0
    OnClick = Button1Click
  end
  object Button2: TButton
    Left = 268
    Top = 8
    Width = 150
    Height = 25
    Caption = 'Button2'
    TabOrder = 1
    OnClick = Button2Click
  end
  object Button3: TButton
    Left = 419
    Top = 8
    Width = 150
    Height = 25
    Caption = 'Button3'
    TabOrder = 2
    OnClick = Button3Click
  end
  object Button4: TButton
    Left = 117
    Top = 39
    Width = 150
    Height = 25
    Caption = 'Button4'
    TabOrder = 3
    OnClick = Button4Click
  end
  object Memo1: TMemo
    Left = 0
    Top = 0
    Width = 116
    Height = 103
    Align = alLeft
    Lines.Strings = (
      'Hello World'
      #19990#30028#24744#22909
      #12495#12525#12540#19990#30028
      #50668#47084#48516', '#50504#45397#54616#49464#50836
      'Ol'#225' Mundo'
      #1055#1088#1080#1074#1077#1090' '#1084#1080#1088)
    TabOrder = 4
  end
  object Memo2: TMemo
    Left = 570
    Top = 0
    Width = 116
    Height = 103
    Align = alRight
    Lines.Strings = (
      'Memo2')
    TabOrder = 5
    ExplicitLeft = 578
  end
  object Button5: TButton
    Left = 268
    Top = 39
    Width = 150
    Height = 25
    Caption = 'Button5'
    TabOrder = 6
    OnClick = Button5Click
  end
  object Button6: TButton
    Left = 419
    Top = 39
    Width = 150
    Height = 25
    Caption = 'Button6'
    TabOrder = 7
    OnClick = Button6Click
  end
  object Button7: TButton
    Left = 117
    Top = 70
    Width = 150
    Height = 25
    Caption = 'Button7'
    TabOrder = 8
    OnClick = Button7Click
  end
  object Button8: TButton
    Left = 268
    Top = 70
    Width = 150
    Height = 25
    Caption = 'Button8'
    TabOrder = 9
    OnClick = Button8Click
  end
  object Button9: TButton
    Left = 419
    Top = 70
    Width = 150
    Height = 25
    Caption = 'Button9'
    TabOrder = 10
    OnClick = Button9Click
  end
end


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Matlab/Scipy/NumpyNURBSToolBox发布时间:2022-07-18
下一篇:
MATLAB求解线性规划发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap