I am using Delphi XE3.
In my code, I will need to create a file stream, and then write some my own data, as well as the contents of several TStringList into it. The file is in UTF-16LE format.
Therefore, my code:
FileStream := TFileStream.Create('D:\MyFile.dat', fmCreate or fmOpenWrite or fmShareExclusive);
try
// Write some data to FileStream
// Write contents of StringList1 into FileStream
StringList1.SaveToStream(FileStream, TEncoding.Unicode);
// Write some more data to FileStream
// Write contents of StringList2 into FileSteram
StringList2.SaveToStream(FileStream, TEncoding.Unicode);
finally
FileStream.Free;
end;
After executing the codes, I find a problem, each time I invoke StringList1.SaveToStream(FileStream, TEncoding.Unicode); it will write BOM (0xFFFE) then followed by the actual strings in the string list.
Therefore, I get a Unicode file like this:
0xFFFE(The first one is written by myself)
(some data)
0xFFFE (StringList1 contents)
(some data)
0xFFFE (StringList2 contents)
But this is not I expect since there should be only one 0xFFFE at the beginning of the file. Therefore, I just wonder how to prevent StringList1.SaveToStream to write the 0xFFFE BOM before writing the actual string lists?
I find another solution for my question.
TStrings has a WriteBOM property, which will control whether to write out the BOM when using SaveToStream or SaveToFile.
Therefore, using the following codes will disable the BOM:
StringList1.WriteBOM := False;
StringList1.SaveToStream(FileStream, TEncoding.Unicode);