appending to a file - Mozilla

This is a discussion on appending to a file - Mozilla ; Hello all :-), My extension efforts have hit another bump.. and this time I can't seem to append data to a file! I am able to create the file and write something in it the first time round, but the ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: appending to a file

  1. appending to a file

    Hello all :-),
    My extension efforts have hit another bump.. and this
    time I can't seem to append data to a file! I am able to create the
    file and write something in it the first time round, but the second/
    third time I try to write something, nothing happens. I am using code
    similar to the File I/O code on MDC and have a small question:

    Do I need something like
    mycachefile.createUnique(Components.interfaces.nsI File.NORMAL_FILE_TYPE,
    0666); the second time I access the file?

    I am using the following code to create the file
    Code:
    mycachefile.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE,
    0666);
    appendtocache(mycachefile, cshorturl, cfullurl, analysis);
    
    function appendtocache(mycachefile, cshorturl, cfullurl, analysis){
    //package data to push in file
    var date1 = new Date() ;
    var pushdata = cshorturl + " " + cfullurl + " " + analysis + " " +
    date1.getTime() + "@ ";
    var cacheStream = Components.classes["@mozilla.org/network/file-
    output-stream;
    1"].createInstance(Components.interfaces.nsIFileOutputStream);
    cacheStream.init(mycachefile, 0x02 | 0x10 | 0x20, 0666, 0);
    cacheStream.write(pushdata, pushdata.length);
    cacheStream.close();
    }
    this creates the file and writes the data in it, the second/third time
    round I use
    Code:
    var date2 = new Date() ;
    var pushdata2 = cshorturl + " " + cfullurl + " " + analysis + " " +
    date2.getTime() + "@ ";
    //
    mycachefile.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE,
    0666);
    var cacheStream2 = Components.classes["@mozilla.org/network/file-
    output-stream;
    1"].createInstance(Components.interfaces.nsIFileOutputStream);
    cacheStream2.init(mycachefile, 0x02 | 0x10 | 0x20, 0666, 0);
    cacheStream2.write(pushdata, pushdata.length);
    cacheStream2.close();
    If I use
    mycachefile.createUnique(Components.interfaces.nsI File.NORMAL_FILE_TYPE,
    0666);
    for the second/third time new "empty" files with a -1/-2/-3/ appended
    at the end of the file name are created nothing is written in them.

    If someone can spot the obvious mistake in the code, could you please
    point me in the right direction. I have searched for similar posts and
    did come across one but it was related to issues with jslim.

    Thanks in advance,
    -A

  2. Re: appending to a file

    Hi you don't need createUnique the second time you access the file, actually
    you don't need create at all. and when you do
    acheStream2.init(mycachefile, 0x02 | 0x10 | 0x20, 0666, 0);
    use the flag :0x10 instead of 0x20. You can see the flags here:
    http://mxr.mozilla.org/mozilla/sourc...include/prio.h
    These are the mistakes i can spot now. Incidentally i needed similar thing
    half an hour ago so here is the code i came up with:



    > function GetFile(path) {
    > var file = Components.classes["@mozilla.org/file/local;1
    > "].createInstance();
    > if (file) file.QueryInterface(Components.interfaces.nsILocal File);
    > file.initWithPath(path);
    > return file;
    > }




    > function dump(str) {
    > var file = 'c:\\dump.txt';
    > var afile = GetFile(file);
    > var charset = "UTF-8";
    > var os = Components.classes["@
    > mozilla.org/intl/converter-output-stream;1"]
    >
    > .createInstance(Components.interfaces.nsIConverter OutputStream);
    > // file is nsIFile, data is a string
    > var fos = Components.classes["@
    > mozilla.org/network/file-output-stream;1"]
    >
    > .createInstance(Components.interfaces.nsIFileOutpu tStream);
    > // fos.init(afile, 0x02 | 0x08 | 0x20, 0666, 0); // this will truncate
    > the file
    > fos.init(afile, 0x02 | 0x10 | 0x08, 0666, 0); // this will append to
    > the file
    > os.init(fos, charset, 0, 0x0000);
    > os.writeString(str);
    > os.close();
    > }



    I hope that helped

  3. Re: appending to a file

    On May 11, 12:12*am, "Alexander Jordanov" wrote:
    > Hi you don't need createUnique the second time you access the file, actually
    > you don't need create at all. and when you do
    > acheStream2.init(mycachefile, 0x02 | 0x10 | 0x20, 0666, 0);
    > use the flag :0x10 instead of 0x20. You can see the flags here:http://mxr.mozilla.org/mozilla/sourc...include/prio.h
    > These are the mistakes i can spot now. Incidentally i needed similar thing
    > half an hour ago so here is the code i came up with:
    >
    >
    >
    >
    >
    > > function GetFile(path) {
    > > * * var file = Components.classes["@mozilla.org/file/local;1
    > > "].createInstance();
    > > * * if (file) file.QueryInterface(Components.interfaces.nsILocal File);
    > > * * file.initWithPath(path);
    > > * * return file;
    > > }
    > > function dump(str) {
    > > * * var file = 'c:\\dump.txt';
    > > * * var afile = GetFile(file);
    > > * * var charset = "UTF-8";
    > > * * var os = Components.classes["@
    > > mozilla.org/intl/converter-output-stream;1"]

    >
    > > .createInstance(Components.interfaces.nsIConverter OutputStream);
    > > * * // file is nsIFile, data is a string
    > > * * var fos = Components.classes["@
    > > mozilla.org/network/file-output-stream;1"]

    >
    > > .createInstance(Components.interfaces.nsIFileOutpu tStream);
    > > * * // fos.init(afile, 0x02 | 0x08 | 0x20, 0666, 0); // this will truncate
    > > the file
    > > * * fos.init(afile, 0x02 | 0x10 | 0x08, 0666, 0); // this will append to
    > > the file
    > > * * os.init(fos, charset, 0, 0x0000);
    > > * * os.writeString(str);
    > > * * os.close();
    > > }

    >
    > I hope that helped- Hide quoted text -
    >
    > - Show quoted text -


    Thanks, it works now :-)

+ Reply to Thread