阅读:1640回复:1
老鸟请进
怎样向default目录写入文件?
我用下面的代码创建了个目录“mydir” var file = Components.classes["@mozilla.org/file/directory_service;1"]. getService(Components.interfaces.nsIProperties). get("ProfD", Components.interfaces.nsIFile); file.append("mydir"); if( !file.exists() || !file.isDirectory() ) { // if it doesn't exist, create file.create(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0777); } 然后我想在这个mydir下写文件,比如整个路径是“/profiles/xxx.default/mydir/data.txt”,怎么写呢?之前在扩展的安装目录下好办,火狐的MDC那边有直接的范例代码,我已经封装成函数,如下: writeToFile: function(strContent,strFile) { try { var MY_ID = "test@mozilla.org"; var em = Components.classes["@mozilla.org/extensions/manager;1"]. getService(Components.interfaces.nsIExtensionManager); var file = em.getInstallLocation(MY_ID).getItemFile(MY_ID, strFile);//file.path,return the file's path var foStream = Components.classes["@mozilla.org/network/file-output-stream;1"]. createInstance(Components.interfaces.nsIFileOutputStream); foStream.init(file, 0x02 | 0x08 | 0x20, 0666, 0); var converter = Components.classes["@mozilla.org/intl/converter-output-stream;1"]. createInstance(Components.interfaces.nsIConverterOutputStream); converter.init(foStream, "UTF-8", 0, 0); converter.writeString(strContent); converter.close(); return true; }catch(exc){alert(exc);alert(strFile);} },显然最关键的是“var file = em.getInstallLocation(MY_ID).getItemFile(MY_ID, strFile);”这一句来跟具体的文件相关联的,em这个对象我在MDC找了半天没找到详细的介绍页面,好像说会在ff4.0中会被替换。 有同学会吗? 相关帖子:https://forums.addons.mozilla.org/viewtopic.php?f=7&t=1344 |
|
1楼#
发布于:2010-07-06 11:10
已经被一位国际友人解决,谢谢!
|
|