问题
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