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.tar get. content is Bitmap) {this._bitmap=event.tar get. 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 "); // set the readable format} 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 ;}}}
Here we write a document for reading text documents and image documents:
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} name1_1_flag.txt. text}/>)} trace ("Directory:" + _ 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 ();}
You must first open and then write. If they are mixed, an error occurs. Remember.
Reprinted:
In AIR... save the file. We will do the following:
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 ()
This will generate a "http://l4cd.net" abc.txt file in drive C ..
In addition, if you want to save some AIR configurations in the installation directory ..
The common method is generic File.ApplicationDirectoryTo get the file object of the installation directory, and then use resolvePath to specify a file
Image
Var file: File = File.ApplicationDirectory. ResolvePath ("ini. xml ")
This will generate a file object for the ini. xml file under the installation directory ..
However, it is worth noting that, when the file object is obtained in this way and then operated, there will be security issues.
After the code is saved and executed
SecurityError: fileWriteResource
At runtime: SecurityManager $/checkPrivilegeForCaller ()
AtFlash. Filesystem: FileStream/open ()
We must use
Var file: File = new File (File.ApplicationDirectory. ResolvePath ("ini. xml"). nativePath );
Simply put, the File path String must be used to create a new File.
After the command is executed, the operation is normal. Open the installation directory of AIR. ini. xml is generated normally.