Home >
This excerpt is the first part of Chapter 8 of the upcoming (March 17, 2008 - NOW SHIPPING) book titled Beginning AIR: Building Applications for the Adobe Integrated Runtime ISBN: 0470229047. Beginning AIR is written in a walk through style where the examples are built upon throughout each section of the book. The easiest way to learn how to use the API's covered is to create the examples and follow along as they are built upon.
Part 1 of this series gave examples of how to work with directories within the file system. This excerpt covers how to use the File class to create, move, copy, and delete files. It will also demonstrate the usage of the FileStream class for working with the contents of individual files.
Listing 8-4: Empty Chapter8_File.mxml file used for the next example set.
In addition to the READ constant of the FileMode class, the class also has WRITE, UPDATE, and
APPEND constants. The definitions of these constants are as follows:
Create a File
Creating a new file is the same process as creating a directory. The following sample will create a new file by getting a reference with the File class and then opening the file with Write permissions using the FileStream and FileMode classes. Finally, the file is written out using the writeUTFBytes method, and the stream is closed. Starting with the empty Chaper8_file.mxml file, add the following
block:
Now add the following button:
The results can be seen in Figure 8-2.
Read a File
To read the file we just created back in, you will need to open the FileStream with the FileMode.READ constant. To demonstrate this, simply add the following function:
Since this example uses an Alert to show the results of the file read, you will also need to add an import statement.
Finally add another button to trigger the new function.
Be sure to create the file before attempting to read the file to avoid a file error. (Error #3003: File or directory does not exist.)
The results of the file read will look like Figure 8-3.
Update a File
Updating a file requires that it is opened in UPDATE mode. The following example will open a file in UPDATE mode and write a new chunk of data at a specific position in the file:
To test this, run the application and create the file using the Create File button. Next, click the Read File button and you should see something similar to Figure 8-3. Now, click the Update File button and then again click the Read File button, and you should now see something similar to Figure 8-4.
Append to a File
To append to a file, you should open the file with FileMode.APPEND. The following function will open the file in APPEND mode and will append some data to the file:
To test this, run the application and create the file using the Create File button. Next, click the Read File button and you should see something similar to Figure 8-3. Now, click the Update File button and then again click the Read File button, and you should see something similar to Figure 8-4. Finally, click the Append File button to append some text, and then hit the Read File button again. You should now see something similar to Figure 8-5.
<
Move a File
To move a file, use the moveTo method of the File class. Add the following function and button to the Chapter8_File.mxml file:
Be sure to create the file before attempting to move the file to avoid a file error. (Error #3003: File or directory does not exist.)
Copy a File
To copy a file, use the copyTo method of the File class. Add the following function and button to the Chapter8_File.mxml file:
Be sure to create the file before attempting to copy the file to avoid a file error. (Error #3003: File or directory does not exist.)
Delete a File
To copy a file, use the deleteFile method of the File class. Add the following function and button to the Chapter8_File.mxml file:
Be sure to create the file before attempting to delete the file to avoid a file error. (Error #3003: File or directory does not exist.)
Move a File to Trash
To copy a file, use the moveToTrash method of the File class. Add the following function and button to the Chapter8_File.mxml file:
Be sure to create the file before attempting to move a file to the trash to avoid a file error. (Error #3003: File or directory does not exist.) The completed code for Chapter8_File.mxml is shown in Listing 8-5.
The 3rd and final part of this series will cover Asynchronous versus Synchronous file system access.
Part 1 of this series gave examples of how to work with directories within the file system. This excerpt covers how to use the File class to create, move, copy, and delete files. It will also demonstrate the usage of the FileStream class for working with the contents of individual files.
Files
Working with files is very similar to working with directories, with the addition of the flash.filesystem.FileStream class for interactions with the contents of an individual file and the flash.filesystem.FileMode class for opening a file with specific permissions (read, write, etc.). The next group of samples will work with the same project and build upon it with each new piece of functionality. To get started, create a new AIR project and name it Chapter8_File. You should now have an empty file named Chapter8_File.mxml that looks like Listing 8-4. Please note that the completed code for Chapter8_File.mxml is shown in Listing 8-5.Listing 8-4: Empty Chapter8_File.mxml file used for the next example set.
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute">
</mx:WindowedApplication>
- FileMode.READ will open a file for read only. The file must exist to avoid a File I/O error.
- FileMode.WRITE will open a file with full Write permissions. If the file does not exist, it will be created. If the file does exist, its contents will be overwritten.
- FileMode.APPEND will open a file with full append access. If the file does not exist, it will be created. If the file does exist, any additions will occur at the end of any existing contents, and no data will be overwritten.
- FileMode.UPDATE will open a file with read and write access. If the file does not exist, it will be created. Only changed data will be overwritten when a file is open with UPDATE.
Create a File
Creating a new file is the same process as creating a directory. The following sample will create a new file by getting a reference with the File class and then opening the file with Write permissions using the FileStream and FileMode classes. Finally, the file is written out using the writeUTFBytes method, and the stream is closed. Starting with the empty Chaper8_file.mxml file, add the following
<mx:Script>
<![CDATA[
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;
import flash.filesystem.File;
private function createFile():void{
var newFile:File = File.desktopDirectory.resolvePath("MyNewFile.txt");
var fileStream:FileStream = new FileStream();
fileStream.open(newFile, FileMode.WRITE);
fileStream.writeUTFBytes("Beginning air");
fileStream.close();
}
]]>
</mx:Script>
<mx:Button label="Create File"
click="createFile()" y="10" horizontalCenter="0"/>
The results can be seen in Figure 8-2.
Read a File
To read the file we just created back in, you will need to open the FileStream with the FileMode.READ constant. To demonstrate this, simply add the following function:
private function readFile():void{
var myFile:File = File.desktopDirectory.resolvePath("MyNewFile.txt");
var fileStream:FileStream = new FileStream();
fileStream.open(myFile, FileMode.READ);
Alert.show(fileStream.readUTFBytes(fileStream.bytesAvailable),"Read File");
fileStream.close();
}
Since this example uses an Alert to show the results of the file read, you will also need to add an import statement.
import mx.controls.Alert;
Finally add another button to trigger the new function.
<mx:Button label="Read File"
click="readFile()" y="40" horizontalCenter="0"/>
Be sure to create the file before attempting to read the file to avoid a file error. (Error #3003: File or directory does not exist.)
The results of the file read will look like Figure 8-3.
Update a File
Updating a file requires that it is opened in UPDATE mode. The following example will open a file in UPDATE mode and write a new chunk of data at a specific position in the file:
private function updateFile():void{
var myFile:File = File.desktopDirectory.resolvePath("MyNewFile.txt");
var fileStream:FileStream = new FileStream();
fileStream.open(myFile, FileMode.UPDATE);
fileStream.position = 10;
fileStream.writeUTFBytes("AIR");
fileStream.close();
}
<mx:Button label="Update File"
click="updateFile()" y="70" horizontalCenter="0"/>
To test this, run the application and create the file using the Create File button. Next, click the Read File button and you should see something similar to Figure 8-3. Now, click the Update File button and then again click the Read File button, and you should now see something similar to Figure 8-4.
Append to a File
To append to a file, you should open the file with FileMode.APPEND. The following function will open the file in APPEND mode and will append some data to the file:
private function appendFile():void{
var myFile:File = File.desktopDirectory.resolvePath("MyNewFile.txt");
var fileStream:FileStream = new FileStream();
fileStream.open(myFile, FileMode.APPEND);
fileStream.writeUTFBytes(" by Rich Tretola");
fileStream.close();
}
<mx:Button label="Append File"
click="appendFile()" y="100" horizontalCenter="0"/>
To test this, run the application and create the file using the Create File button. Next, click the Read File button and you should see something similar to Figure 8-3. Now, click the Update File button and then again click the Read File button, and you should see something similar to Figure 8-4. Finally, click the Append File button to append some text, and then hit the Read File button again. You should now see something similar to Figure 8-5.
<
Move a File
To move a file, use the moveTo method of the File class. Add the following function and button to the Chapter8_File.mxml file:
private function moveFile():void{
var originalLoc:File = File.desktopDirectory.resolvePath("MyNewFile.txt");
var newLoc:File = File.desktopDirectory.resolvePath("MovedFile.txt");
originalLoc.moveTo(newLoc);
}
<mx:Button label="Move File"
click="moveFile()" y="130" horizontalCenter="0"/>
Be sure to create the file before attempting to move the file to avoid a file error. (Error #3003: File or directory does not exist.)
Copy a File
To copy a file, use the copyTo method of the File class. Add the following function and button to the Chapter8_File.mxml file:
private function copyFile():void{
var originalLoc:File = File.desktopDirectory.resolvePath("MyNewFile.txt");
var copyLoc:File = File.desktopDirectory.resolvePath("MyNewFileCopy.txt");
originalLoc.copyTo(copyLoc);
}
<mx:Button label="Copy File"
click="copyFile()" y="160" horizontalCenter="0"/>
Be sure to create the file before attempting to copy the file to avoid a file error. (Error #3003: File or directory does not exist.)
Delete a File
To copy a file, use the deleteFile method of the File class. Add the following function and button to the Chapter8_File.mxml file:
private function deleteFile():void{
var newFile:File = File.desktopDirectory.resolvePath("MyNewFile.txt");
newFile.deleteFile ();
}
<mx:Button label="Delete File"
click="deleteFile()" y="190" horizontalCenter="0"/>
Be sure to create the file before attempting to delete the file to avoid a file error. (Error #3003: File or directory does not exist.)
Move a File to Trash
To copy a file, use the moveToTrash method of the File class. Add the following function and button to the Chapter8_File.mxml file:
private function moveFileToTrash():void{
var newFile:File = File.desktopDirectory.resolvePath("MyNewFile.txt");
newFile.moveToTrash();
}
<mx:Button label="Move File to Trash"
click="moveFileToTrash()" y="220" horizontalCenter="0"/>
Be sure to create the file before attempting to move a file to the trash to avoid a file error. (Error #3003: File or directory does not exist.) The completed code for Chapter8_File.mxml is shown in Listing 8-5.
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute">
<mx:Script>
<![CDATA[
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;
import flash.filesystem.File;
import mx.controls.Alert;
/*
Create a new file
*/
private function createFile():void{
var newFile:File = File.desktopDirectory.resolvePath("MyNewFile.txt");
var fileStream:FileStream = new FileStream();
fileStream.open(newFile, FileMode.WRITE);
fileStream.writeUTFBytes("Beginning air");
fileStream.close();
}
/*
Read in a file
Note: Be sure to create the file first
*/
private function readFile():void{
var myFile:File = File.desktopDirectory.resolvePath("MyNewFile.txt");
var fileStream:FileStream = new FileStream();
fileStream.open(myFile, FileMode.READ);
Alert.show(fileStream.readUTFBytes(fileStream.bytesAvailable),"Read File");
fileStream.close();
}
/*
Update a file
*/
private function updateFile():void{
var myFile:File = File.desktopDirectory.resolvePath("MyNewFile.txt");
var fileStream:FileStream = new FileStream();
fileStream.open(myFile, FileMode.UPDATE);
fileStream.position = 10;
fileStream.writeUTFBytes("AIR");
fileStream.close();
}
/*
Append to a file
*/
private function appendFile():void{
var myFile:File = File.desktopDirectory.resolvePath("MyNewFile.txt");
var fileStream:FileStream = new FileStream();
fileStream.open(myFile, FileMode.APPEND);
fileStream.writeUTFBytes(" by Rich Tretola");
fileStream.close();
}
/*
Move a file
NOTE: Be sure to create the file
first before attempting to move
*/
private function moveFile():void{
var originalLoc:File = File.desktopDirectory.resolvePath("MyNewFile.txt");
var newLoc:File = File.desktopDirectory.resolvePath("MovedFile.txt");
originalLoc.moveTo(newLoc);
}
/*
Copy a file
NOTE: Be sure to create the file
first before attempting to copy
*/
private function copyFile():void{
var originalLoc:File = File.desktopDirectory.resolvePath("MyNewFile.txt");
var copyLoc:File = File.desktopDirectory.resolvePath("MyNewFileCopy.txt");
originalLoc.copyTo(copyLoc);
}
/*
Delete a file
NOTE: Be sure to create the file
first before attempting to delete it
*/
private function deleteFile():void{
var newFile:File = File.desktopDirectory.resolvePath("MyNewFile.txt");
newFile.deleteFile();
}
/*
Move a directory to Trash
NOTE: Be sure to create the file
first before attempting to move it to Trash
*/
private function moveFileToTrash():void{
var newFile:File = File.desktopDirectory.resolvePath("MyNewFile.txt");
newFile.moveToTrash();
}
]]>
</mx:Script>
<mx:Button label="Create File"
click="createFile()" y="10" horizontalCenter="0"/>
<mx:Button label="Read File"
click="readFile()" y="40" horizontalCenter="0"/>
<mx:Button label="Update File"
click="updateFile()" y="70" horizontalCenter="0"/>
<mx:Button label="Append File"
click="appendFile()" y="100" horizontalCenter="0"/>
<mx:Button label="Move File"
click="moveFile()" y="130" horizontalCenter="0"/>
<mx:Button label="Copy File"
click="copyFile()" y="160" horizontalCenter="0"/>
<mx:Button label="Delete File"
click="deleteFile()" y="190" horizontalCenter="0"/>
<mx:Button label="Move File to Trash"
click="moveFileToTrash()" y="220" horizontalCenter="0"/>
</mx:WindowedApplication>
The 3rd and final part of this series will cover Asynchronous versus Synchronous file system access.










Facebook Application Development
Another great post Rich,
I love the new aspects of being able to access the file system with ActionScript.
cheers,
Thanks Rhys,
I agree, although this was obviously a standard for desktop apps for years. It is really cool that we can now do the same with a familiar language like AS3.
Rich - Does your book cover using RemoteObject with BlazeDS from AIR?
It does not cover BlazeDS. It does show how to connect to Http services and RemoteObject via ColdFusion. Here is teh full table of contents:
http://blog.everythingflex.com/2008/01/18/beginning-air-in-the-can/
This post is really nice Rich... well i was working on a problem. can we create custom themes for Flash Air like having black border
Ajitpal,
I am not really sure what you mean? Are you referring to custom Chrome in place of the OS Chrome? If yes, than the answer is certainly and this is covered in my book.
It does not cover Blazes. It does show how to connect to HTTP services and Remote Object via Cold Fusion. Here is teh full table of contents:I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well. In fact your creative writing abilities has inspired me.Really the blogging is spreading its wings rapidly. Your write up is fine example of it. http://www.rouletttegaming.com