string 如何在 Delphi 中将字符串保存到 .txt 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7752273/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to save a string to a .txt file in Delphi?
提问by Nyt Ryda
I need to make a program that generates a password that is saved in a text file format in a specific destination I set and the user needs to open the .txt to get the password to 'unlock' another program.
我需要制作一个程序来生成一个密码,该密码以文本文件格式保存在我设置的特定目标中,用户需要打开 .txt 以获取密码以“解锁”另一个程序。
I have already got the code to generate the password in the string sPass and now I need to use the SaveToFile function to save it into the text file I created called Password.txt but I cannot find the general form to use the SaveTo File Function in Delphi and I do not know where to put the sPass and Password.txt in the function.
我已经得到了在字符串 sPass 中生成密码的代码,现在我需要使用 SaveToFile 函数将其保存到我创建的名为 Password.txt 的文本文件中,但我找不到使用 SaveTo File 函数的一般形式Delphi和我不知道把sPass和Password.txt放在函数的什么地方。
It should be something like : SaveToFile(...) but I do not know how to save sPass in Password.txt
它应该是这样的: SaveToFile(...) 但我不知道如何在 Password.txt 中保存 sPass
Edit :
编辑 :
Just one more question, how do you delete what is previously stored in Password.txt before you add the string to it so that Password.txt is blank before the string is added ? Thanks
还有一个问题,如何在添加字符串之前删除之前存储在 Password.txt 中的内容,以便在添加字符串之前 Password.txt 为空?谢谢
回答by awmross
The Modern Modern way is to use TFile.WriteAllText
in IOUtils (Delphi 2010 and up)
Modern Modern 方法是TFile.WriteAllText
在 IOUtils 中使用(Delphi 2010 及更高版本)
procedure WriteAllText(const Path: string; const Contents: string); overload; static;
Creates a new file, writes the specified string to the file, and then closes the file. If the target file already exists, it is overwritten.
过程WriteAllText(常量路径:字符串;常量内容:字符串);超载; 静止的;
创建一个新文件,将指定的字符串写入文件,然后关闭文件。如果目标文件已经存在,它会被覆盖。
回答by Johan
The modern way is to create a stringlist and save that to file.
现代方法是创建一个字符串列表并将其保存到文件中。
procedure MakeAStringlistAndSaveThat;
var
MyText: TStringlist;
begin
MyText:= TStringlist.create;
try
MyText.Add('line 1');
MyText.Add('line 2');
MyText.SaveToFile('c:\folder\filename.txt');
finally
MyText.Free
end; {try}
end;
Note that Delphi already has a related class that does everything you want: TInifile.
It stores values and keys in a key = 'value'
format.
请注意,Delphi 已经有一个相关的类可以做你想做的一切:TInifile。
它以某种key = 'value'
格式存储值和键。
passwordlist:= TInifile.Create;
try
passwordlist.LoadFromFile('c:\folder\passwords.txt');
//Add or replace a password for `user1`
passwordlist.WriteString('sectionname','user1','topsecretpassword');
passwordlist.SaveToFile('c:\folder\passwords.txt');
finally
passwordlist.Free;
end; {try}
Warning
Note that saving unecrypted passwords in a textfile is a security-leak. It's better to hash your passwords using a hashfunction, see: Password encryption in Delphi
For tips on how to save passwords in a secure way.
警告
请注意,将未加密的密码保存在文本文件中是一个安全漏洞。最好使用散列函数散列密码,请参阅:Delphi 中的密码加密
有关如何以安全方式保存密码的提示。
回答by RRUZ
You can use the TFileStream
class to save a string to a file:
您可以使用TFileStream
该类将字符串保存到文件中:
uses
Classes;
procedure StrToFile(const FileName, SourceString : string);
var
Stream : TFileStream;
begin
Stream:= TFileStream.Create(FileName, fmCreate);
try
Stream.WriteBuffer(Pointer(SourceString)^, Length(SourceString));
finally
Stream.Free;
end;
end;
and to read
并阅读
function FileToStr(const FileName : string):string;
var
Stream : TFileStream;
begin
Stream:= TFileStream.Create(FileName, fmOpenRead);
try
SetLength(Result, Stream.Size);
Stream.Position:=0;
Stream.ReadBuffer(Pointer(Result)^, Stream.Size);
finally
Stream.Free;
end;
end;
回答by TPAKTOPA
Fastest and simplest way, no need to declare any variables:
最快最简单的方法,不需要声明任何变量:
with TStringList.Create do
try
Add(SomeString);
SaveToFile('c:.txt');
finally
Free;
end;