Writing a text file with Flex and AIR
I have been developing in Flex and climbing the learning curve fast. The application I am building uses Adobe AIR to work both offline and online, and I need to save data locally into a text file as XML. The following is the ActionScript code I use to save a text file onto the desktop:
import flash.filesystem.File;
import flash.filesystem.*;
_
// set up fileName, xml
_
// create a file in the desktop folder
var file:File = File.desktopDirectory.resolvePath(this.fileName);
if (file.exists) file.deleteFile();
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.WRITE);
fileStream.writeUTF(xml);
fileStream.close();
No surprises there; it’s clean, intuitive and completely in line with what one does in .NET. Permissions should not be a problem, as the AIR application takes on the privileges of the launching user. Aaron West has a \nice little write-up over at his blog that explains the basics of file access in AIR.
