Flex AIR —— 文件读写

阅读更多

一、文件内容


  
    中银中国
    274
    1.6612
  
  
    广发稳健
    280.85
    1.5942
  
 

 

二、源码

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(); 
}

你可能感兴趣的:(AIR,Flex,.net,XML)