Javascript,写入txt文件另存为UNICODE

时间:2012-01-10 11:54:49

标签: file unicode activex wsh

我有2个字符串。希望所有人首先制作一个.txt文件,然后将字符串保存为unicode。

function WriteFile(file, str, str2)
{
    var tmp=real_url.replace(/%20/g, " ");

    var WshNetwork=new ActiveXObject("WScript.Network");
    var userid=WshNetwork.UserName;

    var fso = new ActiveXObject("Scripting.FileSystemObject");

    if(! fso.FolderExists(tmp+"users/"+userid))
    {
        var cf = fso.CreateFolder(tmp+"users/"+userid);
    }
    else
    {
        //alert("THIS FOLDER ALREADY EXISTS");
    }

    delete fso;

    var fso = new ActiveXObject("Scripting.FileSystemObject");

    var fh = fso.OpenTextFile(tmp+"users/"+userid+"/"+file, 2, true);
    fh.WriteLine(str);
    fh.WriteLine(str2);

    fh.Close();
    delete fso;

    return;
}

1 个答案:

答案 0 :(得分:2)

CreateTextFileOpenTextFile方法都有一个指定编码的参数(ASCII或Unicode(UTF16LE))

var fso = new ActiveXObject("Scripting.FileSystemObject");
var filename = "c:\\testfile.txt";
var f = fso.OpenTextFile(filename, 2, true, -1); // -1 means unicode
f.WriteLine("Hello world!");
f.Close();