我正在尝试使用 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;
提前致谢。