Flex的XML读写

package com.copote.fileOperations
{
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;

	/**
	 * 对ConfigXMl进行操作
	 * */
	public class ConfigXmlOperation
	{
		public function ConfigXmlOperation()
		{
		}
		
		static public var configXML:XML = null;
		
		private static const fileName:String = File.applicationDirectory.resolvePath("config-context.xml").nativePath;
		
		static public function readConfigXml():void{
			var file:File=new File(fileName);
			var fileStream:FileStream = new FileStream();
			fileStream.addEventListener(Event.COMPLETE, function():void{
				configXML = XML(fileStream.readUTFBytes(fileStream.bytesAvailable));
				fileStream.close();
			});
			fileStream.openAsync(file, FileMode.READ);
		}
		
		static public function writeConfigXml(xml:XML):void{
			var xmlHead:String = '<?xml version="1.0" encoding="utf-8" ?>'			
			var stream:FileStream = new FileStream();
			var file:File = new File(fileName);                        
			stream.openAsync(file, FileMode.WRITE);
			stream.addEventListener(IOErrorEvent.IO_ERROR,function():void{
					stream.close();	//这个是用来放置用户磁盘保护这样就会报错,所以会监视,关闭
				}
			);
			var xmlStr:String = xml.toString();
			var pattern:RegExp = /\n/g;
			xmlStr=xmlStr.replace(pattern, "\r\n");
			try{
				stream.writeUTFBytes(String(xmlHead+"\r\n"+xmlStr));
			}catch (e:Error){
				trace(e);
			}finally{
				stream.close();
			}
		}
		
		
	}

 自己写的flex文件操作的工具类,主要是读写XML的,这里主要多了一个流保存,监听,然后关闭,这样就可以逃过这个操作了,

 

如果没有做这个监听,紧紧是finally,如果磁盘是管理员权限,但是用户没有管理员权限,会卡死在这里,我不知道为什么

你可能感兴趣的:(xml,Flex)