file IO - Mozilla

This is a discussion on file IO - Mozilla ; When my extension is executing it is needed to write some text to a text file (in my extension folder). To do it I wrote following code. But the problem is when I execute it within a . js file ...

+ Reply to Thread
Results 1 to 2 of 2

Thread: file IO

  1. file IO

    When my extension is executing it is needed to write some text to a
    text file (in my extension folder). To do it I wrote following code.
    But the problem is when I execute it within a .js file it is working
    properly. But it doesn’t work when it is being executed as a
    JavaScript code embedded in a .xhtml file which is located in a same
    directory with the .js file. Where have I done a mistake?

    The code I wrote as follow.

    var id = "sep@uom.lk";
    var ext = Components.classes["@mozilla.org/extensions/manager;
    1"].getService(Components.interfaces.nsIExtensionMana ger).
    getInstallLocation(id).getItemLocation(id);
    var file = Components.classes["@mozilla.org/
    file/local;1"].createInstance(Components.interfaces.nsILocalFile );
    file.initWithPath(ext.path);
    file.append("whitelist.txt");
    var foStream = Components.classes
    ["@mozilla.org/network/file-output-stream;1"].createInstance
    (Components.interfaces.nsIFileOutputStream);
    foStream.init(file, 0x02 | 0x10, 0664, 0); //
    write, create, truncate
    foStream.write(data + ";" + "\r\n", data.length
    +1);
    foStream.close();




  2. Re: file IO

    On Dec 9, 5:46*am, aruna wrote:
    > When my extension is executing it is needed to write some text to a
    > text file (in my extension folder). *To do it I wrote following code.
    > But the problem is when I execute it within a .js file it is working
    > properly. But it doesn’t work when it is being executed as a
    > JavaScript code embedded in a .xhtml file which is located in a same
    > directory with the .js file. *Where have I done a mistake?
    >
    > The code I wrote as follow.
    >
    > var id = "s...@uom.lk";
    > var ext = Components.classes["@mozilla.org/extensions/manager;
    > 1"].getService(Components.interfaces.nsIExtensionMana ger).
    > * * * * getInstallLocation(id).getItemLocation(id);
    > * * * * * * * * * * * * var file = Components.classes["@mozilla.org/
    > file/local;1"].createInstance(Components.interfaces.nsILocalFile );
    > * * * * * * * * * * * * file.initWithPath(ext.path);
    > * * * * * * * * * * * * file.append("whitelist.txt");
    > * * * * * * * * * * * * var foStream = Components.classes
    > ["@mozilla.org/network/file-output-stream;1"].createInstance
    > (Components.interfaces.nsIFileOutputStream);
    > * * * * * * * * * * * * foStream.init(file, 0x02 | 0x10, 0664, 0); //
    > write, create, truncate
    > * * * * * * * * * * * * foStream.write(data + ";"+ "\r\n", data.length
    > +1);
    > * * * * * * * * * * * * foStream.close();


    Could be a permissions problem, try wrapping the code in try catch
    blocks and looking at the error.

    try
    {

    var id = "s...@uom.lk";
    var ext = Components.classes["@mozilla.org/extensions/manager;
    1"].getService(Components.interfaces.nsIExtensionMana ger).
    getInstallLocation(id).getItemLocation(id);
    var file = Components.classes["@mozilla.org/
    file/local;1"].createInstance(Components.interfaces.nsILocalFile );
    file.initWithPath(ext.path);
    file.append("whitelist.txt");
    var foStream = Components.classes
    ["@mozilla.org/network/file-output-stream;1"].createInstance
    (Components.interfaces.nsIFileOutputStream);
    foStream.init(file, 0x02 | 0x10, 0664, 0); //
    write, create, truncate
    foStream.write(data + ";" + "\r\n",
    data.length
    +1);
    foStream.close();
    }
    catch (e)
    {
    dump(e);
    throw e;
    }

    start mozilla with -console extension to see the console where dump
    outputs to.
    (Does anything appear in the error console already?)

+ Reply to Thread