开发者

Writing to file in Mozilla Firefox extension - strange error

I'm trying to develop a firefox extension. Just for beginning I'm trying to write in file but I get this very strange and inexplicable behaviour to me.

this code works:

var file = showFilePicker(window,"saveTestCaseAs",
                                  Components.interfaces.nsIFilePicker.modeSave,
                                  Format.TEST_CASE_DIRECTORY_PREF,
                                 function(fp) {return fp.file;});

var foStream = Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);
foStream.init(file1, 0x02 | 0x08 | 0x20, 438, 0);

var converter = Components.classes["@mozilla.org/intl/converter-output-stream;1"].
                    createInstance(Components.interfaces.nsIConverterOutputStream);

converter.init(foStream, "UTF-8", 0, 0);

converter.writeString("test string");

converter.close();

and this one doesn't:

var file1 = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
file1.initWithPath("C:\Documents and Settings\XPMUser\Desktop\test.t"); 

var foStream = Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);
// use 0x02 | 0x10 to open file for appending.
foStream.init(file1, 0x02 | 0x08 | 0x20, 438, 0);
var converter = Components.classes["@mozilla.org/intl/converter-output-stream;1"].
                createInstance(Components.interfaces.nsIConverterOutputStream);
converter.init(foStream, "UTF-8", 0, 0);
converter.writeStri开发者_C百科ng("test string");
converter.close(); // this closes foStream

Error message is: Error: Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIFileOutputStream.init] However this error message doesn't mean anything since this is the most generic error of all. this is very strange because the only difference is the way the file object is inilialized, but in both way initilization parameters(file name, prems, etc.) and returned object's type are exactly the same.

Will be very gratefull if anyone gives some clue about this.


@sdwilsh made very good point above. Unforunataley the problem is far more simple and event silly. And it is at the single backslashes and the too general error messages. The path I provided is with single backslashes wich are interpreted as escape symbols and actually the string is no more a valid path. Replacing '\' with '\' solves that problem. So it's very simple problem but one should really pay more attention to details.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