Flash AIR 保存与读取
package {
import flash.display.Sprite;
import flash.net.URLLoader;
import flash.display.Loader;
import flash.display.Bitmap;
import flash.net.URLRequest;
import flash.net.URLLoaderDataFormat;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.utils.ByteArray;
import flash.filesystem.File;
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;
/**
* ...
* @author Purple_Dream
*/
public class Main extends Sprite {
private var file:File;
private var saveByte:ByteArray = new ByteArray();
private var fileStream:FileStream = new FileStream();
private var urlLoader:URLLoader;
private var path:String="file:///d:/bob/";
private var file_name:String="ltp";
//private var loader:Loader=new Loader();
private var a:int=100;
public function Main():void {
save_btn.addEventListener(MouseEvent.CLICK,saveData);
read_btn.addEventListener(MouseEvent.CLICK,readData);
}
private function saveData(e:MouseEvent):void {
a--;
file=new File("file:///d:/bob/");
file.createDirectory();
file=file.resolvePath(file_name);
trace(a)
var obj:Object={};
obj.num=a
obj.listArr=[1,2,3,4,5];
obj.name="测试";
obj.url="1.jpg";
saveByte.writeObject(obj);
saveByte.compress();
fileStream.open(file, FileMode.WRITE);
fileStream.writeBytes(saveByte);
fileStream.close();
}
private function readData(e:MouseEvent):void {
urlLoader=new URLLoader();
urlLoader.dataFormat=URLLoaderDataFormat.BINARY;
urlLoader.load(new URLRequest(path+file_name));
urlLoader.addEventListener(Event.COMPLETE,loaderComplete);
trace(path+file_name)
}
private function loaderComplete(e:Event):void {
var byte:ByteArray=e.target.data;
byte.uncompress();
var obj:Object=byte.readObject();
trace(obj.num,obj.listArr);
}
}
}