English 中文(简体)
如何在Delphi的文字档案中保存插图?
原标题:How to save string into text files in Delphi?

什么最容易地建立和拯救条形(txt文档?

问题回答

使用<代码>TStringList。

uses
  Classes, Dialogs; // Classes for TStrings, Dialogs for ShowMessage

var
  Lines: TStrings;
  Line: string;
  FileName: string;
begin
  FileName :=  test.txt ;
  Lines := TStringList.Create;
  try
    Lines.Add( First line );
    Lines.Add( Second line );
    Lines.SaveToFile(FileName);
    Lines.LoadFromFile(FileName);
    for Line in Lines do
      ShowMessage(Line);
  finally
    Lines.Free;
  end;
end;

Also SaveToFile and LoadFromFile can take an additional Encoding in Delphi 2009 and newer to set the text encoding (Ansi, UTF-8, UTF-16, UTF-16 big endian).

实际上,我更喜欢:

var
  Txt: TextFile;
  SomeFloatNumber: Double;
  SomeStringVariable: string;
  Buffer: Array[1..4096] of byte;
begin
  SomeStringVariable :=  Text ;
  AssignFile(Txt,  Some.txt );
  Rewrite(Txt);
  SetTextBuf(Txt, Buffer, SizeOf(Buffer));
  try
    WriteLn(Txt,  Hello, World. );
    WriteLn(Txt, SomeStringVariable);
    SomeFloatNumber := 3.1415;
    WriteLn(Txt, SomeFloatNumber:0:2); // Will save 3.14
  finally CloseFile(Txt);
  end;
end;

I consider this the easiest way, since you don t need the classes or any other unit for this code. And it works for all Delphi versions including -if I m not mistaken- all .NET versions of Delphi...


我在此举了一个例子,叫SetaTextBuf(SetaTextBuf),这是大大加快Delphi案卷的良好骗局。 通常,文本单只有128个 by。 我倾向于将这一缓冲带扩大到4096个tes。 在几个案例中,我还实施了我自己的文本类型,使我能够利用这些“console”功能,把文字写到电梯田,甚至到另一个外部应用! 在上,这个地点是我于2000年写的,刚刚修改,以确保与Delphi2007年汇编。 但无法确定新的德尔菲版本。 其后,该法典已经存在10年,即:
这些青少年功能是帕斯卡尔语的标准,自此开始,我并不期望他们很快会消失。 “TtextRec”类可能在今后加以修改,但因此,我可以预测该法典今后是否会奏效。 一些解释:

  • WA_TextCustomEdit.AssignCustomEdit allows text to be written to CustomEdit-based objects like TMemo.
  • WA_TextDevice.TWATextDevice is a class that can be dropped on a form, which contains events where you can do something with the data written.
  • WA_TextLog.AssignLog is used by me to add timestamps to every line of text.
  • WA_TextNull.AssignNull is basically a dummy text device. It just discards anything you write to it.
  • WA_TextStream.AssignStream writes text to any TStream object, including memory streams, file streams, TCP/IP streams and whatever else you have.

http://creativecommons.org/licenses/by/3.0/“rel=“nofollow noreferer”>CC-BY“alt


Oh是ZIP档案的服务器,其实力很强,因此,它往往每天被击落几次。 与此有关。

在2010年德尔菲斯启用的IOUtils单位为撰写/阅读文本档案提供了一些非常方便的职能:

//add the text  Some text  to the file  C:usersdocuments	est.txt :
TFile.AppendAllText( C:usersdocuments	ext.txt ,  Some text , TEncoding.ASCII);

或者如果你使用较旧的德尔菲(没有<条码>的条码>,则使用。 2. 重述清单的方法:

var i : integer;
begin

...
try
    Lines.Add( First line );
    Lines.Add( Second line );
    Lines.SaveToFile(FileName);
    Lines.LoadFromFile(FileName);
    for i := 0 to Lines.Count -1 do
      ShowMessage(Lines[i]);
  finally
    Lines.Free;
  end;

如果您重新使用Delphi版本,到2009年,请查阅TStreamWriter

还将关注文本文档编码和新线特性。

procedure String2File;
var s:ansiString;
begin
    s:= My String ;
    with TFileStream.create( c:myfile.txt ,fmCreate) do
    try
      writeBuffer(s[1],length(s));
    finally
      free;
    end;
end;

<>Care need when using unicode strings....





相关问题
determining the character set to use

my delphi 2009 app has a basic translation system that uses GNUGetText. i had used some win API calls to prepare the fonts. i thought it was working correctly until recently when someone from Malta ...

Help with strange Delphi 5 IDE problems

Ok, I m going nuts here. For the last (almost) four years, I ve been putting up with some extremely bad behavior from my Delphi 5 IDE. Problems include: Seemingly random errors in coride50.bpl ...

How to write a Remote DataModule to run on a linux server?

i would like to know if there are any solution to do this. Does anyone? The big picture: I want to access data over the web, using my delphi thin clients. But i´would like to keep my server/service ...

How convert string to integer in Oxygene

In Delphi, there is a function StrToInt() that converts a string to an integer value; there is also IntToStr(), which does the reverse. These functions doesn t appear to be part of Oxygene, and I can ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签