I am trying to save some lines of text in an encoding different from my system, such as Cyrillic, to TFileStream using Delphi XE. However, I can not find a sample code to create an encoded file?
I tried to use the same code as TStrings.SaveToStream, but I'm not sure that I implemented it correctly (for example, the WriteBom part) and would like to know how it will be done elsewhere. Here is my code:
FEncoding := TEncoding.GetEncoding(1251);
FFilePool := TObjectDictionary<string,TFileStream>.Create([doOwnsValues]);
procedure WriteToFile(const aFile, aText: string);
var
Preamble, Buffer: TBytes;
begin
if not FFilePool.ContainsKey(aFile) then
begin
FFilePool.Add(aFile, TFileStream.Create(aFile, fmCreate));
Preamble := FEncoding.GetPreamble;
if Length(Preamble) > 0 then
FFilePool[aFile].WriteBuffer(Preamble[0], Length(Preamble));
end;
Buffer := FEncoding.GetBytes(aText);
FFilePool[aFile].WriteBuffer(Buffer[0], Length(Buffer));
end;
Thanks in advance.
source
share