write file without bom

Issue #92 new
Vasyl Zuzyak repo owner created an issue
// Writes a text to a file with UTF-8 except a BOM.
var writeFile = function(fileName, text) {
    try {
        // Writes a text to a stream with UTF-8.
        var stream = new ActiveXObject('ADODB.Stream');
        stream.Type = 2;
        stream.Charset = ENCODING;
        stream.Open();
        stream.WriteText(text);

        // Changes a stream type.
        stream.Position = 0;
        stream.Type = 1;

        // Skips a BOM and keeps a text.
        stream.Position = 3;
        var bytes = stream.Read();

        // Resets a stream and writes a text to.
        stream.Position = 0;
        stream.SetEOS();
        stream.Write(bytes);

        // Saves a stream.
        stream.SaveToFile(fileName, 2);
    } finally {
        stream.Close();
    }
};

Comments (1)

  1. Log in to comment