简体   繁体   English

Firefox扩展:使用OS.File.jsm进行文件读/写

[英]Firefox Extension: File Read/Write using OS.File.jsm

From the File I/O snippets 文件I / O片段

Use of OS.File is preferred over following the examples in this article. 使用OS.File比遵循本文中的示例更受欢迎 Only use these legacy interfaces if OS.File is not available to you. 如果您无法使用OS.File,则仅使用这些旧版接口。

Now I've got a few questions... 现在我有几个问题......

From OS.File for the main thread OS.File为主线程

Example: Read the contents of a file as text 示例: 以文本形式读取文件的内容

This example requires Firefox 18 or a more recent version. 此示例需要Firefox 18或更新版本。 ... ...

Example: Write a string to a file 示例: 将字符串写入文件

These examples require Firefox 19 or a more recent version. 这些示例需要Firefox 19或更新版本。

Is that to say FF 18 only supports reading and FF19 supports both reading and writing? 是说FF 18只支持阅读,FF19支持读写吗?

Furthermore, from Recent changes to OS.File 此外,从最近的更改到OS.File

To write a string, you can now pass the string directly to writeAtomic: 要编写字符串,您现在可以将字符串直接传递给writeAtomic:

OS.File.writeAtomic(path, "Here is a string", { encoding: "utf-8"})

Similarly, you can now read strings from read: 同样,您现在可以从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). 这样做至少和你自己调用TextEncoder / TextDecoder一样快(见下文)。

I have tested above (without TextEncoder / TextDecoder ) on FF30 and it works fine... but which versions is it available on? 我已经在FF30上面进行了测试(没有TextEncoder / TextDecoder )并且它工作正常......但它可以使用哪些版本?

Finally, NetUtil.jsm/FileUtils will create a file if it does not exist when using FileUtils.openSafeFileOutputStream(file) . 最后,如果NetUtil.jsm/FileUtils在使用FileUtils.openSafeFileOutputStream(file)时不存在,则会创建一个文件。

How does file creation work on OS.File ? 文件创建如何在OS.FileOS.File Is it automatically created? 它是自动创建的吗? Does it require a if(!OS.File.exists(path)) and then how to create one? 它是否需要if(!OS.File.exists(path))然后如何创建一个?

The documentation for the recommended method is very limited and examples are hard to come by. 推荐方法的文档非常有限,很难得到示例。

Is that to say FF 18 only supports reading and FF19 supports both reading and writing? 是说FF 18只支持阅读,FF19支持读写吗?

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 . Firefox 18确实支持写入某些扩展IIRC,但API改变了19,所以你所指的例子只适用于19.不是它真的应该关注你... Firefox 18和19是长期结束-life因此不支持已知的安全问题,用户应该尽快升级

I have tested above (without TextEncoder/TextDecoder) on FF30 and it works fine ...but the question is which versions is it available on? 我已经在FF30上面测试了(没有TextEncoder / TextDecoder)并且它工作得很好......但问题是它可以使用哪些版本?

writeAtomic with an encoding option is available since Firefox 22, while read with an encoding flag is available since Firefox 30. 自Firefox 22以来,带有encoding选项的writeAtomic可用,而自Firefox 30起, read使用encoding标志read

How does file creation work on OS.file? 文件创建如何在OS.file上运行? Is it automatically created? 它是自动创建的吗? Does it require a if(!OS.File.exists(path)) and then how to create one? 它是否需要if(!OS.File.exists(path))然后如何创建一个?

OS.File.writeAtomic will create the file if it doesn't exist already. 如果文件不存在, OS.File.writeAtomic将创建该文件。 Similarly OS.File.open in write mode will also create a file, except when you specify existing: true in the options. 类似地,写入模式下的OS.File.open也会创建一个文件,除非在选项中指定existing: true

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: 或者Task.jsm风格:

try {
  var data = yield OS.File.read("/somefile");
}
catch (ex if ex.becauseNoSuchFile) {
  // File does not exist.
}
catch (ex) {
  // Some other error
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM