加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

FlexForAndroid:文件读写

发布时间:2020-12-15 04:37:42 所属栏目:百科 来源:网络整理
导读:该例子主要介绍如何对 Android 设备的文件进行读写。 源码 ?xml version="1.0" encoding="utf-8"? s : Application xmlns:fx =" http://ns.adobe.com/mxml/2009 " ???????????????????? ?? xmlns:s =" library://ns.adobe.com/flex/spark " applicationDPI =

该例子主要介绍如何对Android设备的文件进行读写。

源码

<?xmlversion="1.0" encoding="utf-8"?>

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"

???????????????????? ?? xmlns:s="library://ns.adobe.com/flex/spark" applicationDPI="160" applicationComplete="Init()">

?????? <s:layout>

????????????? <s:VerticalLayout gap="3" paddingBottom="3" paddingLeft="3" paddingRight="3" paddingTop="3"/>

?????? </s:layout>

?????? <fx:Declarations>

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

?????? </fx:Declarations>

?????? <fx:Script>

????????????? <![CDATA[

???????????????????? private function Init():void

???????????????????? {

?????????????????????????? vars:String = "";

?????????????????????????? s+= "File.applicationDirectory : " +

????????????????????????????????? File.applicationDirectory.nativePath + "nn";

?????????????????????????? s+= "File.applicationStorageDirectory : " +

????????????????????????????????? File.applicationStorageDirectory.nativePath + "nn";

?????????????????????????? s+= "File.desktopDirectory:" +

????????????????????????????????? File.desktopDirectory.nativePath + "nn";

?????????????????????????? s+= "File.documentsDirectory : " +

????????????????????????????????? File.documentsDirectory.nativePath + "nn";

?????????????????????????? s+= "File.userDirectory : " +

????????????????????????????????? File.userDirectory.nativePath + "nn";

?

?????????????????????????? Info.text = s;

???????????????????? }

????????????????????

???????????????????? private function ReadFile():void

???????????????????? {

?????????????????????????? varfile:File = File.applicationStorageDirectory.resolvePath("log.txt");

?????????????????????????? if(file.exists)

?????????????????????????? {

????????????????????????????????? var fs:FileStream = new FileStream();

????????????????????????????????? fs.open(file,FileMode.READ);

?????????????????????????????????

????????????????????????????????? vars:String = "";

????????????????????????????????? s+= "url:" +file.url + "nn";

?????? ?????????????????????????? s+= "path:" + file.nativePath + "nn";

????????????????????????????????? s+= "读取:" + fs.readMultiByte(fs.bytesAvailable,"utf-8");

????????????????????????????????? Info.text = s;

????????????????????????????????? fs.close();

?????????????????????????? }

?????????????????????????? else

????????????????????????????????? Info.text = "文件不存在!";

???????????????????? }

????????????????????

???????????????????? private function WriteFile():void

???????????????????? {

?????????????????????????? varfile:File = File.applicationStorageDirectory.resolvePath("log.txt");

??????????????????????????

?????????????????????????? var fs:FileStream = new FileStream();??????????????????????????????

?????????????????????????? fs.open(file,FileMode.WRITE);

??????????????????????????

?????????????????????????? var date:Date = new Date();

?????????????????????????? var s:String = "时间" + date.time;

?????????????????????????? Info.text = "写入:" + s;

??????????????????????????

?????????????????????????? fs.writeMultiByte(s,"utf-8");

???????????????????? ??? fs.close();

??????????????????????????

???????????????????? }

???????????????????? private function Exit():void

???????????????????? {

?????????????????????????? NativeApplication.nativeApplication.exit();

???????????????????? }

????????????? ]]>

?????? </fx:Script>

?????? <s:HGroup>

????????????? <s:Buttonlabel="读文件"click="ReadFile()"/>

????????????? <s:Buttonlabel="写文件"click="WriteFile()"/>?

????????????? <s:Buttonlabel="退出"click="Exit()"/>

?????? </s:HGroup>

?????? <s:TextArea id="Info"width="100%" height="100%"/>

</s:Application>

?

效果图

? ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? ?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读