Flex读取txt文件中的内容
1、设计源码
LoadTxt.mxml:
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
width="100%" height="100%" creationComplete="initCompleteHandler()">
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.events.FlexEvent;
/*初始化函数*/
protected function initCompleteHandler():void
{
//创建File对象获取文件路径
var file:File = new File(File.applicationDirectory.nativePath+"/phone.txt");
//创建FileStream对象
var stream:FileStream = new FileStream();
//使用FileStream对象以只读方式打开File对象
stream.open(file,FileMode.READ);
//将文件中的所有信息显示在文本区域中
var strs:String = stream.readUTFBytes(stream.bytesAvailable);
txtTextAreaID.text = strs;
//关闭FileStream对象
stream.close();
var temp:String = "";
for(var ix:int =0;ix < strs.length;ix++)
{
var charCode:Number = strs.charCodeAt(ix);
if(charCode >= 48 && charCode <= 57)
{
temp += strs.charAt(ix);
}
else if(charCode == 10)
{
//,替换空格
temp += ",";
}
}
var strArr:Array = temp.split(",");
for each(var str:String in strArr)
{
trace(str);
}
Alert.show(strArr.length + "","数组长度");
}
]]>
</fx:Script>
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<mx:Panel title="从文件中读取数据" width="1200" height="680">
<mx:TextArea id="txtTextAreaID" width="100%" height="100%"/>
</mx:Panel>
</s:WindowedApplication>
2、设计结果
(1)初始化
(2)单击确定后
(3)控制台