gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 06:03:26 28 4
gpt4 key购买 nike

我是新手,正在研究 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"':

这是我的读取函数,它不读取像 'é' 和 'à' 等特殊字符...我知道,我需要做一些改变,但我试过了,但还是没用。

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

最佳答案

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

关于JavaScript UTF-8 文件读写保存在 XUL 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6844265/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com