XPCOM FileOutptStream 附加不起作用

发布于 2024-12-09 09:06:37 字数 1136 浏览 0 评论 0原文

有没有人设法说服 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

独木成林 2024-12-16 09:06:37

根据定义,安全文件输出流无法附加到文件。它是安全的,因为它保证要么您的更改将被完整写入,要么您将得到一个例外,否则什么都不会改变。这是通过写入文件并仅在写入操作完成时重命名该文件以替换原始文件来实现的。当然,这种方法的副作用是您无法追加到文件,因为写入操作发生在不同的文件上。

在 Firefox/Gecko 7 或更高版本中,您可以使用 FileUtils。 openFileOutputStream() 打开常规文件输出流。在旧版本中,您将需要使用“传统”方法:

var fileStream = Cc["@mozilla.org/network/file-output-stream;1"].
                   createInstance(Ci.nsIFileOutputStream);
fileStream.init(file, 0x02 | 0x10 | 0x20, 0666, 0);

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:

var fileStream = Cc["@mozilla.org/network/file-output-stream;1"].
                   createInstance(Ci.nsIFileOutputStream);
fileStream.init(file, 0x02 | 0x10 | 0x20, 0666, 0);
戒ㄋ 2024-12-16 09:06:37

这样怎么样:

function readFile(savefile) {
//alert('hi');
   try {
        netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
    } catch (e) {
        alert("Permission to read file was denied.");
    }
    var file = Components.classes["@mozilla.org/file/local;1"]
        .createInstance(Components.interfaces.nsILocalFile);
    file.initWithPath( savefile );
    if ( file.exists() == false ) {
        alert("File does not exist");
    }
var data = "";
var fstream = Components.classes["@mozilla.org/network/file-input-stream;1"].
              createInstance(Components.interfaces.nsIFileInputStream);
var cstream = Components.classes["@mozilla.org/intl/converter-input-stream;1"].
              createInstance(Components.interfaces.nsIConverterInputStream);
fstream.init(file, -1, 0, 0);
cstream.init(fstream, "UTF-8", 0, 0); // you can use another encoding here if you wish

let (str = {}) {
  let read = 0;
  do {
    read = cstream.readString(0xffffffff, str); // read as much as we can and put it in str.value
    data += str.value;
  } while (read != 0);
}

你可以通过这种方法读写16/32/64位格式。

function saveFile(output, savefile)
{                                                            
    try {
        netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
    } catch (e) {
        alert("Permission to save file was denied.");
    }
var file = Components.classes["@mozilla.org/file/local;1"].
createInstance(Components.interfaces.nsILocalFile);
    file.initWithPath( savefile );
    if ( file.exists() == false ) {
        alert( "File Updated Successfully ");
        file.create( Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 420 );
    }
    var outputStream = Components.classes["@mozilla.org/network/file-output-stream;1"]
        .createInstance( Components.interfaces.nsIFileOutputStream );
    outputStream.init( file, 0x04 | 0x08 | 0x20, 420, 0 );
var converter = Components.classes["@mozilla.org/intl/converter-output-stream;1"].
                    createInstance(Components.interfaces.nsIConverterOutputStream);
    converter.init(outputStream, "UTF-8", 0, 0);
    converter.writeString(output);
    converter.close(); // this closes foStream

    outputStream.close();
//alert( "File Updated Successfully ");
}

您没有导入:resource://gre/modules/FileUtils.jsm

How about this way:

function readFile(savefile) {
//alert('hi');
   try {
        netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
    } catch (e) {
        alert("Permission to read file was denied.");
    }
    var file = Components.classes["@mozilla.org/file/local;1"]
        .createInstance(Components.interfaces.nsILocalFile);
    file.initWithPath( savefile );
    if ( file.exists() == false ) {
        alert("File does not exist");
    }
var data = "";
var fstream = Components.classes["@mozilla.org/network/file-input-stream;1"].
              createInstance(Components.interfaces.nsIFileInputStream);
var cstream = Components.classes["@mozilla.org/intl/converter-input-stream;1"].
              createInstance(Components.interfaces.nsIConverterInputStream);
fstream.init(file, -1, 0, 0);
cstream.init(fstream, "UTF-8", 0, 0); // you can use another encoding here if you wish

let (str = {}) {
  let read = 0;
  do {
    read = cstream.readString(0xffffffff, str); // read as much as we can and put it in str.value
    data += str.value;
  } while (read != 0);
}

You can read write 16/32/64 bit format by this method.

function saveFile(output, savefile)
{                                                            
    try {
        netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
    } catch (e) {
        alert("Permission to save file was denied.");
    }
var file = Components.classes["@mozilla.org/file/local;1"].
createInstance(Components.interfaces.nsILocalFile);
    file.initWithPath( savefile );
    if ( file.exists() == false ) {
        alert( "File Updated Successfully ");
        file.create( Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 420 );
    }
    var outputStream = Components.classes["@mozilla.org/network/file-output-stream;1"]
        .createInstance( Components.interfaces.nsIFileOutputStream );
    outputStream.init( file, 0x04 | 0x08 | 0x20, 420, 0 );
var converter = Components.classes["@mozilla.org/intl/converter-output-stream;1"].
                    createInstance(Components.interfaces.nsIConverterOutputStream);
    converter.init(outputStream, "UTF-8", 0, 0);
    converter.writeString(output);
    converter.close(); // this closes foStream

    outputStream.close();
//alert( "File Updated Successfully ");
}

You don't have import: resource://gre/modules/FileUtils.jsm

书间行客 2024-12-16 09:06:37

已解决:弗拉基米尔·帕兰特(Wladimir Palant)几乎是正确的,安全文件输出流无法附加。不过,我还需要从模式位中删除 0x20(截断)。

以下作品:

Components.utils.import("resource://gre/modules/FileUtils.jsm");
var Cc=Components.classes;
var Ci=Components.interfaces;

function appendFile(fileName,data){
  var file = FileUtils.getFile("Home", [fileName]);
  var fileStream = Cc["@mozilla.org/network/file-output-stream;1"].
                     createInstance(Ci.nsIFileOutputStream);
  fileStream.init(file, 0x02 | 0x10 , 0666, 0);
  fileStream.write(data,data.length);
  fileStream.close();
}

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:

Components.utils.import("resource://gre/modules/FileUtils.jsm");
var Cc=Components.classes;
var Ci=Components.interfaces;

function appendFile(fileName,data){
  var file = FileUtils.getFile("Home", [fileName]);
  var fileStream = Cc["@mozilla.org/network/file-output-stream;1"].
                     createInstance(Ci.nsIFileOutputStream);
  fileStream.init(file, 0x02 | 0x10 , 0666, 0);
  fileStream.write(data,data.length);
  fileStream.close();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文