Reading Files in ActionScript

Our first program will demonstrate reading files in ActionScript. Here’s the full source code:

package {
    import flash.display.Sprite;
    import flash.events.*;
    import flash.net.FileReference;
    import flash.net.FileFilter;
    import flash.utils.ByteArray;

    public class ReadingFiles extends Sprite {
        private var fileRef:FileReference;

        public function ReadingFiles() {
            fileRef = new FileReference();
            fileRef.addEventListener(Event.SELECT, onFileSelected);
            fileRef.addEventListener(Event.COMPLETE, onFileLoaded);

            var txtFilter:FileFilter = new FileFilter("Text Files (*.txt)", "*.txt");
            fileRef.browse([txtFilter]);
        }

        private function onFileSelected(event:Event):void {
            fileRef.load();
        }

        private function onFileLoaded(event:Event):void {
            var fileContent:String = fileRef.data.toString();
            trace("File content:");
            trace(fileContent);

            // Reading specific bytes
            var byteArray:ByteArray = fileRef.data as ByteArray;
            
            // Read first 5 bytes
            byteArray.position = 0;
            var b1:ByteArray = new ByteArray();
            byteArray.readBytes(b1, 0, 5);
            trace("5 bytes: " + b1.toString());

            // Read 2 bytes starting from position 6
            byteArray.position = 6;
            var b2:ByteArray = new ByteArray();
            byteArray.readBytes(b2, 0, 2);
            trace("2 bytes @ 6: " + b2.toString());

            // Read last 10 bytes
            byteArray.position = byteArray.length - 10;
            var b3:ByteArray = new ByteArray();
            byteArray.readBytes(b3, 0, 10);
            trace("Last 10 bytes: " + b3.toString());
        }
    }
}

This ActionScript example demonstrates various file reading operations. Here’s a breakdown of what the code does:

  1. We start by importing necessary classes and creating a FileReference object.

  2. In the constructor, we set up event listeners for file selection and loading, then open a file browse dialog with a filter for text files.

  3. When a file is selected, we load its contents.

  4. Once the file is loaded, we first read its entire content as a string and trace it.

  5. We then demonstrate reading specific parts of the file:

    • Reading the first 5 bytes
    • Reading 2 bytes starting from position 6
    • Reading the last 10 bytes
  6. For each of these operations, we use a ByteArray to read specific portions of the file and convert them to strings.

To run this program, you would typically compile it into a SWF file and run it in a Flash Player or AIR runtime environment. The exact process may vary depending on your development setup.

Note that ActionScript, being primarily used in Adobe Flash and AIR environments, handles file operations differently from many other languages. It uses an event-driven approach and has built-in security restrictions that require user interaction for file access.