XPCOM FileOutptStream 附加不起作用
有没有人设法说服 XPCOM 对本地文件进行追加写入?下面的代码块在标志中使用 0x08(如果不存在则创建)可以正常工作。但是 0x10,只是追加到现有文件的末尾,平面不起作用!特权位 0666 也没有。文件始终创建为 0644。
Components.utils.import("resource://gre/modules/FileUtils.jsm");
var Cc=Components.classes;
var Ci=Components.interfaces;
function writeFile(fileName,data){
var file = FileUtils.getFile("Home", [fileName]);
var fileStream=FileUtils.openSafeFileOutputStream(file, 0x02 | 0x10 | 0x20, 0666, 0);
// ^^^^ does not work! 0x08 does.
fileStream.write(data,data.length);
FileUtils.closeSafeFileOutputStream(fileStream);
}
我尝试在不使用 FileUtils 的情况下打开流。像这样:
var fileStream = Cc["@mozilla.org/network/safe-file-output-stream;1"].
createInstance(Ci.nsIFileOutputStream);
fileStream.init(file, 0x02 | 0x10 | 0x20, 0666, 0);
结果完全相同。
我在 XULRunner 1.9.2 下运行代码,与 XUL Explorer 1.0a1pre、Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.17) Gecko/20110421
一起分发。
任何线索将不胜感激。
Has anyone managed to persuade XPCOM to do an append write to a local file? The code block below works fine with 0x08 (create if not exists) in the flags. But 0x10, append to the end of existing file just, plane don't work! Also the privilege bits 0666 dosen't. The file is always created 0644.
Components.utils.import("resource://gre/modules/FileUtils.jsm");
var Cc=Components.classes;
var Ci=Components.interfaces;
function writeFile(fileName,data){
var file = FileUtils.getFile("Home", [fileName]);
var fileStream=FileUtils.openSafeFileOutputStream(file, 0x02 | 0x10 | 0x20, 0666, 0);
// ^^^^ does not work! 0x08 does.
fileStream.write(data,data.length);
FileUtils.closeSafeFileOutputStream(fileStream);
}
I've tried opening the stream without using FileUtils. Like this:
var fileStream = Cc["@mozilla.org/network/safe-file-output-stream;1"].
createInstance(Ci.nsIFileOutputStream);
fileStream.init(file, 0x02 | 0x10 | 0x20, 0666, 0);
With exactly similar results.
I running the code under XULRunner 1.9.2 as distributed with XUL Explorer 1.0a1pre, Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.17) Gecko/20110421
.
Any clues would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据定义,安全文件输出流无法附加到文件。它是安全的,因为它保证要么您的更改将被完整写入,要么您将得到一个例外,否则什么都不会改变。这是通过写入新文件并仅在写入操作完成时重命名该文件以替换原始文件来实现的。当然,这种方法的副作用是您无法追加到文件,因为写入操作发生在不同的文件上。
在 Firefox/Gecko 7 或更高版本中,您可以使用
FileUtils。 openFileOutputStream()
打开常规文件输出流。在旧版本中,您将需要使用“传统”方法:The safe file output stream cannot append to files by definition. It is safe because it guarantees that either your changes will be written in full or you will get an exception and otherwise nothing will change. And that is implemented by writing to a new file and only renaming that file to replace the original one when the write operation is complete. Of course the side-effect of this approach is that you cannot append to a file because the write operation happens to a different file.
In Firefox/Gecko 7 or higher you can use
FileUtils.openFileOutputStream()
to open a regular file output stream. In older versions you will need to use the "conventional" approach:这样怎么样:
你可以通过这种方法读写16/32/64位格式。
您没有导入:
resource://gre/modules/FileUtils.jsm
How about this way:
You can read write 16/32/64 bit format by this method.
You don't have import:
resource://gre/modules/FileUtils.jsm
已解决:弗拉基米尔·帕兰特(Wladimir Palant)几乎是正确的,安全文件输出流无法附加。不过,我还需要从模式位中删除 0x20(截断)。
以下作品:
SOLVED: Wladimir Palant is nearly right safe-file-output-stream can't append. However I also need to drop 0x20 (truncate) from the mode bits.
The following works: