Notice: register_sidebar was called incorrectly. No id was set in the arguments array for the "Sidebar" sidebar. Defaulting to "sidebar-1". Manually set the id to "sidebar-1" to silence this notice and keep existing sidebar content. Please see Debugging in WordPress for more information. (This message was added in version 4.2.0.) in /home1/jccrosby/public_html/admin.crsby.com/wp-includes/functions.php on line 4139

Notice: The called constructor method for WP_Widget in mts_Widget_Tabs is deprecated since version 4.3.0! Use
__construct()
instead. in /home1/jccrosby/public_html/admin.crsby.com/wp-includes/functions.php on line 3894

Notice: The called constructor method for WP_Widget in mts_ad_widget is deprecated since version 4.3.0! Use
__construct()
instead. in /home1/jccrosby/public_html/admin.crsby.com/wp-includes/functions.php on line 3894

Notice: The called constructor method for WP_Widget in mts_ad_300_widget is deprecated since version 4.3.0! Use
__construct()
instead. in /home1/jccrosby/public_html/admin.crsby.com/wp-includes/functions.php on line 3894

Notice: The called constructor method for WP_Widget in swt_tweet_widget is deprecated since version 4.3.0! Use
__construct()
instead. in /home1/jccrosby/public_html/admin.crsby.com/wp-includes/functions.php on line 3894

Notice: The called constructor method for WP_Widget in Facebook_Like_Widget is deprecated since version 4.3.0! Use
__construct()
instead. in /home1/jccrosby/public_html/admin.crsby.com/wp-includes/functions.php on line 3894
Reading a file with ActionScript 3

Reading a file with ActionScript 3


Notice: Undefined index: mts_posttop_adcode in /home1/jccrosby/public_html/admin.crsby.com/wp-content/themes/playbook/single.php on line 19

The ALPHA release of Apollo, Adobe’s cross-operating system runtime allows for some pretty sweet functionality:

  • Drag-and-drop support
  • Rich clipboard access
  • Desktop and system
    shortcuts

What I was excited about was the File API. The ability to access, read and write to files is something that is completely necessary in a desktop applications, not to mention, exciting to just play with in ActionScript. Its also something that allows for those Web applications that will be ported to desktop applications, to break out of the constraints of the Web (browser security etc.).

I’d like to give a simple example of the File API and get you started on the how’s. Its not that difficult, so lets get started. I’ll show you the code…then explain.

Simple File Read Example:
[as]
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;
import flash.filesystem.File;

var myFile:File = File.appResourceDirectory; // Create out file object and tell our File Object where to look for the file
myFile = myFile.resolve(“mySampleFile.txt”); // Point it to an actual file

var fileStream:FileStream = new FileStream(); // Create our file stream
fileStream.open(myFile, FileMode.READ);

var fileContents:String = fileStream.readUTFBytes(fileStream.bytesAvailable); // Read the contens of the file
fileContents_txt.text = fileContents; // Display the contents. I’ve created a TextArea on the stage for display

fileStream.close(); // Clean up and close the file stream
[/as]
First, we’ll need to import the following classes:
[as]
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;
import flash.filesystem.File;
[/as]
Next, we’ll need to create our File object so we can let our application know where the file we want to read exists. We’ll use the file.appResourceDeirectory to locate the file in relation to our application, then the resolve method of the File object myFile to locate the actual file.
[as]
var myFile:File = File.appResourceDirectory;
myFile = myFile.resolve(“mySampleFile.txt”);
[/as]
Now we’ll need to actually open the file using the FileStream object. So, we create a new FileStream object, then call the open() method and pass it the File object as well as the FileMode of READ.
[as]
var fileStream:FileStream = new FileStream();
fileStream.open(myFile, FileMode.READ);
[/as]
We have our file open, so lets go ahead and read the contents of the file. The file is a text file, so we’ll simple read the file contents into a String variable using the readUTFBytes() method of out FileStream object. Depending on the type of file and the contents of that file, you will want to use a different read method. Like I said, we’ll keep it simple for now. The readUTFBytes() method requires the number of bytes to read. We’ll pass the bytesAvailable property of the FileStream object for this parameter. The bytesAvailable prooperty, in this case, is the total bytes for the file. Finally set the value returned from the readUTFBytes() call equal to our String variable.
[as]
var fileContents:String = fileStream.readUTFBytes(fileStream.bytesAvailable);
[/as]
Alright! Now we have our String variable that contains the contents of our text file. From this point you can do anything you want with it. In this example, I’ve just set the text property of a TextArea to the value of our String variable

There is one final thing to do, and that is clean up. Make sure you call the close() method of the FileStream object once you are done reading the file.
[as]
fileStream.close();
[/as]
So that is all you need to get started with the File API. There are a few more things that I’d like to go into detail about, so I’ll make sure to post about reading file types other than plain text. I’d also like to create an example of opening a file asynchronously, so look for posts on these subjects soon.


Notice: Undefined variable: defaults in /home1/jccrosby/public_html/admin.crsby.com/wp-content/themes/playbook/functions.php on line 340

Notice: Undefined index: mts_postend_adcode in /home1/jccrosby/public_html/admin.crsby.com/wp-content/themes/playbook/single.php on line 28

Notice: WP_Query was called with an argument that is deprecated since version 3.1.0! caller_get_posts is deprecated. Use ignore_sticky_posts instead. in /home1/jccrosby/public_html/admin.crsby.com/wp-includes/functions.php on line 4023
Comments
  1. asim
  2. Ven

Leave a Reply

Your email address will not be published. Required fields are marked *