RE: Reading/Writing from files - RESOLVED

Shibu Narayanan <[email protected]>
Newsgroups gmane.comp.mozilla.devel.xpfe
Message-ID <5a14e0f3-b9dd-44a1-9274-65b137321b89@default>
I have "assembled" code from the Mozilla website and here it is.  Hope it helps someone in the future...


function readFileContents(filePath)    {
    var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
    file.initWithPath(filePath);
    var data = "";  
    var fstream = Components.classes["@mozilla.org/network/file-input-stream;1"].createInstance(Components.interfaces.nsIFileInputStream);  
    var cstream = Components.classes["@mozilla.org/intl/converter-input-stream;1"].createInstance(Components.interfaces.nsIConverterInputStream);  
    fstream.init(file, -1, 0, 0);  
    cstream.init(fstream, "UTF-8", 0, 0); // you can use another encoding here if you wish  
      
    let (str = {}) {  
      let read = 0;  
      do {   
        read = cstream.readString(0xffffffff, str); // read as much as we can and put it in str.value  
        data += str.value;  
      } while (read != 0);  
    }  
    cstream.close(); // this closes fstream  
	return data;
}


function writeFileContents(filePath, data) {
    var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
    file.initWithPath(filePath);
    // file is nsIFile, data is a string  
    var foStream = Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);
    // use 0x02 | 0x10 to open file for appending.  
    foStream.init(file, 0x02 | 0x08 | 0x20, 0666, 0);   
    var converter = Components.classes["@mozilla.org/intl/converter-output-stream;1"].createInstance(Components.interfaces.nsIConverterOutputStream);
    converter.init(foStream, "UTF-8", 0, 0);  
    converter.writeString(data);  
    converter.close();
}




Regards
Shibu

-----Original Message-----
From: Shibu Narayanan 
Sent: Wednesday, February 22, 2012 8:48 PM
To: [email protected]
Subject: RE: Reading/Writing from files

Hi,
I see the below sample code given

Read into a stream or a string

This will allow you to read a file without locking up the UI thread.  This sample code uses NetUtil.jsm. You can pass an nsIFile object directly into NetUtil.asyncFetch:

<---- code starts here ----->
    Components.utils.import("resource://gre/modules/NetUtil.jsm");  
      
    NetUtil.asyncFetch(file, function(inputStream, status) {  
      if (!Components.isSuccessCode(status)) {  
        // Handle error!  
        return;  
      }  
      
      // The file data is contained within inputStream.  
      // You can read it into a string with  
      var data = NetUtil.readInputStreamToString(inputStream, inputStream.available());  
      alert(data);
    });  

<---- code ends here ----->

I do not understand the code.  
Where do I put the NetUtil.jsm file?  Do I create a folder by name <base_dir>/gre/modules/ ? Where do I specify the data file name in the code?
Putting this code in some event handler will show the contents of a file?

Can someone post a full method/function code which accepts a file URL and prints out the contents?  I have already downloaded NetUtil.jsm and FileUtil.jsm and added to my project.

-Shibu



-----Original Message-----
From: reverendlinux [mailto:[email protected]] 
Sent: Tuesday, February 21, 2012 10:17 PM
To: [email protected]
Subject: Re: Reading/Writing from files

On Feb 21, 7:51 am, Shibu Narayanan <[email protected]>
wrote:
> Hi,
>
> I am trying to read/write from files.  I am able to read from properties file using sample code available from the net.
>
> Is there a good way to read line by line from a file.  The content I like to read is not Key/Value pairs.
>
> -Shibu

Have you read through this: https://developer.mozilla.org/en/Code_snippets/File_I%2F%2FO

There is a sample code for reading a file line-by-line.  Although it's use is cautioned against due to performance issues, I have been using it in an app locally and have seen no problems.  Your mileage may vary.

Also, the example is for reading ASCII only.  If you are working with files containing non-ASCII characters, you might want to read through https://developer.mozilla.org/en/Reading_textual_data as well.

Hope this helps.



_______________________________________________
dev-tech-xul mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-xul
_______________________________________________
dev-tech-xul mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-xul
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.