delphi流操作
Delphi
1 把字符串直接保存为文件的方法
2 将文件直接作为字符串读出的方法
3 把字符串转化为内存流的方法
4 把内存流转化为字符串的方法
代码如下:
function StringToFile(mString: string; mFileName: TFileName): Boolean;
{ 返回字符串保存到文件是否成功 }
var
vFileChar: file of Char;//通常是以行为单位,将依次写入每一行,而这里是字,则将依次写入每个字
I: Integer;
begin
{$I-}
AssignFile(vFileChar, mFileName);
Rewrite(vFileChar);
for I := 1 to Length(mString) do
Write(vFileChar, mString[I]);
CloseFile(vFileChar);
{$I+}
Result := (IOResult = 0) and (mFileName <> '');
end; { StringToFile }
function FileToString(mFileName: TFileName): string;
{ 返回从文件载入字符串 }
var
vFileChar: file of Char;
vChar: Char;
begin
Result := '';
{$I-}
AssignFile(vFileChar, mFileName);
Reset(vFileChar);
while not Eof(vFileChar) do begin
Read(vFileChar, vChar);
Result := Result + vChar;
end;
CloseFile(vFileChar);
{$I+}
end; { FileToString }
function StreamToString(mStream: TStream): string;
{ 将内存流转换成字符串 }
var
I: Integer;
begin
Result := '';
if not Assigned(mStream) then Exit;
SetLength(Result, mStream.Size);
for I := 0 to Pred(mStream.Size) do try
mStream.Position := I;
mStream.Read(Result[Succ(I)], 1);
except
Result := '';
end;
end; { StreamToString }
function StringToStream(mString: string; mStream: TStream): Boolean;
{ 返回将字符串保存到内存流是否成功 }
var
I: Integer;
begin
Result := True;
try
mStream.Size := 0;
mStream.Position := 0;
for I := 1 to Length(mString) do mStream.Write(mString[I], 1);
except
Result := False;
end;
end; { StringToStream }
————————————————
版权声明:本文为CSDN博主「Cclpy」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u011558122/article/details/52183906