NS_ERROR_ILLEGAL_VALUE尝试使用xul读取本地.text文件时出错

时间:2012-11-26 12:10:31

标签: javascript firefox firefox-addon xul xulrunner

我正在尝试使用下面的xul程序在windows xp中读取本地存储的文本文件:

function read_text_file(file_path)
{
  var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile); 
  file.initWithPath(file_path); 
  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);
  }
  cstream.close(); // this closes fstream
  return data;
}

但是读取= cstream.readString(0xffffffff,str);

Error: NS_ERROR_ILLEGAL_INPUT: Component returned failure code: 0x8050000e (NS_ERROR_ILLEGAL_INPUT) [nsIConverterInputStream.readString]
Source File: chrome://quicknote/content/overlay.js
Line: 168

here I found这里有一些描述,但没有帮助。 enter image description here

2 个答案:

答案 0 :(得分:1)

这里是答案:Reading textual data

答案 1 :(得分:1)

最可能的原因是该文件中的数据实际上并不是以UTF-8编码的,因此尝试解码为UTF-8最终会抛出。