简体   繁体   English

JavaScript UTF-8文件读写保存在XUL中

[英]JavaScript UTF-8 File read write save in XUL

I'm a newbie and I'm working on XUL. 我是新手,正在研究XUL。 I have a script, which I can read write and save my XML file in XUL. 我有一个脚本,可以读写并将其XML文件保存在XUL中。 If i use special characters like 'é' or 'è' exactly French letters my XUL file couldn't load the XML file because of the different characters. 如果我使用特殊的字符(如“é”或“è”)恰好是法语字母,则由于字符不同,我的XUL文件无法加载XML文件。

1.How can I change my Java-script to read/write/save file UTF-8 encoding? 1.如何更改Java脚本以读取/写入/保存文件UTF-8编码?

Here is the script: 这是脚本:

//Reading the file as a plain xml file.
  function readFile(savefile) {
    try {
        netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
    } catch (e) {
        alert("Permission to read file was denied.");
    }
    var file = Components.classes["@mozilla.org/file/local;1"]
        .createInstance(Components.interfaces.nsILocalFile);
    file.initWithPath( savefile );
    if ( file.exists() == false ) {
        alert("File does not exist");
    }
    var is = Components.classes["@mozilla.org/network/file-input-stream;1"]
        .createInstance( Components.interfaces.nsIFileInputStream );
    is.init( file,0x01, 00004, null);
    var sis = Components.classes["@mozilla.org/scriptableinputstream;1"]
        .createInstance( Components.interfaces.nsIScriptableInputStream );
    sis.init( is );
    var output = sis.read( sis.available() );
    return output;
}

function saveFile(output, savefile) {

    try {
        netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
    } catch (e) {
        alert("Permission to save file was denied.");
    }
    var file = Components.classes["@mozilla.org/file/local;1"]
        .createInstance(Components.interfaces.nsILocalFile);
    file.initWithPath( savefile );
    if ( file.exists() == false ) {
        alert( "File Updated Successfully ");
        file.create( Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 420 );
    }
    var outputStream = Components.classes["@mozilla.org/network/file-output-stream;1"]
        .createInstance( Components.interfaces.nsIFileOutputStream );
    outputStream.init( file, 0x04 | 0x08 | 0x20, 420, 0 );
    var result = outputStream.write( output, output.length );
    outputStream.close();
//alert( "File Updated Successfully ");
clear();
reload();
}

I tried to use the script from this website, I couldn't understand https://developer.mozilla.org/en-US/docs/Code_snippets/File_I_O . 我尝试使用此网站上的脚本,但无法理解https://developer.mozilla.org/en-US/docs/Code_snippets/File_I_O I feel the above script is really simple to read/write/save any file as a plain text. 我觉得上面的脚本非常容易读取/写入/保存为纯文本文件。

Please help me to change the above script to read/write/save file which has 'encoding="UTF-8"' 请帮助我将上述脚本更改为读/写/保存具有“ encoding =“ UTF-8”“的文件

From the above link, I have managed to write the XML file in 'encoding="UTF-8"'. 通过上面的链接,我设法将XML文件写入'encoding =“ UTF-8”'。

This is my file writing script: it works perfectly! 这是我的文件编写脚本:完美运行!

