我正在尝试使用Delphi XE将一些文本行保存到与我的系统不同的代码页,例如Cyrillic到TFileStream中。但是我找不到任何可以生成那些编码文件的代码示例?
我尝试使用与TStrings.SaveToStream相同的代码,但我不确定我是否正确实现了它(例如WriteBom部分),想知道其他人如何实现。这是我的代码:
FEncoding := TEncoding.GetEncoding(1251);
FFilePool := TObjectDictionary<string,TFileStream>.Create([doOwnsValues]);
//...
procedure WriteToFile(const aFile, aText: string);
var
Preamble, Buffer: TBytes;
begin
// Create the file if it doesn't exist
if not FFilePool.ContainsKey(aFile) then
begin
// Create the file
FFilePool.Add(aFile, TFileStream.Create(aFile, fmCreate));
// Write the BOM
Preamble := FEncoding.GetPreamble;
if Length(Preamble) > 0 then
FFilePool[aFile].WriteBuffer(Preamble[0], Length(Preamble));
end;
// Write to the file
Buffer := FEncoding.GetBytes(aText);
FFilePool[aFile].WriteBuffer(Buffer[0], Length(Buffer));
end;
提前感谢你。