程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> Delphi 2009 中 TStrings 與 TStream 的增強

Delphi 2009 中 TStrings 與 TStream 的增強

編輯:Delphi

 在 Delphi 2009 中,

  TStrings 類(包括 TStringList 和包含 TStrings 屬性的所有組件, 譬如 Memo.lines 屬性、ListBox.Items 屬性)、

  TStream 類系列(包括 TFileStream、TMemoryStream 等) 的存取方法, 譬如:

  LoadFromFile、SaveToFile、LoadFromStream、SaveToStream, 都有了選擇 TEncoding 參數的重載.

  本例效果圖:

Delphi 2009 中 TStrings 與 TStream 的增強

  代碼文件: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:temptest.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


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved