博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Delphi 2009 中 TStrings 与 TStream 的增强
阅读量:6702 次
发布时间:2019-06-25

本文共 6313 字,大约阅读时间需要 21 分钟。

  hot3.png

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

本例效果图:
26153000_XyWx.gif

代码文件:

unit Unit1;interfaceuses  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  endend

转载于:https://my.oschina.net/hermer/blog/319106

你可能感兴趣的文章
游戏安全资讯精选 2017年 第七期:游戏账号窃取日益猖獗,Struts2 REST插件远程执行命令漏洞全面分析,2017世界物联网博览会IoT安全观点...
查看>>
项立刚:FDD牌照发放 难改行业大格局
查看>>
移动广告作弊流量超过30%?你中招了吗
查看>>
CentOS 6.5环境 MongoDB 3.2.8 单实例安装部署
查看>>
基于阿里云MaxCompute实现复杂事件检测
查看>>
一键部署自动感知服务的Docker集群(一)
查看>>
【D3.js 学习总结】17、D3布局-分区图(矩形)
查看>>
《C语言及程序设计》实践项目——数组与指针
查看>>
MySQL中char和varchar有啥区别?优缺点是啥?
查看>>
PostgreSQL的函数安全定义解说
查看>>
pageinspect介绍
查看>>
C++语言基础 例程 类声明和成员函数定义的分离
查看>>
剑指offer学习笔记2
查看>>
面向对象
查看>>
动态分配的顺序线性表的十五种操作—C语言实现
查看>>
解决海量数据计算、降低企业成本的利器——MaxCompute
查看>>
JPEG编码
查看>>
github push403错误的处理
查看>>
正确理解ThreadLocal
查看>>
C# 文件流压缩解压
查看>>