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

时间:2011-07-27 12:26:50

标签: javascript xml utf-8 xul

我是新手,我正在研究XUL。我有一个脚本,我可以读写并在XUL中保存我的XML文件。如果我使用像'é'或'è'这样的特殊字符,那么我的XUL文件由于字符不同而无法加载XML文件。

1.如何将我的Java脚本更改为读/写/保存文件UTF-8编码?

这是脚本:

//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();
}

我尝试使用本网站的脚本,我无法理解https://developer.mozilla.org/en-US/docs/Code_snippets/File_I_O。我觉得上面的脚本非常简单,可以将任何文件作为纯文本读取/写入/保存。

请帮我改变上面的脚本来读/写/保存'encoding =“UTF-8”'

的文件

从上面的链接中,我设法用'encoding =“UTF-8”'编写XML文件。

这是我的文件编写脚本:它完美无缺!

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(); 

我的问题是在JavaScript中阅读'encoding =“UTF-8”:

这是我的阅读功能,它不会读取像'é'和amp;这样的特殊字符。 'à'等...... 我知道,我需要做一些改变,但我尝试但仍然无法正常工作。

//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;
 }

下面提到的脚本是读取'encoding =“UTF-8”'文件的脚本,它来自Mozilla网站。

/*///////////////////////////////////////////////
// |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);
*///////////////////////////////////////////////

我需要一些帮助来更改我的原始读取脚本文件以读取'encoding =“UTF-8”'格式。

注意:我已在同一个帖子中更新了原始问题。非常感谢你。

我正在更新此线程中的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 
}

1 个答案:

答案 0 :(得分:1)

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