User Tools

Site Tools


developer_center:recipe_book:extensions_and_core:writing_to_a_file

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

developer_center:recipe_book:extensions_and_core:writing_to_a_file [2013/12/28 10:40] (current)
geekshadow created
Line 1: Line 1:
 +====== Writing to a file ======
 +
 +
 +
 +Writing to local files simply utilises the existing Mozilla XPCOM interfaces for file output streams. ​ See the interfaces for [[https://​developer.mozilla.org/​en-US/​docs/​XPCOM_Interface_Reference/​nsILocalFile|nsILocalFile]] and [[https://​developer.mozilla.org/​en-US/​docs/​XPCOM_Interface_Reference/​nsIFileOutputStream|nsIFileOutputStream]] for more info.
 +
 +For the flags passed to foStream.init(),​ see [[http://​mxr.mozilla.org/​mozilla1.9.2/​source/​nsprpub/​pr/​include/​prio.h#​570|prio.h]]
 +
 +<​code>​
 +var file = Components.classes["​@mozilla.org/​file/​local;​1"​]
 +        .createInstance(Components.interfaces.nsILocalFile);​
 +file.initWithPath("/​tmp/​songbird.test"​);​
 +
 +var foStream = Components.classes["​@mozilla.org/​network/​file-output-stream;​1"​]
 +        .createInstance(Components.interfaces.nsIFileOutputStream);​
 +// 0x02 = PR_WRONLY (write only)
 +// 0x08 = PR_CREATE_FILE (create file if the file doesn'​t exist)
 +// 0x10 = PR_APPEND (append to file with each write)
 +foStream.init(file,​ 0x02 | 0x08 | 0x20, 0666, 0);
 +
 +var data = "Hello world. This is a test string.\n";​
 +foStream.write(data,​ data.length);​
 +foStream.close();​
 +</​code>​
  
developer_center/recipe_book/extensions_and_core/writing_to_a_file.txt ยท Last modified: 2013/12/28 10:40 by geekshadow