RE: Reading/Writing from files
Shibu Narayanan <[email protected]>
| Newsgroups | gmane.comp.mozilla.devel.xpfe |
|---|---|
| Message-ID | <3bb4b065-78d5-4720-b36c-3c83fe0746ef@default> |
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