Stuff I am building:


Ted Patrick - Events & Community @ Adobe Systems


Note: This is the personal blog of Ted Patrick. The opinions and statements voiced here are my own.



FileIO - Central Learns to Read and Write

DIGG IT!     3 Comments Published Monday, September 20, 2004 at 6:12 PM .

The single best addition to Central 1.5 is FileIO. With FileIO you can read, write, upload, and download files directly within a Central App.&

To use FileIO you need to use the FileReference Class. Just like MovieClip or Object, FileRefence is a default class within the Central Player. To use it you need to create an instance:

myFile = new FileReference()

Simple, eh! But wait, FileReference instances do not do anything until you intialize the instance to a file. See the Class needs a file to work with and that is the essence of the initialization process. Initailization either invoves binding it to an exisitng file or creating one from scratch.

So lets intialize a file in the Central Cache by creating a file. The first argument is the path to the file and the second is to overwrite a file if it exists.

myFile.create("myFile.txt", true)

Central just created an empty file named "myFile.txt". If the application were installed from "central.powersdk.com" this file is located here:

C:\Documents and Settings\Theodore Patrick\Application Data\Macromedia\Central\#Central\{MAGICID}\Local Internet\central.powersdk.com\myFile.txt

(Files on Mac are stored on a similar path)

So we have a file, lets write some text into it!

myFile.writeString("Hello World, I am written with Central!")

Now files within the Central cache are fun but writing the file locally is much better. To do this we use the saveAs method to prompt the user to put the file onto their local file system.

myFile.saveAs()

When this executes, the user will recieve a "Save As" dialog box that looks similar to this one:


The last thing we need to do is close the FileReference like so.

myFile.close()

TADA, Central can read and write. What is amazing is that FileReferences can read and write binary files which dramatically expands your file capabilites.

I will post some other FileIO goodness soon.

Cheers,

Ted ;)

3 Responses to “FileIO - Central Learns to Read and Write”

  1. # Blogger Romain

    hey,

    I don't methods you have... I create a filereference var and i haven't create, save ... How do you have these methods  

  2. # Anonymous Tom

    First of all, you have to create a AIR project (because FileIO classes are only available with the AIR framework).

    Then you create your file like this:

    var myStream:FileStream = new FileStream ();
    myStream.open( new File (File.applicationResourceDirectory + File.separator + "jaja.txt"),FileMode.WRITE);

    And you can write:

    myStream.writeUTF("Laet I love you!");

    myStream.close();

    That all!  

  3. # Anonymous Tom

    Oups sorry! I forget something:

    To create a file:

    var myFile:File = new File (File.applicationResourceDirectory.nativePath + File.separator + "config2.txt");  

Post a Comment

Where to find me:

Ted on Twitter - @AdobeTed
Ted on Adobe Groups
Ted on LinkedIn
Ted on Facebook
Ted at Adobe


Latest

Lists

Links

Jobs

Flex Jobs
city, state, zip

Archives