Learning OS.File

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Learning OS.File

reverendlinux-2
I'm starting to delve into OS.File in preparation for moving away from nsIFile in a XUL app I have. I've set up a simple test script to try to read in a small text file containing 25 lines of CSV text. However, I can't seem to get it to read the file (or at least get to the data it reads in). My test code looks like this:

Components.utils.import("resource://gre/modules/osfile.jsm");<BR>
let deskDir = OS.Constants.Path.tmpDir;<BR>
let testFile =  OS.Path.join(deskDir,"testFiles","csvTest.txt");<BR>
let csvContents = OS.File.read(testFile);

From the info on here and on the "OS.File for the main thread" page, this should read my text file into csvContents as array. However, trying to access the elements of the array give me an undefined (i.e. - alert(csvContents[0]);.

Can someone point me in the right direction?
_______________________________________________
dev-tech-xul mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-xul
Reply | Threaded
Open this post in threaded view
|

Re: Learning OS.File

Neil-4
[hidden email] wrote:

>Components.utils.import("resource://gre/modules/osfile.jsm");<BR>
>let deskDir = OS.Constants.Path.tmpDir;<BR>
>let testFile =  OS.Path.join(deskDir,"testFiles","csvTest.txt");<BR>
>let csvContents = OS.File.read(testFile);
>
>From the info on here and on the "OS.File for the main thread" page, this should read my text file into csvContents as array.
>  
>
I'm reading the same page and it says that it returns a Promise. If
however you wrap your function in a Task you can make it look like a
normal assignment:

let csvContents = yield OS.File.read(testFile);

--
Warning: May contain traces of nuts.
_______________________________________________
dev-tech-xul mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-xul
Reply | Threaded
Open this post in threaded view
|

Re: Learning OS.File

reverendlinux-2
In reply to this post by reverendlinux-2
On Tuesday, December 16, 2014 8:13:17 AM UTC-7, [hidden email] wrote:

> I'm starting to delve into OS.File in preparation for moving away from nsIFile in a XUL app I have. I've set up a simple test script to try to read in a small text file containing 25 lines of CSV text. However, I can't seem to get it to read the file (or at least get to the data it reads in). My test code looks like this:
>
> Components.utils.import("resource://gre/modules/osfile.jsm");<BR>
> let deskDir = OS.Constants.Path.tmpDir;<BR>
> let testFile =  OS.Path.join(deskDir,"testFiles","csvTest.txt");<BR>
> let csvContents = OS.File.read(testFile);
>
> From the info on here and on the "OS.File for the main thread" page, this should read my text file into csvContents as array. However, trying to access the elements of the array give me an undefined (i.e. - alert(csvContents[0]);.
>
> Can someone point me in the right direction?

Neil,

As always, thanks for the quick response.

Some progress made.  Revised test script looks like this:

Components.utils.import("resource://gre/modules/osfile.jsm");
let deskDir = OS.Constants.Path.tmpDir;
let testFile = OS.Path.join(deskDir,"testFiles","csvTest.txt");

let decoder = new TextDecoder();

let promise = OS.File.read(gradeFile);

promise = promise.then(
   function onSuccess(csvArray) {
     console.log(csvArray);
     let txtCSV = decoder.decode(csvArray);
     console.log(txtCSV);
   },
   function onFail(csvError) {
     console.log("--> Error!!!\n\n"+csvError);
   }
);

Displaying csvArray to the console now gives [object Uint8Array].  Decoding to text give the contents in a text blob.

So this brings up two questions:
1.  Why is the read operation converting the contents of the text file into 8-bit integers?
2.  Is there a way to read or decode into a standard array of elements (docs on read and encode are kind of skimpy)?
_______________________________________________
dev-tech-xul mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-xul
Reply | Threaded
Open this post in threaded view
|

Re: Learning OS.File

reverendlinux-2
In reply to this post by reverendlinux-2
On Tuesday, December 16, 2014 8:13:17 AM UTC-7, [hidden email] wrote:

> I'm starting to delve into OS.File in preparation for moving away from nsIFile in a XUL app I have. I've set up a simple test script to try to read in a small text file containing 25 lines of CSV text. However, I can't seem to get it to read the file (or at least get to the data it reads in). My test code looks like this:
>
> Components.utils.import("resource://gre/modules/osfile.jsm");<BR>
> let deskDir = OS.Constants.Path.tmpDir;<BR>
> let testFile =  OS.Path.join(deskDir,"testFiles","csvTest.txt");<BR>
> let csvContents = OS.File.read(testFile);
>
> From the info on here and on the "OS.File for the main thread" page, this should read my text file into csvContents as array. However, trying to access the elements of the array give me an undefined (i.e. - alert(csvContents[0]);.
>
> Can someone point me in the right direction?

Neil,

As always, thanks for the quick response.

Some progress made.  Revised test script looks like this:

Components.utils.import("resource://gre/modules/osfile.jsm");
let deskDir = OS.Constants.Path.tmpDir;

let testFile = OS.Path.join(deskDir,"testFiles","csvTest.txt");

let decoder = new TextDecoder();

let promise = OS.File.read(testFile);

promise = promise.then(
   function onSuccess(csvArray) {
     console.log(csvArray);
     let txtCSV = decoder.decode(csvArray);
     console.log(txtCSV);
   },
   function onFail(csvError) {
     console.log("--> Error!!!\n\n"+csvError);
   }
);

Displaying csvArray to the console now gives [object Uint8Array].  Decoding to text give the contents in a text blob.

So this brings up two questions:
1.  Why is the read operation converting the contents of the text file into 8-bit integers?
2.  Is there a way to read or decode into a standard array of elements (docs on read and encode are kind of skimpy)?
_______________________________________________
dev-tech-xul mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-xul
Reply | Threaded
Open this post in threaded view
|

Re: Learning OS.File

Neil-4
[hidden email] wrote:

>Why is the read operation converting the contents of the text file into 8-bit integers?
>  
>
Files contain 8-bit integers. Different files make use of those 8-bit
integers in different ways. Only you know what meaning to give to the
8-bit integers in the file that you're reading.

>Is there a way to read or decode into a standard array of elements (docs on read and encode are kind of skimpy)?
>  
>
If you have text data then you can skip the array and pass the encoding
as an option:
let promise = OS.File.read(testFile, {encoding: "utf-8"});
The typed array is the best choice for binary data; was there some
specific problem you were having with it?

--
Warning: May contain traces of nuts.
_______________________________________________
dev-tech-xul mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-xul
Reply | Threaded
Open this post in threaded view
|

Re: Learning OS.File

reverendlinux-2
On Thursday, December 18, 2014 2:33:47 AM UTC-7, Neil wrote:

> reverendlinux wrote:
>
> >Why is the read operation converting the contents of the text file into 8-bit integers?
> >  
> >
> Files contain 8-bit integers. Different files make use of those 8-bit
> integers in different ways. Only you know what meaning to give to the
> 8-bit integers in the file that you're reading.
>
> >Is there a way to read or decode into a standard array of elements (docs on read and encode are kind of skimpy)?
> >  
> >
> If you have text data then you can skip the array and pass the encoding
> as an option:
> let promise = OS.File.read(testFile, {encoding: "utf-8"});
> The typed array is the best choice for binary data; was there some
> specific problem you were having with it?
>
> --
> Warning: May contain traces of nuts.

On Tuesday, December 16, 2014 8:13:17 AM UTC-7, [hidden email] wrote:

> I'm starting to delve into OS.File in preparation for moving away from nsIFile in a XUL app I have. I've set up a simple test script to try to read in a small text file containing 25 lines of CSV text. However, I can't seem to get it to read the file (or at least get to the data it reads in). My test code looks like this:
>
> Components.utils.import("resource://gre/modules/osfile.jsm");<BR>
> let deskDir = OS.Constants.Path.tmpDir;<BR>
> let testFile =  OS.Path.join(deskDir,"testFiles","csvTest.txt");<BR>
> let csvContents = OS.File.read(testFile);
>
> From the info on here and on the "OS.File for the main thread" page, this should read my text file into csvContents as array. However, trying to access the elements of the array give me an undefined (i.e. - alert(csvContents[0]);.
>
> Can someone point me in the right direction?

I think my problem is that I'm so used to working with nsIFile that I'm expecting OS.File to operate in the same manner.  Specifically, nsIFile reads the file without automatically converting the data into another format.  If the source file is text, then what is read in stays as text.  With OS.File, the conversion of the text file into a typed array was throwing me off.  Experimenting, I found the solution to be to decode the typed array to a text string then split the text string at the new lines (\n).  This creates the array I wanted so that I can access the elements as needed later on.

Components.utils.import("resource://gre/modules/osfile.jsm");

let deskDir = OS.Constants.Path.tmpDir;

let testFile = OS.Path.join(deskDir,"testFiles","csvTest.txt");

let decoder = new TextDecoder();

let promise = OS.File.read(testFile);

promise = promise.then(
   function onSuccess(csvArray) {
     var line = decoder.decode(csvArray);
     var line = line.split("\n");
     console.log(csvArray);
   },
   function onFail(csvError) {
     console.log("--> Error!!!\n\n"+csvError);
   }
);

If you have a more efficient method of doing this, please(!) let me know.
_______________________________________________
dev-tech-xul mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-xul
Reply | Threaded
Open this post in threaded view
|

Re: Learning OS.File

Neil-4
[hidden email] wrote:

>I think my problem is that I'm so used to working with nsIFile that I'm expecting OS.File to operate in the same manner.  Specifically, nsIFile reads the file without automatically converting the data into another format.  If the source file is text, then what is read in stays as text.  With OS.File, the conversion of the text file into a typed array was throwing me off.
>
nsIFile doesn't actually read anything; my best guess is that you were
using one of the line input streams. I don't know of any equivalent for
OS.File.

>Experimenting, I found the solution to be to decode the typed array to a text string then split the text string at the new lines (\n).  This creates the array I wanted so that I can access the elements as needed later on.
>
>If you have a more efficient method of doing this, please(!) let me know.
>  
>
It's slightly more efficient to specify the encoding when you ask to
read the file, as then the decoding is done off-main-thread too.

--
Warning: May contain traces of nuts.
_______________________________________________
dev-tech-xul mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-xul
Reply | Threaded
Open this post in threaded view
|

Re: Learning OS.File

reverendlinux-2
On Friday, December 19, 2014 6:20:30 AM UTC-7, Neil wrote:

> reverendlinux wrote:
>
> >I think my problem is that I'm so used to working with nsIFile that I'm expecting OS.File to operate in the same manner.  Specifically, nsIFile reads the file without automatically converting the data into another format.  If the source file is text, then what is read in stays as text.  With OS.File, the conversion of the text file into a typed array was throwing me off.
> >
> nsIFile doesn't actually read anything; my best guess is that you were
> using one of the line input streams. I don't know of any equivalent for
> OS.File.
>
> >Experimenting, I found the solution to be to decode the typed array to a text string then split the text string at the new lines (\n).  This creates the array I wanted so that I can access the elements as needed later on.
> >
> >If you have a more efficient method of doing this, please(!) let me know.
> >  
> >
> It's slightly more efficient to specify the encoding when you ask to
> read the file, as then the decoding is done off-main-thread too.
>
> --
> Warning: May contain traces of nuts.

Neil,

Correct again.  I was using streams to read in files.  I need to pay more attention to my narratives.

I've got my script running well.  Appreciate the help.
_______________________________________________
dev-tech-xul mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-xul