新建两个Flash AIR文档read.fla,write.fla;write.fla作为写入数据,read.fla作为读取数据,仅作为测试的例子。
在write.fla关键帧第一帧写一下代码:
import flash.utils.ByteArray;import flash.filesystem.File;import flash.filesystem.FileStream;const TEXT:int = 2;var valueStr:String = "hello!"var bytes:ByteArray = new ByteArray();bytes.writeInt(TEXT);bytes.writeInt(16 + valueStr.length);bytes.writeInt(100);bytes.writeInt(150);bytes.writeUTFBytes(valueStr);var file:File = File.desktopDirectory.resolvePath("test.abc");var fs:FileStream = new FileStream();fs.open(file, flash.filesystem.FileMode.WRITE);fs.writeBytes(bytes, 0, bytes.length);fs.close();
发布后会在桌面生成test.abc的文件,abc是扩展名,可任意命名;
在read.fla关键帧第一帧写一下代码:
import flash.utils.ByteArray;import flash.filesystem.File;import flash.filesystem.FileStream;import flash.text.TextField;const TEXT:int = 2;var bytes:ByteArray = new ByteArray();var file:File = File.desktopDirectory.resolvePath("test.abc");var fs:FileStream = new FileStream();fs.open(file, flash.filesystem.FileMode.READ);fs.readBytes(bytes);fs.close();if (bytes.readInt() == TEXT){ var length:int = bytes.readInt(); var tx:int = bytes.readInt(); var ty:int = bytes.readInt(); var str:String = bytes.readUTFBytes(length - 16); var t:TextField = new TextField(); t.x = tx; t.y = ty; t.text = str; addChild(t);}
File读写数据是按顺序读写的,先写先读,后写后读;