附加数据文件与Firefox扩展 [英] Append data to file with firefox extension
问题描述
$ mydir = null;
mylog = null;
mystream = null;
函数initFolder(){
var dirSvc = Components.classes [@ mozilla.org/file/directory_service;1]
.getService(Components.interfaces.nsIProperties) ;
mydir = dirSvc.get(ProfD,Components.interfaces.nsILocalFile);
mydir.append(mylogFolder);
if(!mydir.exists())
mydir.create(mydir.DIRECTORY_TYPE,0700);
var fileName =logFile.txt;
mylog = mydir.clone();
mylog.append(fileName);
mylog.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE,0777);
函数mywriteFile(aData){
//初始化流
mystream = Components.classes [@ mozilla.org/network/safe-file-output -stream; 1\" ]。
createInstance(Components.interfaces.nsIFileOutputStream);
尝试{
mystream.init(mylog,0x02 | 0x10,0777,0); //这些标志附加文件?
} catch(e){
dump(exception:+ e +\\\
);
}
//转换为UTF-8
var converter = Components.classes [@ mozilla.org/intl/scriptableunicodeconverter]。
createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
converter.charset =UTF-8;
var convertedData = converter.ConvertFromUnicode(aData);
convertedData + = converter.Finish();
尝试{
mystream.write(convertedData,convertedData.length);
} catch(e){
dump(exception:+ e +\\\
);
$ b函数close(){
if(mystream instanceof Components.interfaces.nsISafeOutputStream){
mystream.finish();
} else {
mystream.close();
$ b window.addEventListener(load,function(){initFolder();},false);
window.addEventListener(unload,function(){close();},false);
有什么建议?
安全文件输出流安全的原因是它将数据写入一个临时文件,并在您调用stream.finish()时仅将其复制到实际文件中。所以任何现有的数据都会丢失。如果你想追加你将不得不使用一个不同的组件(普通的老@ mozilla.org /网络/文件输出流; 1应该正常工作)。
I'm trying to build a firefox extension and i want to write things periodically in a file. So i want a file in which i append new strings. The following code writes the file but at the end the file contains only the last string i wrote and not the previous.
Can you help me?
mydir=null;
mylog=null;
mystream=null;
function initFolder() {
var dirSvc = Components.classes["@mozilla.org/file/directory_service;1"]
.getService(Components.interfaces.nsIProperties);
mydir = dirSvc.get("ProfD", Components.interfaces.nsILocalFile);
mydir.append("mylogFolder");
if (!mydir.exists())
mydir.create(mydir.DIRECTORY_TYPE, 0700);
var fileName = "logFile.txt";
mylog = mydir.clone();
mylog.append(fileName);
mylog.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0777);
}
function mywriteFile(aData) {
// init stream
mystream = Components.classes["@mozilla.org/network/safe-file-output-stream;1"].
createInstance(Components.interfaces.nsIFileOutputStream);
try {
mystream.init(mylog, 0x02 | 0x10, 0777, 0); //these flags to append file?
} catch (e) {
dump("exception: " + e + "\n");
}
// convert to UTF-8
var converter = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"].
createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
converter.charset = "UTF-8";
var convertedData = converter.ConvertFromUnicode(aData);
convertedData += converter.Finish();
try {
mystream.write(convertedData, convertedData.length);
} catch (e) {
dump("exception: " + e + "\n");
}
}
function close() {
if (mystream instanceof Components.interfaces.nsISafeOutputStream) {
mystream.finish();
} else {
mystream.close();
}
}
window.addEventListener("load", function(){ initFolder(); }, false);
window.addEventListener("unload", function(){close(); }, false);
Any suggestions?
The reason the "safe" file output stream is safe is that it writes the data to a temporary file and only copies it over to the actual file when you call stream.finish(). So any existing data is lost. If you want to append you'll have to use a different component (plain old "@mozilla.org/network/file-output-stream;1" should work fine).
这篇关于附加数据文件与Firefox扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!