function saveFile(output, savefile) {

    try {
        netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
    } catch (e) {
        alert("Permission to save file was denied.");
    }
    var file = Components.classes["@mozilla.org/file/local;1"]
        .createInstance(Components.interfaces.nsILocalFile);
    file.initWithPath( savefile );
    if ( file.exists() == false ) {
        alert( "File Updated Successfully ");
        file.create( Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 420 );
    }
    var outputStream = Components.classes["@mozilla.org/network/file-output-stream;1"]
        .createInstance( Components.interfaces.nsIFileOutputStream );
    outputStream.init( file, 0x04 | 0x08 | 0x20, 420, 0 );
var converter = Components.classes["@mozilla.org/intl/converter-output-stream;1"].
                    createInstance(Components.interfaces.nsIConverterOutputStream);
    converter.init(outputStream, "UTF-8", 0, 0);
    converter.writeString(output);
    converter.close(); // this closes foStream

    outputStream.close();
alert( "File Updated Successfully ");
clear();
reload(); 

My problem here to read the 'encoding="UTF-8"' in JavaScript: 我的问题是在JavaScript中读取“ encoding =“ UTF-8””:

This is my read function, which is not reading the special characters like 'é' & 'à' etc... I know, i need to do some change but i tried but still it's not working. 这是我的读取功能,不会读取诸如'é'和'à'等特殊字符...我知道,我需要进行一些更改,但我尝试过但仍无法正常工作。

//Reading the file as a plain xml file.
  function readFile(savefile) {
    try {
        netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
    } catch (e) {
        alert("Permission to read file was denied.");
    }
    var file = Components.classes["@mozilla.org/file/local;1"]
        .createInstance(Components.interfaces.nsILocalFile);
    file.initWithPath( savefile );
    if ( file.exists() == false ) {
        alert("File does not exist");
    }
   // var data = "";
    var is = Components.classes["@mozilla.org/network/file-input-stream;1"]
        .createInstance( Components.interfaces.nsIFileInputStream );
    is.init(file, -1, 0, 0);
    var sis = Components.classes["@mozilla.org/scriptableinputstream;1"]
        .createInstance( Components.interfaces.nsIScriptableInputStream );
    sis.init( is, "UTF-8", 0, 0);
    var output = sis.read(0xffffffff, sis.available() );
    return output;
 }

The script mentioned below is the one to read the 'encoding="UTF-8"' file and it's from Mozilla website. 下面提到的脚本是从Mozilla网站读取“ encoding =“ UTF-8””文件的脚本。

/*///////////////////////////////////////////////
// |file| is nsIFile
var data = "";
var fstream = Components.classes["@mozilla.org/network/file-input-stream;1"].
              createInstance(Components.interfaces.nsIFileInputStream);
var cstream = Components.classes["@mozilla.org/intl/converter-input-stream;1"].
              createInstance(Components.interfaces.nsIConverterInputStream);
fstream.init(file, -1, 0, 0);
cstream.init(fstream, "UTF-8", 0, 0); // you can use another encoding here if you wish

let (str = {}) {
  let read = 0;
  do {
    read = cstream.readString(0xffffffff, str); // read as much as we can and put it in str.value
    data += str.value;
  } while (read != 0);
}
cstream.close(); // this closes fstream

alert(data);
*///////////////////////////////////////////////

I need some help in changing my original read script file to read the 'encoding="UTF-8"' format. 在更改原始的读取脚本文件以读取'encoding =“ UTF-8”'格式时,我需要一些帮助。

Note: I have updated my original question in the same thread. 注意:我已经在同一主题中更新了原始问题。 Thank you very much. 非常感谢你。

I'm updating the readfile function in this thread: 我正在此线程中更新readfile函数:

function readFile(savefile) { 
   try { 
        netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); 
    } catch (e) { 
        alert("Permission to read file was denied."); 
    } 
    var file = Components.classes["@mozilla.org/file/local;1"] 
        .createInstance(Components.interfaces.nsILocalFile); 
    file.initWithPath( savefile ); 
    if ( file.exists() == false ) { 
        alert("File does not exist"); 
    }  
var data = ""; 
var fstream = Components.classes["@mozilla.org/network/file-input-stream;1"]. 
              createInstance(Components.interfaces.nsIFileInputStream); 
var cstream = Components.classes["@mozilla.org/intl/converter-input-stream;1"]. 
              createInstance(Components.interfaces.nsIConverterInputStream); 
fstream.init(file, -1, 0, 0); 
cstream.init(fstream, "UTF-8", 0, 0); // you can use another encoding here if you wish 

let (str = {}) { 
  let read = 0; 
  do { 
    read = cstream.readString(0xffffffff, str); // read as much as we can and put it in str.value 
    data += str.value; 
  } while (read != 0); 
} 
var output= data; 
return output; 
cstream.close(); // this closes fstream 
}

在链接到的文档中搜索nsIConverterInputStream和nsIConverterOutputStream。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM