Open SourceProgrammingXRap

XUL File I/O: Write Files

By November 26, 2008August 21st, 20136 Comments

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”] .createInstance(Components.interfaces.nsILocalFile);
var foStream = Components.classes[“@mozilla.org/network/file-output-stream;1”]                          .createInstance(Components.interfaces.nsIFileOutputStream);
file.initWithPath(filePath);
if ( file.exists() == false ) {
      alert(“File does not exist”);
}

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

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) –
OptionType
filensIFile
File_io_flags0x01Read only
0x02Write only

0x04

Read and Write
0x08Create File
0x10Append
0x20Truncate
0x40Sync
0x80Exclude
File_mode_flags00400Read by owner
00200Write by owner
00100Execute by owner
00040Read by Group
00020Write by Group
00010Execute by Group
00004Read by Others
00002Write by Others
00001Execute by Others
* Mode flags can by added to combine flag
* File_mode_flags is only applicable to UNIX based systems
Behavour_flags0Default

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 Comments

  • kerry says:

    great stuff….very informative…thanks…

  • Rajesh says:

    Hi
    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

    • Rajesh says:

      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

      • Joshua says:

        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”].
        createInstance(Components.interfaces.nsILocalFile);

        file.initWithPath(applicationPath);
        var process = Components.classes[“@mozilla.org/process/util;1”]
        .createInstance(Components.interfaces.nsIProcess);

        process.init(file);
        var args = [];
        process.run(true,args,args.length);

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

  • Rajesh says:

    Thanks.

    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

  • Karlas says:

    Thanks.
    Useful example, its helps me

Leave a Reply