Re: Learning OS.File
[email protected] Thu, 18 Dec 2014 08:32:24 -0800 (PST)
| Newsgroups | gmane.comp.mozilla.devel.xpfe |
|---|---|
| Message-ID | <[email protected]> |
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, [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?
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.