User Tools

Site Tools


developer_center:recipe_book:extensions_and_core:writing_to_a_file

Writing to a file

Writing to local files simply utilises the existing Mozilla XPCOM interfaces for file output streams. See the interfaces for nsILocalFile and nsIFileOutputStream for more info.

For the flags passed to foStream.init(), see prio.h

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();
developer_center/recipe_book/extensions_and_core/writing_to_a_file.txt · Last modified: 2013/12/28 10:40 by geekshadow