Javascript, write to txt file save as UNICODE

耗尽温柔 提交于 2019-12-19 11:47:14

问题


I have 2 strings. Would like it all to first make a .txt file and then save the strings to it as 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;
}

I


回答1:


Both the CreateTextFile and OpenTextFile methods have a parameter that specify the encoding (ASCII or 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();


来源:https://stackoverflow.com/questions/8802867/javascript-write-to-txt-file-save-as-unicode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!