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

发布于 2024-11-26 11:42:51 字数 6782 浏览 1 评论 0原文

我是一名新手,正在开发 XUL。我有一个脚本,我可以读写它并将我的 XML 文件保存在 XUL 中。如果我使用像“é”或“è”这样的特殊字符(完全是法语字母),我的 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“”:

这是我的读取函数,它不读取“é”等特殊字符。 ‘à’等等... 我知道,我需要做一些改变,但我尝试过,但仍然不起作用。

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

I'm a newbie and I'm working on XUL. I have a script, which I can read write and save my XML file in 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.

1.How can I change my Java-script to read/write/save file UTF-8 encoding?

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. 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"'

From the above link, I have managed to write the XML file in '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:

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.

/*///////////////////////////////////////////////
// |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.

Note: I have updated my original question in the same thread. Thank you very much.

I'm updating the readfile function in this thread:

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 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清引 2024-12-03 11:42:51

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

Search for nsIConverterInputStream and nsIConverterOutputStream in the document you linked to.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文