Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 517275 of io/text-streams

  • Revision slug: Mozilla/Add-ons/SDK/Low-Level_APIs/io_text-streams
  • Revision title: io/text-streams
  • Revision id: 517275
  • Created:
  • Creator: wbamberg
  • Is current revision? No
  • Comment

Revision Content

Experimental

Provides streams for reading and writing text.

function readTextFromFile(filename) {
  var fileIO = require("sdk/io/file");
  var text = null;
  if (fileIO.exists(filename)) {
    var TextReader = fileIO.open(filename, "r");
    if (!TextReader.closed) {
      text = TextReader.read();
      TextReader.close();
    }
  }
  return text;
}
function writeTextToFile(text, filename) {
  var fileIO = require("sdk/io/file");
  var TextWriter = fileIO.open(filename, "w");
  if (!TextWriter.closed) {
    TextWriter.write(text);
    TextWriter.close();
  }
}

Globals

Constructors

TextReader(inputStream, charset)

Creates a buffered input stream that reads text from a backing stream using a given text encoding.

You can also create TextReader objects using io/file's open() function.

Parameters

inputStream : stream
The backing stream, an nsIInputStream. It must already be opened.

charset : string
inputStream is expected to be in the character encoding named by this value. If not specified, "UTF-8" is assumed. See nsICharsetConverterManager.idl for documentation on how to determine other valid values for this.

TextWriter(outputStream, charset)

Creates a buffered output stream that writes text to a backing stream using a given text encoding.

You can also create TextWriter objects using io/file's open() function.

Parameters

outputStream : stream
The backing stream, an nsIOutputStream. It must already be opened.

charset : string
Text will be written to outputStream using the character encoding named by this value. If not specified, "UTF-8" is assumed. See nsICharsetConverterManager.idl for documentation on how to determine other valid values for this.

TextReader

Methods

close()

Closes both the stream and its backing stream.

read(numChars)

Reads and returns a string from the stream. If the stream is closed, an exception is thrown.

Parameters

numChars : number
The number of characters to read. If not given, the remainder of the stream is read.

Returns

string : The string read. If the stream is at the end, the empty string is returned.

Properties

closed

True if the stream is closed.

Sample

function readTextFromFile (filename) {

  var fileIO = require("sdk/io/file");

  var text = null;

  if (fileIO.exists(filename)) {
    var TextReader = fileIO.open(filename, "r");
    if (!TextReader.closed) {
      text = TextReader.read();
      TextReader.close();
    }
  }

  return text;
};

TextWriter

Methods

close()

Flushes the backing stream's buffer and closes both the stream and the backing stream. If the stream is already closed, an exception is thrown.

flush()

Flushes the backing stream's buffer.

write(str)

Writes a string to the stream. If the stream is closed, an exception is thrown.

Parameters

str : string
The string to write.

writeAsync(str, callback)

Writes a string on a background thread. After the write completes, the backing stream's buffer is flushed, and both the stream and the backing stream are closed, also on the background thread. If the stream is already closed, an exception is thrown immediately.

Parameters

str : string
The string to write.

callback : callback
callback, if given, must be a function. It's called as callback(error) when the write completes. error is an Error object or undefined if there was no error. Inside callback, this is the TextWriter object.

Properties

closed

True if the stream is closed.

Sample

function writeTextToFile (text, filename) {

  var fileIO = require("sdk/io/file");

  var TextWriter = fileIO.open(filename, "w");
  if (!TextWriter.closed) {
    TextWriter.write(text);
    TextWriter.close();
  }

};

Revision Source

<div class="note">
 <p>Experimental</p>
</div>
<p><span class="seoSummary">Provides streams for reading and writing text.</span></p>
<pre class="brush: js">
function readTextFromFile(filename) {
  var fileIO = require("sdk/io/file");
  var text = null;
  if (fileIO.exists(filename)) {
    var TextReader = fileIO.open(filename, "r");
    if (!TextReader.closed) {
      text = TextReader.read();
      TextReader.close();
    }
  }
  return text;
}</pre>
<pre class="brush: js">
function writeTextToFile(text, filename) {
  var fileIO = require("sdk/io/file");
  var TextWriter = fileIO.open(filename, "w");
  if (!TextWriter.closed) {
    TextWriter.write(text);
    TextWriter.close();
  }
}</pre>
<h2 id="Globals">Globals</h2>
<h3 id="Constructors">Constructors</h3>
<h4 class="addon-sdk-api-name" id="TextReader(inputStream.2C_charset)"><code>TextReader(inputStream, charset)</code></h4>
<p>Creates a buffered input stream that reads text from a backing stream using a given text encoding.</p>
<p>You can also create TextReader objects using <a href="/en-US/Add-ons/SDK/Low-Level_APIs/io_file#open(path.2C_mode)">io/file's <code>open()</code> function</a>.</p>
<h5 id="Parameters">Parameters</h5>
<p><strong>inputStream : stream</strong><br />
 The backing stream, an <a href="https://mxr.mozilla.org/mozilla-central/source/xpcom/io/nsIInputStream.idl"><code>nsIInputStream</code></a>. It must already be opened.</p>
