Symmetri Developer Blog

December 17, 2008

Flex - writeUTF() vs writeUTFBytes()

Flash/Flex/Actionscript - By Shourov Bhattacharya

I am using FileStream.writeUTF() to write out an XML file, and I noticed that the file is being prefixed by strange characters, which is messing up my XML.

var file:File = File.desktopDirectory.resolvePath(FILENAME);
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.WRITE);
fileStream.writeUTF(data); // file now has data prefixed by strange characters
fileStream.close();

It turns out that this is by design, as the writeUTF() function prefixes the file with the length of the file. God knows why it does that, but the solution is to use writeUTFBytes() instead - does the same thing without prefixing the data with the length. Does anyone know why the writeUTF() function writes out the length?

December 7, 2008

Writing a text file with Flex and AIR

Flash/Flex/Actionscript - By Shourov Bhattacharya

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.

Get free blog up and running in minutes with Blogsome
Theme designed by Janis Joseph