▼ActionScript 3.0メモ▼
ファイルの読み書き


ファイルの読み書きを行うAIRアプリを作成する。


ソースコード
FileEx.as
package {
    import flash.display.*;
    import flash.events.*;
    import flash.filesystem.*;
    import flash.net.*;
    import flash.text.*;
   
    //ファイルへのデータ保存
    [SWF(width=240, height=240, backgroundColor=0xFFFFFF)]
    public class FileEx extends Sprite {
        private var textField:TextField;//テキストフィールド
        private var btnWrite :TextField;//書き込みボタン
        private var btnRead  :TextField;//読み込みボタン
        private var file     :File;     //ファイル
        private var fileMode :String;   //ファイルモード
        
        //コンストラクタ
        public function FileEx() {
            //ベースの追加
            var base:Sprite = new Sprite();
            base.graphics.beginFill(0xffffff);
            base.graphics.drawRect(0, 0, 240, 240);            
            base.graphics.endFill();
            addChild(base);

            //テキストフィールドの追加
            textField = addTextField(base, "" ,10 ,10);            

            //ボタンの追加
            btnWrite = addButton(base, "書き込み", 10, 36);            
            btnRead  = addButton(base, "読み込み", 70, 36);
        }
        
        //テキストフィールドの追加
        private function addTextField(doc:DisplayObjectContainer,
            text:String, x:int, y:int):TextField {
            var textField:TextField = new TextField();
            doc.addChild(textField);
            textField.text       = text;
            textField.x          = x;
            textField.y          = y;
            textField.width      = 200;
            textField.height     = 16;
            textField.selectable = true;
            textField.border     = true;
            textField.type       = TextFieldType.INPUT;
            return textField;
        }

        //ボタンの追加
        private function addButton(doc:DisplayObjectContainer,
            text:String, x:int, y:int):TextField {
            var button:TextField = new TextField();            
            doc.addChild(button);
            button.text            = text;                  
            button.x               = x;
            button.y               = y;
            button.autoSize        = TextFieldAutoSize.LEFT;
            button.selectable      = false;                 
            button.border          = true;                  
            button.background      = true;                  
            button.backgroundColor = 0xdddddd;
            button.addEventListener(MouseEvent.CLICK, onClick);
            return button;
        }

        //クリックイベントの処理
        private function onClick(evt:MouseEvent):void {
            //書き込み            
            if (evt.currentTarget == btnWrite) {
                fileMode = "write";
                file = File.desktopDirectory;                 
                file.addEventListener(Event.SELECT, onSelect);                
                file.browseForSave("保存");
            }
            //読み込み
            else if (evt.currentTarget == btnRead) {
                fileMode = "read";
                file = File.desktopDirectory;                 
                file.addEventListener(Event.SELECT, onSelect);            
                var filter:FileFilter = new FileFilter("テキスト", "*.txt;*.html");
                file.browseForOpen("開く", [filter]);
            }
        }
        
        //選択イベントの処理
        private function onSelect(evt:Event):void {
            try {
                //書き込み
                if (fileMode == "write") {                    
                    var fsw:FileStream = new FileStream();            
                    fsw.open(file, FileMode.WRITE);                    
                    fsw.writeMultiByte(textField.text, "shift_jis");
                    fsw.close();
                }
                //読み込み
                else if (fileMode == "read") {
                    var fsr:FileStream = new FileStream();                    
                    fsr.open(file, FileMode.READ);                    
                    textField.text = fsr.readMultiByte(file.size, "shift_jis");
                    fsr.close();                    
                }
            } catch (e:Error) {
                trace(e.toString());
            }
        }
    }

}



−戻る−