ACE
Ph: 00000013

Calendar

March 2007
S M T W T F S
  1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

Tag Cloud

360Flex actionscript air android apple as3 books conference contest cookbook deeplinking FileReference flash flash builder flash catalyst Flash Player flashplayer flashplayer10 Flex flex4 flexbuilder flickr fotobooth Frameworks google gumbo ImageDropR indexing InsideRIA iphone java localconnection max merapi microsoft mobile pixel bender riatrax SilverLight sqlite swf Thermo update framework updatemanager URLTestr

Categories

Archives

Recent Posts

Recent Comments


« Write your first file to the file system using Apollo | Main | Create your first Apollo Window »

File and FileStream within Apollo - full source code

By Rich Tretola | March 19, 2007 Print This Post Print This Post
2,227 views

filereadwrite.jpg

Local access to the file system is what separates Apollo from Flex/Flash. This simple sample demonstrates how to read and write files to the local file system.

Here is the function that loads a file:

1
2
3
4
5
6
7
8
9
10
11
private function loadFile():void{
var file:File = File.appStorageDirectory;
file = file.resolve("Files/" + fdg.selectedItem.name);
stream = new FileStream();
stream.open(file, FileMode.READ);
var str:String = stream.readUTFBytes(stream.bytesAvailable);
stream.close();
str = str.replace(File.lineEnding, "\n");
contents.text = str;
fileName.text = file.name;
}

Here is the function that saves the file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
private function saveFile():void{
var myPattern:RegExp = / /g;
var newFileName:String = fileName.text.replace('.txt','');
if(newFileName.length > 1){
var file:File = File.appStorageDirectory.resolve("Files/" + newFileName.replace(myPattern,'_') + ".txt");
var stream:FileStream = new FileStream()
stream.open(file, FileMode.WRITE);
var str:String = contents.text;
str = str.replace(/\r/g, File.lineEnding);
stream.writeUTFBytes(str);
stream.close();
fdg.directory = File.appStorageDirectory.resolve("Files/");
fileName.text = "";
contents.text = "";
mx.controls.Alert.show("File has been saved", "Notice");
} else {
mx.controls.Alert.show("File name is required", "Error Saving File");
}
}

Note that although this application uses File.appStorageDirectory, you also have easy access within Apollo to:

File.appResourceDirectory
File.appStorageDirectory
File.currentDirectory
File.desktopDirectory
File.documentsDirectory

Download the application

Download source code

Share this Post


Topics: Adobe AIR, Tutorials |

5 Responses to “File and FileStream within Apollo - full source code”

Critter Says:
March 26th, 2007 at 10:57 pm

what is the purpose of this line:
fdg.directory = File.appStorageDirectory.resolve(”Files/”)

in both functions?

everythingflex Says:
March 27th, 2007 at 3:25 am

Well, it is the way to get the path to a file. The two functions are looking at the same folder but different files. The loadFile function gets a reference to the selected filename within the Files folder.

var file:File = File.appStorageDirectory;
file = file.resolve(â€Files/†+ fdg.selectedItem.name);

The saveFile function gets a refernce to the new filename within the Files folder.

var file:File = File.appStorageDirectory.resolve(â€Files/†+ newFileName.replace(myPattern,’_’) + “.txtâ€);

MatteoNY Says:
June 20th, 2007 at 6:16 pm

The code works but I had to change the
“appStorageDirectory” to:”applicationStorageDirectory”
As well as the WindowedApplication used by Flex3.

I’m getting an error when I load “Windows - No Disk” Exception Processing Message c00000013 Parameters 75b6bf9c 4 75b6…. [cancel] [Try Again] [Continue]. If you continue the program works. I commented out the init() function to see if that would help but I am still getting the error. I hope this helps.

everythingflex Says:
June 20th, 2007 at 6:54 pm

This application was built for the Apollo alpha and does not play nicely with the AIR beta and many things have changed between builds.

Nancy Gill Says:
November 23rd, 2007 at 1:23 pm

Was the No Disk error ever solved? I am getting the same thing in my registered copy of FlexBuilder 2.

Comments



You are viewing a mobilized version of this site...
View original page here

Mobilized by Mowser Mowser
Mobilytics