<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Yukun&#039;s Blog &#187; AIR</title>
	<atom:link href="http://www.yukun.info/blog/tag/air/feed" rel="self" type="application/rss+xml" />
	<link>http://www.yukun.info</link>
	<description>難しいことは分かりやすく、簡単なことは面白く紹介</description>
	<lastBuildDate>Thu, 26 Jan 2012 03:33:59 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>AIR: Webサーバ、Socketの接続状況を検知 &#8211; URLMonitor、SocketMonitorクラス</title>
		<link>http://www.yukun.info/blog/2008/12/actionscript-air-url-socket-monitor.html</link>
		<comments>http://www.yukun.info/blog/2008/12/actionscript-air-url-socket-monitor.html#comments</comments>
		<pubDate>Wed, 24 Dec 2008 14:45:08 +0000</pubDate>
		<dc:creator>yukun</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[AIR]]></category>
		<category><![CDATA[GUI]]></category>
		<category><![CDATA[Network]]></category>
		<category><![CDATA[Socket]]></category>

		<guid isPermaLink="false">http://www.yukun.info/?p=1282</guid>
		<description><![CDATA[任意のアドレスのWebサイト[サービス]のネットワーク状況を検知するURLMonitorと、任意のサーバ＋ポートに接続可能か否かを検知するSocketMonitorクラスの動作サンプルを下記に示します。 ソースコード &#038; &#8230; <a href="http://www.yukun.info/blog/2008/12/actionscript-air-url-socket-monitor.html">Continue reading <span class="meta-nav">&#8594;</span></a><p><a href="http://www.yukun.info/blog/2008/12/actionscript-air-url-socket-monitor.html">AIR: Webサーバ、Socketの接続状況を検知 &#8211; URLMonitor、SocketMonitorクラス</a> is a post from: <a href="http://www.yukun.info">Yukun&#039;s Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p>任意のアドレスのWebサイト[サービス]のネットワーク状況を検知するURLMonitorと、任意のサーバ＋ポートに接続可能か否かを検知するSocketMonitorクラスの動作サンプルを下記に示します。</p>
<h2>ソースコード</h2>
<pre>
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;mx:WindowedApplication xmlns:mx=&quot;http://www.adobe.com/2006/mxml&quot; layout=&quot;absolute&quot;
  applicationComplete=&quot;startup()&quot;&gt;
  &lt;mx:Script&gt;
    &lt;![CDATA[
    import air.net.SocketMonitor
    import air.net.URLMonitor;
    import flash.net.URLRequest;
    import flash.events.StatusEvent;

    private var SERVER_URL:String = &quot;http://www.yukun.info/&quot;;
    private var SOCK_ADRR:String = &quot;yukun.info&quot;;
    private var PORT:int = 6667;
    private var INTERVAL_TIME:int = 3000; // ms
    private var serviceMonitor:URLMonitor = null;
    private var socketMonitor:SocketMonitor = null;

    private function startup():void {
      var endpoint:URLRequest = new URLRequest(SERVER_URL);
      serviceMonitor = new URLMonitor(endpoint);
      serviceMonitor.addEventListener(StatusEvent.STATUS, onStatusEvent);
      serviceMonitor.pollInterval = INTERVAL_TIME;
      serviceMonitor.start();

      socketMonitor = new SocketMonitor(SOCK_ADRR, PORT);
      socketMonitor.addEventListener(StatusEvent.STATUS, onSocketStatusChange);
      socketMonitor.pollInterval = INTERVAL_TIME;
      socketMonitor.start();
    }

    // ネットワークサービスの状態の検知
    private function onStatusEvent(e:StatusEvent):void {
      var date:Date = new Date();
      trace(date.toLocaleTimeString());
      trace(SERVER_URL + &quot;に&quot; + (serviceMonitor.available ? &quot;接続可&quot; : &quot;切断中&quot;));
    }

    private function onSocketStatusChange(e:StatusEvent):void {
      trace(SOCK_ADRR + &quot;のポート&quot; + PORT + &quot;は&quot; +
        (socketMonitor.available ? &quot;接続可&quot; : &quot;切断中&quot;));
    }
    ]]&gt;
  &lt;/mx:Script&gt;
&lt;/mx:WindowedApplication&gt;
</pre>
<h2>実行結果</h2>
<pre>

http://www.yukun.info/に接続可

yukun.infoのポート6667は切断中
</pre>
<p>URLMonitorはネットワーク状況を検知する為にサーバへGETリクエストを送出して、レスポンスのステイタスコードを確認して判断しているようです↓。</p>
<h3>リファレンス</h3>
<ul>
<li><a href="http://livedocs.adobe.com/flex/3_jp/langref/air/net/URLMonitor.html" title="URLMonitor - ActionScript 3.0 言語およびコンポーネントリファレンス" target="_blank">URLMonitor &#8211; ActionScript 3.0 言語およびコンポーネントリファレンス</a></li>
<li><a href="http://help.adobe.com/ja_JP/AIR/1.1/devappsflash/WS5b3ccc516d4fbf351e63e3d118666ade46-7fcc.html" title="Adobe AIR 1.1 * ネットワーク接続の監視" target="_blank">Adobe AIR 1.1 * ネットワーク接続の監視</a></li>
</ul>
<h4>関連すると思われる記事：</h4>
<ul class="similar-posts">
<li><a href="http://www.yukun.info/blog/2010/05/java-networkinterface-ipv6-ipv4.html" rel="bookmark" title="2010年5月16日">Java: インターフェースとローカルのIPv6, IPv4アドレスの取得 &#8211; NetworkInterfaceクラス</a></li>
<li><a href="http://www.yukun.info/blog/2008/12/air-actionscript-sqlite-statement.html" rel="bookmark" title="2008年12月22日">AIR: SQLiteでデータの挿入と検索 &#8211; SQLConnection、SQLStatementクラス</a></li>
<li><a href="http://www.yukun.info/blog/2008/12/actionscript-air-async-write-text.html" rel="bookmark" title="2008年12月18日">AIR: テキストファイルに書き込み &#8211; openAsync()、writeMultiByte()</a></li>
<li><a href="http://www.yukun.info/blog/2008/12/actionscript-air-async-read-text.html" rel="bookmark" title="2008年12月17日">AIR: テキストファイルを非同期に読み込む &#8211; openAsync()、readMultiByte()</a></li>
<li><a href="http://www.yukun.info/blog/2011/02/java-tcp-socket-echo.html" rel="bookmark" title="2011年2月12日">Java: TCP Socket Echo Server/Client サンプル</a></li>
</ul>
<p><!-- Similar Posts took 9.941 ms --></p>
<p><a href="http://www.yukun.info/blog/2008/12/actionscript-air-url-socket-monitor.html">AIR: Webサーバ、Socketの接続状況を検知 &#8211; URLMonitor、SocketMonitorクラス</a> is a post from: <a href="http://www.yukun.info">Yukun&#039;s Blog</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.yukun.info/blog/2008/12/actionscript-air-url-socket-monitor.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>AIR: SQLiteでデータの挿入と検索 &#8211; SQLConnection、SQLStatementクラス</title>
		<link>http://www.yukun.info/blog/2008/12/air-actionscript-sqlite-statement.html</link>
		<comments>http://www.yukun.info/blog/2008/12/air-actionscript-sqlite-statement.html#comments</comments>
		<pubDate>Mon, 22 Dec 2008 09:00:30 +0000</pubDate>
		<dc:creator>yukun</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[AIR]]></category>
		<category><![CDATA[GUI]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.yukun.info/?p=1278</guid>
		<description><![CDATA[AIRアプリケーションからSQLiteのDBにアクセスするには主にSQLConnectionとSQLStatementクラスを用います。基本的な処理の流れは、 SQLConnection#openAsync(＜Fileオ &#8230; <a href="http://www.yukun.info/blog/2008/12/air-actionscript-sqlite-statement.html">Continue reading <span class="meta-nav">&#8594;</span></a><p><a href="http://www.yukun.info/blog/2008/12/air-actionscript-sqlite-statement.html">AIR: SQLiteでデータの挿入と検索 &#8211; SQLConnection、SQLStatementクラス</a> is a post from: <a href="http://www.yukun.info">Yukun&#039;s Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p>AIRアプリケーションからSQLiteのDBにアクセスするには主にSQLConnectionとSQLStatementクラスを用います。基本的な処理の流れは、</p>
<ol>
<li>SQLConnection#openAsync(＜Fileオブジェクト＞, ＜モード＞)でDBに接続</li>
<li>SQLStatement#textプロパティにSQL文を代入しexecute()で実行</li>
<li>その際、フィールド値はSQLStatement#parameters["@＜定義されたパラメータ＞"]で代入する</li>
<li>SELECT文の検索結果データははSQLStatement#getResult()で取得</li>
<li>結果データの型はSQLResultで、データそのものはdataプロパティ(Array型)に入っている。e.g. ＜SQLResult＞.data[i].＜フィールド名＞</li>
</ol>
<p>下のプログラムはDBに接続してテーブルの作成、レコードの追加、検索を行うサンプルです。</p>
<h2>ソースコード</h2>
<pre>
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;mx:WindowedApplication xmlns:mx=&quot;http://www.adobe.com/2006/mxml&quot;
layout=&quot;absolute&quot; creationComplete=&quot;dbConnect()&quot;&gt;
&lt;mx:Script&gt;
  &lt;![CDATA[
  import flash.data.SQLConnection;
  import flash.filesystem.File;
  import flash.events.SQLErrorEvent;
  import flash.events.SQLEvent;

  private var conn:SQLConnection;
  private var dbFile:File;
  private var sql:SQLStatement;

  // DBに接続
  private function dbConnect():void {
    conn = new SQLConnection();
    conn.addEventListener(SQLEvent.OPEN, onDBOpen);
    conn.addEventListener(SQLErrorEvent.ERROR, onDBError);
    dbFile = File.desktopDirectory.resolvePath(&quot;test01.db&quot;);
    trace(&quot;フィアルは存在&quot; + (dbFile.exists ? &quot;します。：&quot; : &quot;しません。&quot;) + dbFile.nativePath);
    conn.openAsync(dbFile, SQLMode.CREATE);
  }

  // 接続完了
  private function onDBOpen(e:SQLEvent):void {
    trace(&quot;Connect DB&quot;);
    conn.removeEventListener(SQLEvent.OPEN, onDBOpen);
    conn.removeEventListener(SQLErrorEvent.ERROR, onDBError);
    createTable(); // テーブルを作成
  }

  private function onDBError(e:SQLErrorEvent):void {
    trace(&quot;Can&#039;t connect DB: &quot; + e.error.message);
    trace(&quot;Error detals: &quot; + e.error.details);
  }

  // テーブルを作成する関数
  private function createTable():void {
    sql = new SQLStatement();
    sql.sqlConnection = conn;
    var sqlTxt:String = &quot;CREATE TABLE IF NOT EXISTS addresses &quot; +
        &quot;(id INTEGER PRIMARY KEY, name TEXT, age INTEGER)&quot;;
    sql.text = sqlTxt;
    sql.addEventListener(SQLEvent.RESULT, onCreate);
    sql.addEventListener(SQLErrorEvent.ERROR, onCreateError);
    sql.execute();
  }

  // テーブル作成完了
  private function onCreate(e:SQLEvent):void {
    trace(&quot;Can create table&quot;);
    sql.removeEventListener(SQLEvent.RESULT, onCreate);
    sql.removeEventListener(SQLErrorEvent.ERROR, onCreateError);
    insertData(); // データを挿入
  }

  private function onCreateError(e:SQLErrorEvent):void {
    trace(&quot;Can&#039;t create table: &quot; + e.error.message);
    trace(&quot;Error details: &quot; + e.error.details);
  }

  private function insertData():void {
    sql = new SQLStatement();
    sql.sqlConnection = conn;
    var sqlTxt:String = &quot;INSERT INTO addresses (id, name, age) &quot; +
      &quot;VALUES (@id, @name, @age)&quot;;
    sql.text = sqlTxt;
    sql.parameters[&quot;@id&quot;] = 1;
    sql.parameters[&quot;@name&quot;] = &quot;名無し&quot;;
    sql.parameters[&quot;@age&quot;] = 18;
    sql.addEventListener(SQLEvent.RESULT, onInsert);
    sql.addEventListener(SQLErrorEvent.ERROR, onInsertError);
    sql.execute();
  }

  // データ挿入完了
  private function onInsert(e:SQLEvent):void {
    trace(&quot;Can insert data&quot;);
    sql.removeEventListener(SQLEvent.RESULT, onInsert);
    sql.removeEventListener(SQLErrorEvent.ERROR, onInsertError);
    selectData(); // データを検索
  }

  private function onInsertError(e:SQLErrorEvent):void {
    trace(&quot;Can&#039;t insert data: &quot; + e.error.message);
    trace(&quot;Error details: &quot; + e.error.details);
  }

  private function selectData():void {
    sql = new SQLStatement();
    sql.sqlConnection = conn;
    var sqlTxt:String = &quot;SELECT * FROM addresses&quot;;
    sql.text = sqlTxt;
    sql.addEventListener(SQLEvent.RESULT, onSelect);
    sql.addEventListener(SQLErrorEvent.ERROR, onSelectError);
    sql.execute();
  }

  // 検索結果を取得完了
  private function onSelect(e:SQLEvent):void {
    trace(&quot;Can select data&quot;);
    sql.removeEventListener(SQLEvent.RESULT, onSelect);
    sql.removeEventListener(SQLErrorEvent.ERROR, onSelectError);
    var result:SQLResult = sql.getResult();
    var num:int = result.data.length;
    var id:int;
    var name:String;
    var age:String;

    for (var i:int = 0; i &lt; num; i++) {
      var row:Object = result.data[i];
      id = row.id;
      name = row.name;
      age = row.age;
      var str:String = &quot;id=&quot; + id + &quot;, name=&quot; + name + &quot;, age=&quot; + age;
      trace(str);
    }
  }

  private function onSelectError(e:SQLErrorEvent):void {
    trace(&quot;Can&#039;t select data: &quot; + e.error.message);
    trace(&quot;Error details: &quot; + e.error.details);
  }

  ]]&gt;
&lt;/mx:Script&gt;

&lt;/mx:WindowedApplication&gt;
</pre>
<h2>実行結果</h2>
<pre>
フィアルは存在しません。/Users/yukun/Desktop/test01.db
Connect DB
Can create table
Can insert data
Can select data
id=1, name=名無し, age=18
</pre>
<h3>リファレンス</h3>
<ul>
<li><a href="http://help.adobe.com/ja_JP/AIR/1.5/devappshtml/WS5b3ccc516d4fbf351e63e3d118676a5497-7fb4.html" title="Adobe AIR 1.5 * ローカル SQL データベースの操作" target="_blank">Adobe AIR 1.5 * ローカル SQL データベースの操作</a></li>
<li><a href="http://help.adobe.com/ja_JP/AIR/1.5/devappshtml/WS5b3ccc516d4fbf351e63e3d118666ade46-7d32.html" title="Adobe AIR 1.5 * データベースへの接続" target="_blank">Adobe AIR 1.5 * データベースへの接続</a></li>
</ul>
<h4>関連すると思われる記事：</h4>
<ul class="similar-posts">
<li><a href="http://www.yukun.info/blog/2008/12/actionscript-air-async-read-text.html" rel="bookmark" title="2008年12月17日">AIR: テキストファイルを非同期に読み込む &#8211; openAsync()、readMultiByte()</a></li>
<li><a href="http://www.yukun.info/blog/2008/12/actionscript-air-async-write-text.html" rel="bookmark" title="2008年12月18日">AIR: テキストファイルに書き込み &#8211; openAsync()、writeMultiByte()</a></li>
<li><a href="http://www.yukun.info/blog/2008/12/actionscript-air-url-socket-monitor.html" rel="bookmark" title="2008年12月24日">AIR: Webサーバ、Socketの接続状況を検知 &#8211; URLMonitor、SocketMonitorクラス</a></li>
<li><a href="http://www.yukun.info/blog/2008/11/mysql-query-select-where-in-like-between.html" rel="bookmark" title="2008年11月13日">MySQL: データ検索クエリの基本 &#8211; SELECT文、WHERE句、LIKE、IN、BETWEENキーワード</a></li>
<li><a href="http://www.yukun.info/blog/2008/07/python-sqlite-insert.html" rel="bookmark" title="2008年7月14日">Python: SQLiteにデータを格納、検索、出力 &#8211; pysqlite</a></li>
</ul>
<p><!-- Similar Posts took 10.055 ms --></p>
<p><a href="http://www.yukun.info/blog/2008/12/air-actionscript-sqlite-statement.html">AIR: SQLiteでデータの挿入と検索 &#8211; SQLConnection、SQLStatementクラス</a> is a post from: <a href="http://www.yukun.info">Yukun&#039;s Blog</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.yukun.info/blog/2008/12/air-actionscript-sqlite-statement.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>AIR: テキストファイルに書き込み &#8211; openAsync()、writeMultiByte()</title>
		<link>http://www.yukun.info/blog/2008/12/actionscript-air-async-write-text.html</link>
		<comments>http://www.yukun.info/blog/2008/12/actionscript-air-async-write-text.html#comments</comments>
		<pubDate>Wed, 17 Dec 2008 15:00:26 +0000</pubDate>
		<dc:creator>yukun</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[AIR]]></category>
		<category><![CDATA[File]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[GUI]]></category>
		<category><![CDATA[write]]></category>

		<guid isPermaLink="false">http://www.yukun.info/?p=1272</guid>
		<description><![CDATA[AIRコンポーネントではローカルのファイルにアクセスすることができます。下記のコードは日本語を含むマルチバイトの文字列をテキストファイルに書き込む処理をします。 処理の手順 FileStream#openAsync()か &#8230; <a href="http://www.yukun.info/blog/2008/12/actionscript-air-async-write-text.html">Continue reading <span class="meta-nav">&#8594;</span></a><p><a href="http://www.yukun.info/blog/2008/12/actionscript-air-async-write-text.html">AIR: テキストファイルに書き込み &#8211; openAsync()、writeMultiByte()</a> is a post from: <a href="http://www.yukun.info">Yukun&#039;s Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.yukun.info/wp-content/uploads/WriteTxtFile01_result.png"><img src="http://www.yukun.info/wp-content/uploads/WriteTxtFile01_result-e1273382752288.png" alt="AIR: テキストファイルに書き込み" title="WriteTxtFile01_result" width="400" height="328" class="size-full wp-image-1533" /></a></p>
<p>AIRコンポーネントではローカルのファイルにアクセスすることができます。下記のコードは日本語を含むマルチバイトの文字列をテキストファイルに書き込む処理をします。</p>
<h2>処理の手順</h2>
<ol>
<li>FileStream#openAsync()かopen()メソッドの引数にFileインスタンスとFileModeのプロパティを設定して実ファイルのパイプに接続</li>
<li>FileStream#writeMultiByte()でファイルに書き込み</li>
<li>FileStream#close()でストリームを閉じる</li>
</ol>
<h3>ソースコード</h3>
<pre>
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;mx:WindowedApplication xmlns:mx=&quot;http://www.adobe.com/2006/mxml&quot; layout=&quot;absolute&quot; title=&quot;シンプルテキストメイカー&quot;&gt;
&lt;mx:Script&gt;
  &lt;![CDATA[
  import mx.controls.Alert;

  private var choDir:File = File.documentsDirectory; // ダイアログの初期ディレクトリ
  private var saveFile:File;
  private var stream:FileStream;

  private function onSaveFileBut():void {
    choDir.addEventListener(Event.SELECT, onSelectSaveFile);
    choDir.browseForSave(&quot;テキストファイルに保存&quot;);
  }

  private function onSelectSaveFile(e:Event):void {
    saveFile = e.target as File; // 選択されたファイル
    choDir.removeEventListener(Event.SELECT, onSelectSaveFile);
    try {
      stream = new FileStream();
      stream.addEventListener(IOErrorEvent.IO_ERROR, onIOErrorWriteFile);
      stream.openAsync(saveFile, FileMode.WRITE); // 書き込みmodeで開く(フツーのopen()でもOK)
      var str:String = txtArea_.text;
      // 改行文字と文字コードをOS標準のものに置き換えて書き込み
      str = str.replace(/\n/g, File.lineEnding);
      stream.writeMultiByte(str, File.systemCharset); // 実際に書き込み
    } catch (err:IOError) {
      progLab_.text = &quot;IOError : &quot; + err;
    } finally {
      if (stream != null) {
        stream.close();
      }
    }
  }

  // ファイル書き込みに失敗した場合
  private function onIOErrorWriteFile(e:IOErrorEvent):void {
    Alert.show(&quot;ファイルの書き込みに失敗&quot;, &quot;エラー&quot;, Alert.OK, this);
    if (stream != null) {
      stream.close();
    }
  }
  ]]&gt;
&lt;/mx:Script&gt;
  &lt;mx:VBox x=&quot;0&quot; y=&quot;0&quot; height=&quot;100%&quot; width=&quot;100%&quot;&gt;
    &lt;mx:HBox width=&quot;100%&quot;&gt;
      &lt;mx:Button label=&quot;ファイルに保存&quot; id=&quot;saveBut_&quot; click=&quot;onSaveFileBut();&quot;/&gt;
      &lt;mx:Label id=&quot;progLab_&quot;/&gt;
    &lt;/mx:HBox&gt;
    &lt;mx:TextArea width=&quot;100%&quot; height=&quot;100%&quot; id=&quot;txtArea_&quot;/&gt;
  &lt;/mx:VBox&gt;
&lt;/mx:WindowedApplication&gt;
</pre>
<h3>リファレンス</h3>
<ul>
<li><a href="http://help.adobe.com/ja_JP/AIR/1.5/devappshtml/WS5b3ccc516d4fbf351e63e3d118666ade46-7e4a.html" title="Adobe AIR 1.5 * ファイルシステムの操作" target="_blank">Adobe AIR 1.5 * ファイルシステムの操作</a></li>
<li><a href="http://help.adobe.com/ja_JP/AIR/1.5/devappshtml/WS5b3ccc516d4fbf351e63e3d118666ade46-7dc2.html" title="Adobe AIR 1.5 * ファイルの読み取りと書き込み" target="_blank">Adobe AIR 1.5 * ファイルの読み取りと書き込み</a></li>
</ul>
<h4>関連すると思われる記事：</h4>
<ul class="similar-posts">
<li><a href="http://www.yukun.info/blog/2008/12/actionscript-air-async-read-text.html" rel="bookmark" title="2008年12月17日">AIR: テキストファイルを非同期に読み込む &#8211; openAsync()、readMultiByte()</a></li>
<li><a href="http://www.yukun.info/blog/2008/09/python-file-write-writelines.html" rel="bookmark" title="2008年9月6日">Python: テキストファイルに書き込み &#8211; write()、writelines()メソッド</a></li>
<li><a href="http://www.yukun.info/blog/2008/06/python-csv-write.html" rel="bookmark" title="2008年6月18日">Python: CSVファイルに書き込み &#8211; csv.writerオブジェクト</a></li>
<li><a href="http://www.yukun.info/blog/2008/09/python-add-line-number-to-text-file.html" rel="bookmark" title="2008年9月8日">Python: テキストファイルの行頭に行番号を追加</a></li>
<li><a href="http://www.yukun.info/blog/2008/12/air-actionscript-sqlite-statement.html" rel="bookmark" title="2008年12月22日">AIR: SQLiteでデータの挿入と検索 &#8211; SQLConnection、SQLStatementクラス</a></li>
</ul>
<p><!-- Similar Posts took 9.313 ms --></p>
<p><a href="http://www.yukun.info/blog/2008/12/actionscript-air-async-write-text.html">AIR: テキストファイルに書き込み &#8211; openAsync()、writeMultiByte()</a> is a post from: <a href="http://www.yukun.info">Yukun&#039;s Blog</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.yukun.info/blog/2008/12/actionscript-air-async-write-text.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>AIR: テキストファイルを非同期に読み込む &#8211; openAsync()、readMultiByte()</title>
		<link>http://www.yukun.info/blog/2008/12/actionscript-air-async-read-text.html</link>
		<comments>http://www.yukun.info/blog/2008/12/actionscript-air-async-read-text.html#comments</comments>
		<pubDate>Wed, 17 Dec 2008 03:30:27 +0000</pubDate>
		<dc:creator>yukun</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[AIR]]></category>
		<category><![CDATA[File]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[GUI]]></category>
		<category><![CDATA[read]]></category>

		<guid isPermaLink="false">http://www.yukun.info/?p=1269</guid>
		<description><![CDATA[AIRコンポーネントではローカルのファイルにアクセスすることができます。下記のコードは日本語を含むマルチバイトのテキストファイルを読み込み、画面い表示する処理を行います。 大まかな手順 FileStreamのコンストラク &#8230; <a href="http://www.yukun.info/blog/2008/12/actionscript-air-async-read-text.html">Continue reading <span class="meta-nav">&#8594;</span></a><p><a href="http://www.yukun.info/blog/2008/12/actionscript-air-async-read-text.html">AIR: テキストファイルを非同期に読み込む &#8211; openAsync()、readMultiByte()</a> is a post from: <a href="http://www.yukun.info">Yukun&#039;s Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.yukun.info/wp-content/uploads/ReadTxtFile01_result.png"><img src="http://www.yukun.info/wp-content/uploads/ReadTxtFile01_result-e1273382664744.png" alt="AIR: テキストファイル読み込みの実行結果" title="ReadTxtFile01_result" width="400" height="328" class="size-full wp-image-1531" /></a></p>
<p>AIRコンポーネントではローカルのファイルにアクセスすることができます。下記のコードは日本語を含むマルチバイトのテキストファイルを読み込み、画面い表示する処理を行います。</p>
<h2>大まかな手順</h2>
<ol>
<li>FileStreamのコンストラクタの引数に対象のファイルへのパスが設定されたFileインスタンスを渡す。</li>
<li>FileStream#openAsyncで実ファイルへのパイプ接続。</li>
<li>この時、非同期の読み込み完了／エラーを取得するためにイベントを登録しておく。</li>
<li>実際の文字の読み取り（どれだけ読むか、文字コードの変換など）はFileStream#readMultiByteで行う。</li>
<li>ストリームのインスタンスには接続時にpositionプロパティ（何処読んでいるかのポインタみたいなもの）からファイル末尾までのサイズ（bytesAvailable）を取得してるので、読み込みサイズにそれを指定。</li>
<li>FileStream#close()でストリームを閉じる</li>
</ol>
<h3>ソースコード</h3>
<pre>
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;mx:WindowedApplication xmlns:mx=&quot;http://www.adobe.com/2006/mxml&quot; layout=&quot;absolute&quot;
  title=&quot;テキストビュワー&quot;&gt;
  &lt;mx:Script&gt;
    &lt;![CDATA[
    import flash.filesystem.*;
    import mx.events.*;
    import mx.controls.Alert;

    private var choDir:File = File.documentsDirectory; // 開くディレクトリを指す
    private var curFile:File; // 選択されたファイル
    private var stream:FileStream;

    private function onOpenFileBut():void {
      choDir.addEventListener(Event.SELECT, onSelectFile);
      choDir.browseForOpen(&quot;開く&quot;); // ファイル選択ダイアログの表示
    }

    // ファイルが選択されたイベント
    private function onSelectFile(e:Event):void {
      txtArea_.text = &quot;&quot;;
      stream = new FileStream();
      curFile = e.target as File;
      stream.addEventListener(Event.COMPLETE, onCompleteReadFile);
      stream.addEventListener(IOErrorEvent.IO_ERROR, onIOErrorReadFile);
      stream.addEventListener(ProgressEvent.PROGRESS, onProgReadFile);
      stream.openAsync(curFile, FileMode.READ); // 非同期読み込み
      curFile.removeEventListener(Event.SELECT, onSelectFile);
    }

    private function onCompleteReadFile(e:Event):void {
      try {
        // OS標準の文字コードで読み込み
        var str:String = stream.readMultiByte(stream.bytesAvailable, File.systemCharset);
        // OS標準の改行文字への変換
        var pat:RegExp = new RegExp(File.lineEnding, &quot;g&quot;);
        str = str.replace(pat, &quot;n&quot;);
        txtArea_.text = str; // テキストエリアに表示
        stream.removeEventListener(Event.COMPLETE, onCompleteReadFile);
        stream.removeEventListener(IOErrorEvent.IO_ERROR, onIOErrorReadFile);
        stream.removeEventListener(ProgressEvent.PROGRESS, onProgReadFile);
      } catch (err:Error) {
        progLab_.text = &quot;IOError: &quot; + err;
      }
      finally {
        // パイプのクローズ
        if (stream != null) {
          stream.close();
        }
      }
    }

    private function onIOErrorReadFile(e:IOErrorEvent):void {
      Alert.show(&quot;ファイルを読み込み不可&quot;, &quot;Error&quot;, Alert.OK, this); // 第4引数には親オブジェトを渡す
      if (stream != null) {
        stream.close();
      }
    }

    private function onProgReadFile(e:ProgressEvent):void {
      progLab_.text = &quot;Progress: &quot; +  e.bytesLoaded + &quot; / &quot; + e.bytesTotal + &quot; bytes&quot;;
    }
    ]]&gt;
  &lt;/mx:Script&gt;
  &lt;mx:VBox x=&quot;0&quot; y=&quot;0&quot; height=&quot;100%&quot; width=&quot;100%&quot;&gt;
    &lt;mx:HBox width=&quot;100%&quot;&gt;
      &lt;mx:Button label=&quot;ファイルを開く&quot; id=&quot;openBut_&quot; click=&quot;onOpenFileBut();&quot;/&gt;
      &lt;mx:Label id=&quot;progLab_&quot;/&gt;
    &lt;/mx:HBox&gt;
    &lt;mx:TextArea width=&quot;100%&quot; height=&quot;100%&quot; id=&quot;txtArea_&quot;/&gt;
  &lt;/mx:VBox&gt;

&lt;/mx:WindowedApplication&gt;
</pre>
<h3>リファレンス</h3>
<ul>
<li><a href="http://help.adobe.com/ja_JP/AIR/1.5/devappshtml/WS5b3ccc516d4fbf351e63e3d118666ade46-7dc8.html" title="Adobe AIR 1.5 * ファイルの読み取りと書き込みのワークフロー" target="_blank">Adobe AIR 1.5 * ファイルの読み取りと書き込みのワークフロー</a></li>
<li><a href="http://help.adobe.com/ja_JP/AIR/1.5/devappshtml/WS5b3ccc516d4fbf351e63e3d118666ade46-7dac.html" title="Adobe AIR 1.5 * 読み取りバッファと FileStream オブジェクトの bytesAvailable プロパティ" target="_blank">Adobe AIR 1.5 * 読み取りバッファと FileStream オブジェクトの bytesAvailable プロパティ</a></li>
</ul>
<h4>関連すると思われる記事：</h4>
<ul class="similar-posts">
<li><a href="http://www.yukun.info/blog/2008/12/actionscript-air-async-write-text.html" rel="bookmark" title="2008年12月18日">AIR: テキストファイルに書き込み &#8211; openAsync()、writeMultiByte()</a></li>
<li><a href="http://www.yukun.info/blog/2008/06/python-csv-read.html" rel="bookmark" title="2008年6月17日">Python: CSVファイルの読み込み &#8211; csv.readerオブジェクト</a></li>
<li><a href="http://www.yukun.info/blog/2008/06/python-file.html" rel="bookmark" title="2008年6月9日">Python: テキストファイルの読み込み &#8211; read()、readlines()、readline()メソッド</a></li>
<li><a href="http://www.yukun.info/blog/2008/09/python-file-write-writelines.html" rel="bookmark" title="2008年9月6日">Python: テキストファイルに書き込み &#8211; write()、writelines()メソッド</a></li>
<li><a href="http://www.yukun.info/blog/2008/09/python-add-line-number-to-text-file.html" rel="bookmark" title="2008年9月8日">Python: テキストファイルの行頭に行番号を追加</a></li>
</ul>
<p><!-- Similar Posts took 10.674 ms --></p>
<p><a href="http://www.yukun.info/blog/2008/12/actionscript-air-async-read-text.html">AIR: テキストファイルを非同期に読み込む &#8211; openAsync()、readMultiByte()</a> is a post from: <a href="http://www.yukun.info">Yukun&#039;s Blog</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.yukun.info/blog/2008/12/actionscript-air-async-read-text.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

