Flex读取二进制文件
Flex中可用于读取二进制文件的类有四个:ByteArray,FileStream,Socket,URLStream。它们都实现了IdataInput接口,分别位于flash.utils,flash.filesystem,flash.net,flash.net包。需要指出的是,FileStream只适用于AIR项目中。
上述四个类的详细内容和使用方法,请参考Adobe官方文档:ActionScript3.0语言与组件参考:http://www.adobe.com/livedocs/flex/3_cn/。
下附用URLStream读取ESRI的shapfile格式文件的示例,此示例为基于Adobe Flash Builder4开发环境的ActionScript项目。
package { import flash.display.Sprite; import flash.errors.IOError; import flash.events.*; import flash.net.URLRequest; import flash.net.URLStream; import flash.utils.Endian; public class ShapeReader extends Sprite { private var _stream:URLStream; public function ShapeReader() { this._stream = new URLStream(); this.readfile("http://localhost/data/regionprov_Clip.shp"); } private function readfile(urlPath:String):void { var request:URLRequest = new URLRequest(urlPath); this._stream.addEventListener(Event.COMPLETE,onComplete); try { this._stream.load(request); } catch (error:Error) { trace("Unable to load requested URL."); } } private function onComplete(event:Event):void { try { //.........................Begin of FileHeader var code:int = this._stream.readInt();//File Code this._stream.readUTFBytes(5*4);//skip int bytes in number of 5 var length:int = this._stream.readInt();//File Length this._stream.endian = Endian.LITTLE_ENDIAN;//change byte order var version:int = this._stream.readInt();//Version var type:int = this._stream.readInt();//Shape Type var xMin:Number = this._stream.readDouble();//Xmin var yMin:Number = this._stream.readDouble();//Ymin var xMax:Number = this._stream.readDouble();//Xmax var yMax:Number = this._stream.readDouble();//Ymax this._stream.readUTFBytes(4*8);// skip double bytes in number of 4 //.........................End of FileHeader this._stream.close();// close stream } catch(error:IOError) { trace(error.toString()); } } } }
LookAtYou 2010.8.25