Reputation: 7721
From the File I/O snippets
Use of OS.File is preferred over following the examples in this article. Only use these legacy interfaces if OS.File is not available to you.
Now I've got a few questions...
From OS.File for the main thread
Example: Read the contents of a file as text
This example requires Firefox 18 or a more recent version. ...
Example: Write a string to a file
These examples require Firefox 19 or a more recent version.
Is that to say FF 18 only supports reading and FF19 supports both reading and writing?
Furthermore, from Recent changes to OS.File
To write a string, you can now pass the string directly to writeAtomic:
OS.File.writeAtomic(path, "Here is a string", { encoding: "utf-8"})
Similarly, you can now read strings from read:
OS.File.read(path, { encoding: "utf-8" } );
// Resolves to a string.Doing this is at least as fast as calling TextEncoder/TextDecoder yourself (see below).
I have tested above (without TextEncoder
/TextDecoder
) on FF30 and it works fine... but which versions is it available on?
Finally, NetUtil.jsm/FileUtils
will create a file if it does not exist when using FileUtils.openSafeFileOutputStream(file)
.
How does file creation work on OS.File
? Is it automatically created? Does it require a if(!OS.File.exists(path))
and then how to create one?
The documentation for the recommended method is very limited and examples are hard to come by.
Upvotes: 3
Views: 2626
Reputation: 33162
Is that to say FF 18 only supports reading and FF19 supports both reading and writing?
Yes and no. Firefox 18 did support writing to some extend IIRC, but the API changed in 19 so that the example you're referring to does only apply to 19. Not that it should really concern you... Firefox 18 and 19 are long end-of-life and therefore unsupported with known security issues and users should really upgrade ASAP.
I have tested above (without TextEncoder/TextDecoder) on FF30 and it works fine ...but the question is which versions is it available on?
writeAtomic
with an encoding
option is available since Firefox 22, while read
with an encoding
flag is available since Firefox 30.
How does file creation work on OS.file? Is it automatically created? Does it require a if(!OS.File.exists(path)) and then how to create one?
OS.File.writeAtomic
will create the file if it doesn't exist already. Similarly OS.File.open
in write mode will also create a file, except when you specify existing: true
in the options.
You cannot read from a file that does not exist. An error will be raised.
OS.File.read("/somefile").then(function(data) {
// do something with the retrieved data;
}, function(ex) {
if (ex.becauseNoSuchFile) {
// File does not exist);
}
else {
// Some other error
}
});
Or Task.jsm
-style:
try {
var data = yield OS.File.read("/somefile");
}
catch (ex if ex.becauseNoSuchFile) {
// File does not exist.
}
catch (ex) {
// Some other error
}
Upvotes: 4