写文件
// path格式也许为:"c://xxx.txt" function write(aStr,path) { var file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile); file.QueryInterface(Ci.nsIFile); file.initWithPath(path ); var strm = Cc["@mozilla.org/network/file-output-stream;1"].createInstance(Ci.nsIFileOutputStream); strm.QueryInterface(Ci.nsIOutputStream); strm.QueryInterface(Ci.nsISeekableStream); strm.init( file, 0x02 | 0x08 | 0x20, 0664, 0 ); /* netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); var converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(Ci.nsIScriptableUnicodeConverter); converter.charset = 'UTF-8'; aStr= converter.ConvertFromUnicode(aStr); */ strm.write( aStr, aStr.length); strm.flush(); strm.close(); } 读文件
// path格式也许为:"c://xxx.txt" function readFile(path) { try { var inputFile = this._getLocalFile(path); var stream = Cc['@mozilla.org/network/file-input-stream;1'].createInstance(Ci.nsIFileInputStream); stream.init(inputFile, -1, 0, 0); var sStream = Cc['@mozilla.org/scriptableinputstream;1'].createInstance(Ci.nsIScriptableInputStream); sStream.init(stream); var fileContents = sStream.read(sStream.available()); sStream.close(); stream.close(); return fileContents; } catch (e) {} }