Class to Save object(s) to file with Flex in Air
by Nicolas Noben
The class
Feel free to use it for free or commercial projects.
package com.idletogether
{
import flash.events.Event;
import flash.filesystem.*;
public class Saver
{
public static var file:File;
public static function saveToFile() :void
{
// pick an unused extension
file = new File("/filename.ext");
file.addEventListener(Event.SELECT, dirSelected);
file.browseForSave('');
}
public static function dirSelected(e:Event) :void
{
// this object will get saved to the file
var dat:Object = new Object();
dat.data = some.object;
dat.structure = some.other.object;
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.WRITE);
fileStream.writeObject(dat);
fileStream.close();
}
}
}
How to use it
It’s a static method to be called so, simply…
import com.idletogether.Saver; Saver.saveToFile();
This will prompt the user to pick a destination and will suggest the file name “filename.ext”.
Please note that you can’t save DisplayObjects straight to file using writeObject. It’s a limitation.
From here, find out how to get the object back into flex in the next post.
HTH,
Cheers.
Related Posts
- Class to Open object(s) from file with Flex in Air
- Flash / Flex / AIR Development Adelaide - Freelance Available...
- Why Flash really rocks - digging out the past!
- Make montages with flash and google image
- iminlikewithyou - A cool social flash environment

Stay focussed
Del.icio.us
Reddit
Stumble it
Digg it


mark Says:
February 24th, 2009 at 12:23 amWhat is some for:
dat.data = some.object;
dat.structure = some.other.object;
How to use the “some”?
Thanks
Mark
Nicolas Noben Says:
February 24th, 2009 at 8:38 amHey Mark,
’some’ is whatever object you want to save. Think of it as myObject.
dat.structure = myStructureObject.
So that could be a XML, pretty sure, or Array, or String… Just not a DisplayObject (such as Button).
n