FlexForAndroid:文件读写

来源:互联网 发布:linux如何安装ftp服务 编辑:程序博客网 时间:2024/04/29 11:34

该例子主要介绍如何对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 + "\n\n";

                           s+= "File.applicationStorageDirectory : " +

                                  File.applicationStorageDirectory.nativePath + "\n\n";

                           s+= "File.desktopDirectory:" +

                                  File.desktopDirectory.nativePath + "\n\n";

                           s+= "File.documentsDirectory : " +

                                  File.documentsDirectory.nativePath + "\n\n";

                           s+= "File.userDirectory : " +

                                  File.userDirectory.nativePath + "\n\n";

 

                           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 + "\n\n";

                                  s+= "path:" + file.nativePath + "\n\n";

                                  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>

 

效果图

                            

 

原创粉丝点击