Flex的XML读写
发布时间:2020-12-15 04:02:57 所属栏目:百科 来源:网络整理
导读: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 Config
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,"rn"); try{ stream.writeUTFBytes(String(xmlHead+"rn"+xmlStr)); }catch (e:Error){ trace(e); }finally{ stream.close(); } } } ?自己写的flex文件操作的工具类,主要是读写XML的,这里主要多了一个流保存,监听,然后关闭,这样就可以逃过这个操作了, ? 如果没有做这个监听,紧紧是finally,如果磁盘是管理员权限,但是用户没有管理员权限,会卡死在这里,我不知道为什么 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |