Flex AIR —— 文件读写

本文介绍了一个使用ActionScript编写的简单程序,该程序能够打开并读取XML文件中的基金数据,包括基金名称、份额及净值,并允许修改这些数据后保存回原文件。

一、文件内容

<funds>
  <fund>
    <name>中银中国</name>
    <lot>274</lot>
    <net>1.6612</net>
  </fund>
  <fund>
    <name>广发稳健</name>
    <lot>280.85</lot>
    <net>1.5942</net>
  </fund>
</funds>
 

 

二、源码

private var filePath:String = null;
private var xmlList:XMLList = null;

private function openConfig():void{
	var file:File = new File();
	file.browseForOpen("选择文件", [new FileFilter("*.xml","*.xml")]);
	file.addEventListener(Event.SELECT, onFileSelect);
}

private function onFileSelect(e:Event):void{
	//读文件
	var fs:FileStream = new FileStream();
	fs.open(File(e.target), FileMode.READ);
	var txt:String = fs.readUTFBytes(fs.bytesAvailable);
	fs.close();
	
	filePath = File(e.target).nativePath; //文件路径
	xmlList = new XMLList(txt); //文件内容
	
	//获取节点值
	lot1.text = xmlList.children()[0].lot;
	netValue1.text = xmlList.children()[0].net;
	
	lot2.text = xmlList.children()[1].lot;
	netValue2.text = xmlList.children()[1].net;
}

private function saveConfig():void{
	//设置节点值
	xmlList.children()[0].lot = lot1.text;
	xmlList.children()[0].net = netValue1.text;
	xmlList.children()[1].lot = lot2.text;
	xmlList.children()[1].net = netValue2.text;
	
	//写文件
	var fs:FileStream = new FileStream();
	fs.open(new File(filePath), FileMode.WRITE); 
	fs.writeUTFBytes(xmlList.toXMLString()); 
	fs.close(); 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值