Compare Revisions

File I/O

Revision 39494:

Revision 39494 by sdwilsh on

Revision 39495:

Revision 39495 by sdwilsh on

Title:
File I/O
File I/O
Slug:
Code_snippets/File_I//O
Code_snippets/File_I//O
Tags:
Extensions, Add-ons, Files, Streams, "Code snippets"
Extensions, Add-ons, Files, Streams, "Code snippets"
Content:

Revision 39494
Revision 39495
tt494    <h4>
495      Asynchronously
496    </h4>
497    <p>
498      This sample uses <a href="/en/JavaScript_code_modules/NetUt
 >il.jsm" title="en/JavaScript_code_modules/NetUtil.jsm">NetUtil.js
 >m</a>
499    </p>
500    <pre class="brush: js">
501// file is nsIFile, data is a string
502var otream = Components.classes["@mozilla.org/network/file-output
 >-stream;1"].
503             createInstance(Components.interfaces.nsIFileOutputSt
 >ream);
504 
505// use 0x02 | 0x10 to open file for appending.
506otream.init(file, 0x02 | 0x08 | 0x20, 0666, ostream.DEFER_OPEN); 
507// write, create, truncate
508 
509var converter = Components.classes["@mozilla.org/intl/scriptableu
 >nicodeconverter"].
510                createInstance(Components.interfaces.nsIScriptabl
 >eUnicodeConverter);
511converter.charset = "UTF-8";
512var istream = converter.convertToInputStream(data);
513Components.utils.import("resource://gre/modules/NetUtil.jsm");
514NetUtil.asyncCopy(istream, ostream);
515</pre>

Back to History