<p><strong>charset : string</strong><br />
 <code>inputStream</code> is expected to be in the character encoding named by this value. If not specified, "UTF-8" is assumed. See <a href="https://mxr.mozilla.org/mozilla-central/source/intl/uconv/idl/nsICharsetConverterManager.idl"><code>nsICharsetConverterManager.idl</code></a> for documentation on how to determine other valid values for this.</p>
<h4 class="addon-sdk-api-name" id="TextWriter(outputStream.2C_charset)"><code>TextWriter(outputStream, charset)</code></h4>
<p>Creates a buffered output stream that writes text to a backing stream using a given text encoding.</p>
<p>You can also create TextWriter objects using <a href="/en-US/Add-ons/SDK/Low-Level_APIs/io_file#open(path.2C_mode)">io/file's <code>open()</code> function</a>.</p>
<h5 id="Parameters">Parameters</h5>
<p><strong>outputStream : stream</strong><br />
 The backing stream, an <a href="https://mxr.mozilla.org/mozilla-central/source/xpcom/io/nsIOutputStream.idl"><code>nsIOutputStream</code></a>. It must already be opened.</p>
<p><strong>charset : string</strong><br />
 Text will be written to <code>outputStream</code> using the character encoding named by this value. If not specified, "UTF-8" is assumed. See <a href="https://mxr.mozilla.org/mozilla-central/source/intl/uconv/idl/nsICharsetConverterManager.idl"><code>nsICharsetConverterManager.idl</code></a> for documentation on how to determine other valid values for this.</p>
<h2 id="TextReader">TextReader</h2>
<h3 id="Methods">Methods</h3>
<h4 class="addon-sdk-api-name" id="close()"><code>close()</code></h4>
<p>Closes both the stream and its backing stream.</p>
<h4 class="addon-sdk-api-name" id="read(numChars)"><code>read(numChars)</code></h4>
<p>Reads and returns a string from the stream. If the stream is closed, an exception is thrown.</p>
<h5 id="Parameters">Parameters</h5>
<p><strong>numChars : number</strong><br />
 The number of characters to read. If not given, the remainder of the stream is read.</p>
<h5 id="Returns">Returns</h5>
<p><strong>string</strong> : The string read. If the stream is at the end, the empty string is returned.</p>
<h3 id="Properties">Properties</h3>
<h4 class="addon-sdk-api-name" id="closed"><code>closed</code></h4>
<p>True if the stream is closed.</p>
<h3 id="Sample">Sample</h3>
<pre class="brush: js">
function readTextFromFile (filename) {

  var fileIO = require("sdk/io/file");

  var text = null;

  if (fileIO.exists(filename)) {
    var TextReader = fileIO.open(filename, "r");
    if (!TextReader.closed) {
      text = TextReader.read();
      TextReader.close();
    }
  }

  return text;
};</pre>
<h2 id="TextWriter">TextWriter</h2>
<h3 id="Methods">Methods</h3>
<h4 class="addon-sdk-api-name" id="close()"><code>close()</code></h4>
<p>Flushes the backing stream's buffer and closes both the stream and the backing stream. If the stream is already closed, an exception is thrown.</p>
<h4 class="addon-sdk-api-name" id="flush()"><code>flush()</code></h4>
<p>Flushes the backing stream's buffer.</p>
<h4 class="addon-sdk-api-name" id="write(str)"><code>write(str)</code></h4>
<p>Writes a string to the stream. If the stream is closed, an exception is thrown.</p>
<h5 id="Parameters">Parameters</h5>
<p><strong>str : string</strong><br />
 The string to write.</p>
<h4 class="addon-sdk-api-name" id="writeAsync(str.2C_callback)"><code>writeAsync(str, callback)</code></h4>
<p>Writes a string on a background thread. After the write completes, the backing stream's buffer is flushed, and both the stream and the backing stream are closed, also on the background thread. If the stream is already closed, an exception is thrown immediately.</p>
<h5 id="Parameters">Parameters</h5>
<p><strong>str : string</strong><br />
 The string to write.</p>
<p><strong>callback : callback</strong><br />
 <em><code>callback</code></em>, if given, must be a function. It's called as <code>callback(error)</code> when the write completes. <code>error</code> is an <code>Error</code> object or undefined if there was no error. Inside <em><code>callback</code></em>, <code>this</code> is the <code>TextWriter</code> object.</p>
<h3 id="Properties">Properties</h3>
<h4 class="addon-sdk-api-name" id="closed"><code>closed</code></h4>
<p>True if the stream is closed.</p>
<h3 id="Sample">Sample</h3>
<pre class="brush: js">
function writeTextToFile (text, filename) {

  var fileIO = require("sdk/io/file");

  var TextWriter = fileIO.open(filename, "w");
  if (!TextWriter.closed) {
    TextWriter.write(text);
    TextWriter.close();
  }

};</pre>
Revert to this revision