ActionScript 3 使用ActionScript 3异步读取文件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ActionScript 3 使用ActionScript 3异步读取文件相关的知识,希望对你有一定的参考价值。

// Imports
      import flash.filesystem.FileMode;
      import flash.filesystem.FileStream;
      import flash.filesystem.File;
      import flash.events.ProgressEvent;
      import flash.events.Event;
       
      // Declare the FileStream and String variables

      private var _fileStream:FileStream;
 
      private var _fileContents:String;
       
      private function onCreationComplete():void // Fired when the application has been created

      {

      var myFile:File = File.appResourceDirectory; // Create out file object and tell our File Object where to look for the file

      myFile = myFile.resolve("mySampleFile.txt"); // Point it to an actual file


      _fileStream = new FileStream(); // Create our file stream

      _fileStream.addEventListener(ProgressEvent.PROGRESS, onFileProgress); // Add our the progress event listener

      _fileStream.addEventListener(Event.COMPLETE, onFileComplete); // Add our the complete event listener
       
      _fileStream.openAsync(myFile, FileMode.READ); // Call the openAsync() method instead of open()

      }
      private function onFileProgress(p_evt:ProgressEvent):void // Event handler for the PROGRESS Event
      {
      _fileContents += _fileStream.readMultiByte(_fileStream.bytesAvailable, "iso-8859-1"); // Read the contens of the file and add to the contents variable
       
      fileContents_txt.text = _fileContents; // Display the contents. I've created a TextArea on the stage for display
      }
       
      private function onFileComplete(p_evt:Event):void // Event handler for the COMPLETE event

      {
      _fileStream.close(); // Clean up and close the file stream
      }

以上是关于ActionScript 3 使用ActionScript 3异步读取文件的主要内容,如果未能解决你的问题,请参考以下文章

ActionScript 3:字符命中测试对象,所有对象都在数组中

ActionScript 3 使用ActionScript 3暂停框架

ActionScript 3 clickTag使用ActionScript 3

ActionScript 3 全屏使用ActionScript 3

ActionScript 3 使用ActionScript 3异步读取文件

ActionScript 3 使用Actionscript 3的onEnterFrame事件