XUL File I/O: Write Files

Writing files in XUL isn’t that much harder than reading a file, in fact its very similar.  What a relief.  In my example I am appending the data “Appending Data” to my file.

function writeFile()
var data = “Appending Data”;

var filePath = “C:\\text.txt”;

var file = Components.classes[“@mozilla.org/file/local;1”]
var foStream = Components.classes[“@mozilla.org/network/file-output-stream;1”]
if ( file.exists() == false ) {
      alert(“File does not exist”);

foStream.init(file, 0x02 | 0x10, 00666, 0);
foStream.write(data, data.length);

file – gets the nsILocalFile interface component, making file of type nsILocalFile
foStream – gets the nsIFileOutputStream interface component, inherited from nsIOutputStream, making foStream of type nsIFileOutputStream

  • init(file, file_io_flags,file_mode_flags, behavior_flags) –
Option Type
file nsIFile
File_io_flags 0x01 Read only
0x02 Write only


Read and Write
0x08 Create File
0x10 Append
0x20 Truncate
0x40 Sync
0x80 Exclude
File_mode_flags 00400 Read by owner
00200 Write by owner
00100 Execute by owner
00040 Read by Group
00020 Write by Group
00010 Execute by Group
00004 Read by Others
00002 Write by Others
00001 Execute by Others
* Mode flags can by added to combine flag
* File_mode_flags is only applicable to UNIX based systems
Behavour_flags 0 Default

write(data, count) – sends the data string parameter to the output stream buffer, and writes the number of bytes according to count
close() – closes the stream

Not too difficult, but it is sure difficult to find the instructions and documentation

6 thoughts on “XUL File I/O: Write Files

  1. Rajesh

    Is it possible to write executable file on mac using above code.
    I have tried all the way but file gets written only in read mode.
    so how to execute file
    In unix same code works fine and writes exectable shell script
    but in Mac its not

    1. Rajesh

      I see ..’File_mode_flags is only applicable to UNIX based systems’
      then how to execute Mac file. some of them are .sh , some are .app file
      how to pass arguements

      1. Joshua

        Hey Rajesh,
        Well from what I understand Mac OS X is basically a version of UNIX, so it should follow the same format. I have an example of me creating a script for Mac here: Using Mozilla to create a DMG Part II. If your talking about making an executable program for Mac, that’s a different story. You’ll need an info.plist file (which is an xml outline for your program), then give the directory a .app extension, you shouldn’t need any arguments, unless your app requires it somehow.

        To run without arguments, all you really need is this:
        var file = Components.classes[“@mozilla.org/file/local;1”].

        var process = Components.classes[“@mozilla.org/process/util;1”]

        var args = [];

        if you have arguments, just add them as a string to the args array

  2. Rajesh


    But my requirement was little different.
    I am wirting script file .sh files
    which i execute through java code.
    To execute this script file either i need to make it 755 mode or i can pass that script file to /bin/sh as argument

    Thanks a lot


Leave a Reply

Your email address will not be published. Required fields are marked *