Re: Learning OS.File

[email protected] Wed, 17 Dec 2014 08:43:06 -0800 (PST)
Newsgroups gmane.comp.mozilla.devel.xpfe
Message-ID <[email protected]>
On Tuesday, December 16, 2014 8:13:17 AM UTC-7, [email protected] 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)?