Reputation: 1939
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
}
Upvotes: 2
Views: 2189
Reputation: 4537
Search for nsIConverterInputStream and nsIConverterOutputStream in the document you linked to.
Upvotes: 1