Adobe AIR read and write file System – Part1

AIR Tutorials, Getting Started Add comments

flash.filesystem package class provides Adobe AIR developers the ability read, write and manage files and folders.

The file classes are as follows

File  This object is used to represent the path of the file or directory for further action
FileMode  This class defines string constants (READ, WRITE, APPEND, UPDATE) used in the fileMode parameter of open() and openAsync() methods of FileStream class.

Example Usage
var fileOpened:File = File.desktopDirectory.resolvePath("abc.txt");
var newfileStream:FileStream = new FileStream();
newfileStream.openAsync(fileOpened, FileMode.READ);

FileStream  FileStream is used to read and write files. A file object points to a new or existing file and this is referred to FileStream for further actions like add,modify,delete etc (Refer the above example usage)

File Class

The File class extends the FileReference class.A file object can be used to point to a file or a directory
The File object as two properties

nativePath platform  specific ex: for windows “c:\somedirectory\somefile.txt” and for mac  “/somedirectory/somefile.txt” .Note the backslash usage is different for windows and mac.
url Uses file url scheme to point a file ex: for windows “file:///c:/somedirectory/somefile.txt”

By using the file or directory path from the File object you can edit, copy, move , delete files and directories.

The static properties included in File class are

File.applicationStorageDirectory  A unique storage directory unique for each installed AIR application
File.applicationDirectory  Directory where the application is installed and it is read only
File.desktopDirectory  Desktop directory of the User
File.documentsDirectory  Documents directory of the user
File.userDirectory  User Directory

The below example will point the file object to the desktop and lists all the files and directories available under desktop and displays  inside the list component.

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml"
    layout="absolute" title="List all files and folders from the desktop">
    <mx:Script>
		<![CDATA[
			import mx.collections.ArrayCollection; 
			import flash.filesystem.File;
 
			//declare array collection 'listfiles' dataprovider for the list//			
			[Bindable]
			private var listfiles:ArrayCollection=new ArrayCollection();
 
			private function listdesktopfiles():void{
 
			//point the file object 'desktop' to the desktop path//			
 
			var desktop:File = File.desktopDirectory;
			/* var desktop:File = File.desktopDirectory.resolvePath("Adobe AIR"); 
			will direct the path to the subdirectory 'Adobe AIR' available on the desktop */
 
			//getDirectoryListing() returns array of file objects from the file object pointer, desktop in this case//
			var getfiles:Array = desktop.getDirectoryListing();
 
			//Push all thi values to the Arraycollection to be diaplayed in the list//
			for (var i:int = 0; i < getfiles.length; i++) { 			  	 			  
  			  	listfiles.addItemAt((getfiles[i].nativePath), i);   
				}
			}		
		]]>
	</mx:Script>
	<mx:Style>
		.pad5{
			paddingTop:5;
			paddingLeft:5;
			paddingRight:5;
			paddingBottom:5
		}
	</mx:Style>
	<mx:VBox width="100%" height="100%" styleName="pad5">
	<mx:Button label="Show all files/folders on Desktop" click="listdesktopfiles()"/>
	<mx:List id="getlistings" dataProvider="{listfiles}" width="100%" height="100%"/>
	</mx:VBox>	
</mx:WindowedApplication>


Adobe AIR read and write file System – Part2

Disclaimer – The content provided is meant for learning purpose only.
www.adobeairtutorials.com is not responsible for any negative consequence that may result from using or implementing any information covered in the our tutorials or Articles.
www.adobeairtutorials.com does not guarantee/accuracy of the content contained in the site and reserves the right to correct any errors at any time without notice.

2 Responses to “Adobe AIR read and write file System – Part1”

  1. Vishwa Says:

    How will i do it with javascript?

  2. codelinkers Says:

    Hey Vishwa,

    Check this very usefull link at ajaxian http://ajaxian.com/archives/adobe-air-for-javascript-developers-pocketguide ,you can download the PDF Adobe® AIR™ for JavaScript Developers Pocket Guide and look for chapter4 File API section :)

Leave a Reply

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS