A
Alex
Yes you can:
<html><head><script language="javascript">
SaveToFile('This is a text to save in a file', 'C:\\temp\\test.txt');
alert(read('C:\\temp\\test.txt'));
function SaveToFile (text, fileName) {
try
{netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');}
catch (e) {alert("Permission to write file denied."); return 0;}
var file =
Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
file.initWithPath(fileName);
if (!file.exists()) file.create(0x00, 0644);
var outputStream =
Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);
outputStream.init(file,0x20 | 0x02,00004,null);
outputStream.write(text, text.length);
outputStream.flush();
outputStream.close();
}
function read(myfile) {
try
{netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");}
catch (e) {alert("Permission to read file denied."); return '';}
var file =
Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
file.initWithPath(myfile);
if (!file.exists()) {alert("File not found."); return '';}
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;
}
</script></head><body></body></html>
<html><head><script language="javascript">
SaveToFile('This is a text to save in a file', 'C:\\temp\\test.txt');
alert(read('C:\\temp\\test.txt'));
function SaveToFile (text, fileName) {
try
{netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');}
catch (e) {alert("Permission to write file denied."); return 0;}
var file =
Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
file.initWithPath(fileName);
if (!file.exists()) file.create(0x00, 0644);
var outputStream =
Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);
outputStream.init(file,0x20 | 0x02,00004,null);
outputStream.write(text, text.length);
outputStream.flush();
outputStream.close();
}
function read(myfile) {
try
{netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");}
catch (e) {alert("Permission to read file denied."); return '';}
var file =
Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
file.initWithPath(myfile);
if (!file.exists()) {alert("File not found."); return '';}
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;
}
</script></head><body></body></html>