air读取本地资源文件

<?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"
initialize="windowedapplication1_initializeHandler(event)"

>
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>

<fx:Script>
<![CDATA[
import flash.filesystem.File;
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;

//flash.filesystem.*这个库文件为air独有,web程序则不拥有的

import mx.controls.Alert;
import mx.events.FlexEvent;


private function getStudentInfo():void{
/*File.applicationDirectory.nativePath的值是:
C:\Documents and Settings\a\My Documents\Flex Builder 3\readTxtDemo\bin-debug
*/
// Alert.show(File.applicationDirectory.nativePath);
//创建File对象获取文件路径



var file:File = new File(File.applicationDirectory.nativePath + "/student.txt");
var stream:FileStream = new FileStream(); //创建FileStream对象
stream.open(file,FileMode.READ); //使用FileStream对象以只读方式打开File对象
//将文件中的所有信息显示在文本区域中
txtTextAreaID.text = stream.readUTFBytes(stream.bytesAvailable);
stream.close(); //关闭FileStream对象
}

protected function windowedapplication1_initializeHandler(event:FlexEvent):void
{
// TODO Auto-generated method stub
Alert.show(File.applicationDirectory.nativePath);
//相当于一个bin目录
}

protected function button1_clickHandler(event:MouseEvent):void
{
// TODO Auto-generated method stub
getStudentInfo();
}

protected function exitButton_clickHandler(event:MouseEvent):void
{
// TODO Auto-generated method stub
this.close();
//退出程序
}

]]>
</fx:Script>

<mx:Panel title="从文件中读取数据" verticalAlign="middle" horizontalAlign ="center" width="1078" height="90%">
<mx:TextArea id="txtTextAreaID" width="300" height="200" />
<s:Button label="Click me" click="button1_clickHandler(event)" />
<s:Button label="Exit" id="exitButton" click="exitButton_clickHandler(event)" />
</mx:Panel>

</s:WindowedApplication>
阅读更多
个人分类: Flex4 && Air 开发
上一篇Flex4 CookBook 之类与接口
下一篇Flex4之HTTPService,读取天气预报
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