<?xml version="1.0" encoding="utf-8"?> <!-- Description: Show how to write string to a file. Note: This example can only be run as Adobe AIR application. --> <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" creationComplete="main()" > <fx:Script> <![CDATA[ import flash.filesystem.File; import flash.filesystem.FileStream; import flash.filesystem.FileMode; public function main():void { // Point a File object to a file path. var oFile:File = new File(); var sFilePath:String = "c:/myfile.txt"; oFile.nativePath = sFilePath; // Open the file. var oWrite:FileStream = new FileStream(); oWrite.open(oFile, FileMode.WRITE); // Here is the tricky part. In order to write // a string to a file, you have to first convert the string // into a ByteArray and then write the ByteArray into the file. var sText:String = "Write this message to a file." var oByteArray:ByteArray = new ByteArray(); oByteArray.writeUTFBytes(sText); oWrite.writeBytes(oByteArray, 0, oByteArray.length); // Close the file. oWrite.close(); } ]]> </fx:Script> </s:WindowedApplication>
Reference:
http://livedocs.adobe.com/flex/3/html/help.html?content=Filesystem_08.html
http://livedocs.adobe.com/flex/3/html/help.html?content=Filesystem_03.html#1028332