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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值