Symmetri Developer Blog

December 17, 2008

Flex - writeUTF() vs writeUTFBytes()

Flash/Flex - 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?

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