title: How to use Delphi XE's TEncoding to save Cyrillic or ShiftJis text to a file?
body: <p>I'm trying to save some lines of text in a codepage different from my system's such as Cyrillic to a TFileStream using Delphi XE. However I can't find any code sample to produce those encoded file ?</p>
  
  <p>I tried using the same code as TStrings.SaveToStream however I'm not sure I implemented it correctly (the WriteBom part for example) and would like to know how it would be done elsewhere. Here is my code:</p>
  
  <pre><code>FEncoding := TEncoding.GetEncoding(1251);
  FFilePool := TObjectDictionary&lt;string,TFileStream&gt;.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) &gt; 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;
  </code></pre>
  
  <p>Thanks in advance.</p>