1 package 2 { 3 import flash.display.Sprite; 4 import flash.events.Event; 5 import flash.filesystem.File; 6 import flash.filesystem.FileMode; 7 import flash.filesystem.FileStream; 8 import flash.net.URLLoader; 9 import flash.net.URLRequest; 10 import flash.system.System; 11 import flash.text.TextField; 12 /** 13 * ... 14 * @author Frost.Yen 15 */ 16 public class Main extends Sprite 17 { 18 private var _t:TextField; 19 public function Main() 20 { 21 System.useCodePage = true;//解决中文乱码问题 22 _t = new TextField(); 23 _t.autoSize = "left"; 24 this.addChild(_t); 25 readTxt();//方法一(仅限AIR) 26 //loadTxt();//方法二 27 } 28 private function readTxt():void 29 { 30 var file:File = new File(File.applicationDirectory.nativePath + "/新建文本文档.txt"); 31 var stream:FileStream = new FileStream(); //创建FileStream对象 32 stream.open(file,FileMode.READ); //使用FileStream对象以只读方式打开File对象 33 //将文件中的所有信息显示在文本区域中 34 _t.text = stream.readUTFBytes(stream.bytesAvailable); 35 stream.close(); //关闭FileStream对象 36 } 37 private function loadTxt():void 38 { 39 var ldr:URLLoader = new URLLoader(); 40 ldr.load(new URLRequest("新建文本文档.txt")); 41 ldr.addEventListener(Event.COMPLETE, onComplete); 42 } 43 private function onComplete(e:Event):void 44 { 45 _t.text = String(e.target.data); 46 } 47 } 48 49 }