Save File in Adobe Flex -AIR
<?xml version=”1.0″ encoding=”utf-8″?>
<mx:WindowedApplication xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute” creationComplete=”onCreate(event)”><mx:Script><![CDATA[import flash.filesystem.FileMode;import flash.filesystem.FileStream;import flash.filesystem.File;
private function onCreate(e:Event):void
{
var file:File = File.desktopDirectory; //Set the Directory location to Desktop
file = file.resolvePath("test.txt"); //File Name
var fileStream:FileStream = new FileStream(); // Create new FileStream to Perform Read or Write
fileStream.open(file, FileMode.WRITE); // Open file in WRITE Mode
fileStream.writeUTFBytes("Hello World"); //Write Hello World into test.txt
fileStream.close(); //Close the File Stream
}
]]></mx:Script>
</mx:WindowedApplication>
private function onCreate(e:Event):void
{
var file:File = File.desktopDirectory; //Set the Directory location to Desktop
file = file.resolvePath("test.txt"); //File Name
var fileStream:FileStream = new FileStream(); // Create new FileStream to Perform Read or Write
fileStream.open(file, FileMode.WRITE); // Open file in WRITE Mode
fileStream.writeUTFBytes("Hello World"); //Write Hello World into test.txt
fileStream.close(); //Close the File Stream
}
]]></mx:Script>
</mx:WindowedApplication>
This code creates a file named test.txt with content “Hello World” in desktop folder.
The desktopDirectory property provides a way to reference the desktop directory that works across platforms. If you set a File object to reference the desktop directory using the nativePath or url property, it will only work on the platform for which that path is valid Similarly documentDirectory /applicationStorageDirectory /applicationDirectory can be used to represent the folder.
var file:File = new File();
file = file.resolvePath(“c:\\test.txt”);
http://www.flex-blog.com/save-data-to-file-system-with-air-in-flex-4/
private function save():void
{
// Create file object
file = File.documentsDirectory.resolvePath(directory + "\\" + fileName);
// FileStream for writing the file
fileStream = new FileStream();
// Open the file in write mode
fileStream.open(file, FileMode.WRITE);
// Write the ArrayCollection object of persons to the file
fileStream.writeObject(persons);
// Close FileStream
fileStream.close();
}
private function read():void
{
// Get hte correct path
file = File.documentsDirectory.resolvePath(directory + "\\" + fileName);
// read the file if it exists
if(file.exists)
{
// FileStream for reading the file
fileStream = new FileStream();
// Open the file in read mode
fileStream.open(file, FileMode.READ);
// Read the ArrayCollection object of persons from the file
persons = fileStream.readObject() as ArrayCollection;
// Close the FileStream
fileStream.close();
}
else
{
// some sample data + save if file does not exist
persons = new ArrayCollection();
var person:Object = new Object();
person.firstName = "John";
person.lastName = "Doe";
persons.addItem(person);
person = new Object();
person.firstName = "Peter";
person.lastName = "Pan";
persons.addItem(person);
person = new Object();
person.firstName = "Captain";
person.lastName = "Hook";
persons.addItem(person);
save();
}
}
file = file.resolvePath(“c:\\test.txt”);
http://www.flex-blog.com/save-data-to-file-system-with-air-in-flex-4/
private function save():void
{
// Create file object
file = File.documentsDirectory.resolvePath(directory + "\\" + fileName);
// FileStream for writing the file
fileStream = new FileStream();
// Open the file in write mode
fileStream.open(file, FileMode.WRITE);
// Write the ArrayCollection object of persons to the file
fileStream.writeObject(persons);
// Close FileStream
fileStream.close();
}
private function read():void
{
// Get hte correct path
file = File.documentsDirectory.resolvePath(directory + "\\" + fileName);
// read the file if it exists
if(file.exists)
{
// FileStream for reading the file
fileStream = new FileStream();
// Open the file in read mode
fileStream.open(file, FileMode.READ);
// Read the ArrayCollection object of persons from the file
persons = fileStream.readObject() as ArrayCollection;
// Close the FileStream
fileStream.close();
}
else
{
// some sample data + save if file does not exist
persons = new ArrayCollection();
var person:Object = new Object();
person.firstName = "John";
person.lastName = "Doe";
persons.addItem(person);
person = new Object();
person.firstName = "Peter";
person.lastName = "Pan";
persons.addItem(person);
person = new Object();
person.firstName = "Captain";
person.lastName = "Hook";
persons.addItem(person);
save();
}
}
沒有留言:
張貼留言