Flex读取二进制文件

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读取ESRIshapfile格式文件的示例,此示例为基于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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值