air 讀寫檔案

來源:互聯網
上載者:User

FileReference:

 
package com{ import flash.display.Bitmap; import flash.display.Loader; import flash.display.Sprite; import flash.events.Event; import flash.events.IOErrorEvent; import flash.events.ProgressEvent; import flash.net.FileFilter; import flash.net.FileReference; import flash.net.URLLoader; import flash.utils.ByteArray;  import mx.messaging.channels.StreamingAMFChannel; public class FileFiag extends Sprite {  private var fileR:FileReference;  private var _bitmap:Bitmap;  private var _xml:XML;  private var load_type:String="";  public function FileFiag()  {   init();  }  private function init():void  {   fileR=new FileReference();   fileR.addEventListener(Event.SELECT, onFileSelected);    fileR.addEventListener(Event.CANCEL, onCancel);   fileR.addEventListener(IOErrorEvent.IO_ERROR, onIOError);  }  private function onFileSelected(event:Event):void  {     fileR.addEventListener(ProgressEvent.PROGRESS,onProgress);   fileR.addEventListener(Event.COMPLETE, onComplete);   fileR.load();  }  private function onProgress(event:ProgressEvent):void  {     }  private function onComplete(event:Event):void  {      switch(fileR.extension)   {    case "png":     ImageLoad();     break;    case "xml":     XmlLoad(event);     break;   }     }  private function CompleteHandler(event:Event):void  {   if(event.target.content is Bitmap)   {    this._bitmap=event.target.content as Bitmap;   }   dispatchEvent(event);  }  private function onCancel(event:Event):void  {     }  private function onIOError(event:IOErrorEvent):void  {     }  public function OpenFile(bol:Boolean):void  {     var typeFilter:FileFilter   if(bol)   {    typeFilter=new FileFilter("Images(*.jpg,*.jpeg,*.gif,*.png)","*.jpg;*.jpeg;*.gif;*.png");//設定可讀格式       }   else   {    typeFilter=new FileFilter("xml(*.xml)","*.xml")   }   fileR.browse([typeFilter]);  }  private function ImageLoad():void  {   this.load_type="png";   var loader:Loader=new Loader();   var bt:ByteArray=fileR.data as ByteArray;   loader.loadBytes(bt);   loader.contentLoaderInfo.addEventListener(Event.COMPLETE,CompleteHandler);  }  private function XmlLoad(event:Event):void  {   this.load_type="xml";   var urlloader:URLLoader=new URLLoader();   this._xml=new XML(this.fileR.data);   dispatchEvent(event);   }  public function get loadType():String  {   return this.load_type;  }  public function get bitmap():Bitmap  {   return _bitmap;  }    public function set bitmap(value:Bitmap):void  {   _bitmap = value;  }  public function get xml():XML  {   return _xml;  }    public function set xml(value:XML):void  {   _xml = value;  } }}
 此上為讀文字文件和圖片文檔下面是寫文檔:
public static function BornXml(_ui:UIComponent,_bit:Bitmap):void{var xml:XML=<root url={File.applicationDirectory.resolvePath("data/map.png").nativePath.toString()}></root>;for(var i:int=0;i<_ui.numChildren;i++){if(_ui.getChildAt(i) is Flag){var flag:Flag=_ui.getChildAt(i) as Flag;xml.appendChild(<pic x={flag.x} y={flag.y} name={flag.txt.text}/>)}}trace("目錄:  "+_bit.width);var filexml:File = new File(File.applicationDirectory.resolvePath("data/map.xml").nativePath);var png:PNGEncoder=new PNGEncoder();var by:ByteArray=png.encode(_bit.bitmapData);var fileimg:File=new File(File.applicationDirectory.resolvePath("data/map.png").nativePath);var stream:FileStream = new FileStream;stream.open(filexml, FileMode.WRITE);stream.writeMultiByte(xml.toString(), "utf-8"); stream.open(fileimg, FileMode.WRITE);stream.writeBytes(by);stream.close();}

必須先open 然後write 。如果相互混合會出現錯誤。切記。

 轉載:  

AIR中..儲存檔案我們般會像下面這樣操作

var file:File = new File("C:\\abc.txt")

var fs:FileStream = new FileStream()

fs.open(file,FileMode.WRITE)

fs.position = 0

fs.writeUTFBytes("http://l4cd.net")

fs.close()

這樣就會在C盤產生一個內容為"http://l4cd.net"的abc.txt檔案..

另外,如果想在安裝目錄儲存一些AIR的配置什麼的..

常用的方法是通用File.applicationDirectory來擷取安裝目錄的file對象,再用resolvePath來指定某個檔案

var file:File = File.applicationDirectory.resolvePath("ini.xml")

這樣就會產生一個安裝目錄下的ini.xml檔案的file對象..

可是值得注意的是..當這樣擷取file對象再進行操作時..會有安全問題

儲存代碼執行後會出現

SecurityError: fileWriteResource

  at runtime::SecurityManager$/checkPrivilegeForCaller()

  at flash.filesystem::FileStream/open()

我們必須使用

var file:File = new File(File.applicationDirectory.resolvePath("ini.xml").nativePath);

簡單來說必須以檔案的路徑String來new File.

執行後操作正常..開啟AIR的安裝目錄..發現ini.xml已經正常產生.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.