2 Replies Latest reply on Sep 2, 2009 4:45 PM by marauderr

    parsing a textfile?


      Is this at all possible using an AIR application?


      open text file

      read text file

      sort into organized information

      and then upload to mysql database on a website


      Im new and all, but from what I gather the real power behind flex and air is the ability to use actionscript. the only programming experience i have is with PHP. I've had a hard time finding tutorials on actionscript for non-flash implementation, so im just wondering if this is even possible. i assume i'd have to use regex to sort the text file line by line into various arraycollections right?


      i'm not looking for handouts on code, just a step in the right direction - even links to where i can read about what i need to be doing.


      many thanks!

        • 1. Re: parsing a textfile?
          camfieldaj Level 1

          The simple answer is "Yes, it is possible." My background has been mainly in Perl/PHP, but I had no problem getting used to ActionScript in Flex. The help files included with the product are pretty good and there is a fair amount of online documentation. I get the sense that ActionScript is more Java like, but if you're comfortable with the *nix type languages(C, Perl, etc.) the syntax is pretty close and you won't feel out of place.

          On the other hand, I see Flex as more of a front end type product, whereas I now use PHP and Perl for complicated backend functions. It's pretty much up to you. I've done a couple of AIR apps where all the logic and computation was done in AS and I could not complain about the perfomance of the end product. The size of the files that were being accessed and processed were not small, so I was a little surprized.


          Good luck.

          1 person found this helpful
          • 2. Re: parsing a textfile?
            marauderr Level 1

            hey, thanks for the reply.

            actionscript syntax isnt too hard to get accustomed with as you mentioned. the only thing which may take a bit of time is finding built in functions for which i've found the language reference very handy (http://livedocs.adobe.com/flex/3/langref/)


            here's what i have so far:


            <?xml version="1.0" encoding="utf-8"?>
            <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
                width="700" height="700" borderColor="#0F273B" backgroundGradientAlphas="[1.0, 1.0]" backgroundColor="#333333">
                    import mx.collections.ArrayCollection;
                    import flash.filesystem.*;
                    import flash.events.*;
                    import mx.controls.*;


                    private var fileOpened:File = File.desktopDirectory;
                    private var fileContents:String;
                    private var stream:FileStream;
                    private var array:ArrayCollection = new ArrayCollection();
                    private function selectFile(root:File):void {
                        var filter:FileFilter = new FileFilter("Text", "*.txt");
                        root.browseForOpen("Open", [filter]);
                        root.addEventListener(Event.SELECT, fileSelected);
                    private function fileSelected(e:Event):void {
                        var path:String = fileOpened.nativePath;
                        filePath.text = path;
                        stream = new FileStream();
                        stream.addEventListener(ProgressEvent.PROGRESS, fileProgress);
                        stream.addEventListener(Event.COMPLETE, fileComplete);
                        stream.openAsync(fileOpened, FileMode.READ);


                    private function fileProgress(p_evt:ProgressEvent):void {
                        fileContents += stream.readMultiByte(stream.bytesAvailable, File.systemCharset);
                        readProgress.text = ((p_evt.bytesLoaded/1048576).toFixed(2)) + "MB out of " + ((p_evt.bytesTotal/1048576).toFixed(2)) + "MB read";
                    private function fileComplete(p_evt:Event):void {
                        //fileText.text = fileContents;
                    private function process(c:String):void {
                        if(!c.length > 0) {
                            Alert.show("File contents empty!", "Error");
                        array += c.split(/\n/);
                        for(z = 0; z <= array.length; z++) {
                            fileText.text = array[z];
                <mx:Text x="10" y="10" id="filePath" text="Select a file..." width="678" height="22" color="#FFFFFF"  fontWeight="bold"/>
                <mx:Button x="10" y="40" label="Browse" click="selectFile(fileOpened)" color="#FFFFFF" fontWeight="bold" fillAlphas="[1.0, 1.0]" fillColors="[#E2E2E2, #484848]"/>
                <mx:Button x="86" y="40" label="Process" click="process(fileContents)" color="#FFFFFF" fontWeight="bold"  fillAlphas="[1.0, 1.0]" fillColors="[#E2E2E2, #484848]"/>
                <mx:TextArea x="10" y="70" id="fileText" width="678" height="333" editable="false"/>
                <mx:Label x="10" y="411" id="readProgress" text="" width="678" height="19" color="#FFFFFF"/>


            the stuff i commented out was freezing the application (outputting file contents into a text area). the file is about 100mb but you mentioned you used large files as well so maybe thats not the problem.


            getting there slowly!