首先新建一个文本文件(data.txt)放入工程目录的bin-debug文件夹下(也可以自动创建)
<?xml version="1.0" encoding="utf-8"?> <mx:WindowedApplication width="550" height="400" xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()"> <mx:Script> <![CDATA[ import mx.controls.Alert; import flash.filesystem.*; private var file:File = new File(); private var fileStream:FileStream = new FileStream(); private function init():void { this.file.nativePath = File.applicationDirectory.nativePath+"/data.txt"; } 向本地data.txt文件写入数据 private function writeDataHandler(e:MouseEvent):void { this.fileStream.open(this.file,FileMode.WRITE); this.fileStream.writeUTFBytes(this.input.text); this.fileStream.close(); reLoadDataHandler();///重新从本地读取并显示,不信可以打开本地文件查看内容是否已经修改 } //读取本地data.txt文件里边的数据 private function reLoadDataHandler(e:MouseEvent=null):void { this.fileStream.open(this.file,FileMode.READ); this.content.text = this.fileStream.readUTFBytes(this.fileStream.bytesAvailable); this.fileStream.close(); } ]]> </mx:Script> <mx:TextArea id="content" x="15" y="83" borderColor="0xff0000" height="255" width="518"/> <mx:TextInput x="15" y="53" width="330" id="input"/> <mx:Button x="353" y="53" label="写入" click="writeDataHandler(event)"/> <mx:Button x="420" y="53" label="读取" click="reLoadDataHandler(event)"/> </mx:WindowedApplication>