Writing Files in ActionScript

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

package {
    import flash.display.Sprite;
    import flash.filesystem.File;
    import flash.filesystem.FileMode;
    import flash.filesystem.FileStream;

    public class WritingFiles extends Sprite {
        public function WritingFiles() {
            writeFiles();
        }

        private function writeFiles():void {
            // To start, here's how to dump a string into a file.
            var file1:File = File.documentsDirectory.resolvePath("dat1.txt");
            var stream1:FileStream = new FileStream();
            stream1.open(file1, FileMode.WRITE);
            stream1.writeUTFBytes("hello\nactionscript\n");
            stream1.close();

            // For more granular writes, open a file for writing.
            var file2:File = File.documentsDirectory.resolvePath("dat2.txt");
            var stream2:FileStream = new FileStream();
            stream2.open(file2, FileMode.WRITE);

            // You can write byte arrays as you'd expect.
            var bytes:ByteArray = new ByteArray();
            bytes.writeUTFBytes("some\n");
            stream2.writeBytes(bytes);
            trace("wrote " + bytes.length + " bytes");

            // A writeUTFBytes is also available.
            stream2.writeUTFBytes("writes\n");
            trace("wrote 7 bytes");

            // Use close to ensure all buffered operations have
            // been applied to the underlying file.
            stream2.close();

            // ActionScript doesn't have a built-in buffered writer,
            // but you can implement your own if needed.
        }
    }
}

This ActionScript code demonstrates how to write files using the FileStream class. Here’s a breakdown of what the code does:

  1. We start by writing a simple string to a file named “dat1.txt” in the user’s documents directory.

  2. For more granular writes, we open another file named “dat2.txt”.

  3. We write a byte array to the file using writeBytes().

  4. We then use writeUTFBytes() to write a string directly to the file.

  5. Finally, we close the file stream to ensure all data is written to the file.

Note that ActionScript doesn’t have a built-in buffered writer like some other languages, but you can implement your own if needed for performance reasons when writing large amounts of data.

To run this code, you would need to set up a Flash or AIR project in an ActionScript development environment like Flash Builder or FlashDevelop. The code would execute when the SWF is run, writing the files to the user’s documents directory.

After running the code, you could check the contents of the written files in your documents folder:

$ cat ~/Documents/dat1.txt
hello
actionscript

$ cat ~/Documents/dat2.txt
some
writes

This example demonstrates basic file writing operations in ActionScript. Remember that when working with files in ActionScript, you need to consider security restrictions, especially when running in a browser environment.