文本内容保存问题

将文本编辑器内容保存到本地的一个地址,转自http://space.actionscript3.cn/html/43/t-3643.html

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        backgroundColor="0x0000FF"
        backgroundImage=""
        backgroundAlpha="0.75"
        title="Text Editor"
        applicationComplete="appCompleteHandler()">
        <mx:Script>
                <![CDATA[
                        import flash.filesystem.*;
                        import mx.events.*;
                        import mx.controls.Alert;
       
                        private var currentFile:File; // The currentFile opened (and saved) by the application
                        private var stream:FileStream = new FileStream(); // The FileStream object used for reading and writing the currentFile
                        private var defaultDirectory:File; // The default directory
                        [Bindable] public var dataChanged:Boolean = false; // Whether the text data has changed (and should be saved)

                        /**
                        * Called as the event handler for the applicationComplete event of the Application. This method
                        * sets the position and size of the main window.
                        */
                        private function appCompleteHandler():void
                        {
                                defaultDirectory = File.documentsDirectory;
                                stage.nativeWindow.width = Math.min(800, Capabilities.screenResolutionX - 60);
                                stage.nativeWindow.height = Capabilities.screenResolutionY - 60;
                                stage.nativeWindow.visible = true;
                        }
                        /**
                        * Called when the user clicks the Open button. Opens a file chooser dialog box, in which the
                        * user selects a currentFile.
                        */
                        private function openFile():void
                        {       
                                var fileChooser:File;
                                if (currentFile)
                                {
                                        fileChooser = currentFile;
                                }
                                else
                                {
                                        fileChooser = defaultDirectory;
                                }
                                fileChooser.browseForOpen("Open");
                                fileChooser.addEventListener(Event.SELECT, fileOpenSelected);
                        }
                        /**
                        * Called when the user selects the currentFile in the FileOpenPanel control. The method passes
                        * File object pointing to the selected currentFile, and opens a FileStream object in read mode (with a FileMode
                        * setting of READ), and modify's the title of the application window based on the filename.
                        */
                        private function fileOpenSelected(event:Event):void
                        {
                                currentFile = event.target as File;
                                stream = new FileStream();
                                stream.openAsync(currentFile, FileMode.READ);
                                stream.addEventListener(Event.COMPLETE, fileReadHandler);
                                stream.addEventListener(IOErrorEvent.IO_ERROR, readIOErrorHandler);
                                dataChanged = false;
                                title = "Text Editor - " + currentFile.name;
                                currentFile.removeEventListener(Event.SELECT, fileOpenSelected);
                        }
                        /**
                        * Called when the stream object has finished reading the data from the currentFile (in the openFile()
                        * method). This method reads the data as UTF data, converts the system-specific line ending characters
                        * in the data to the "\n" character, and displays the data in the mainTextField Text component.
                        */
                        private function fileReadHandler(event:Event):void
                        {
                                var str:String = stream.readUTFBytes(stream.bytesAvailable);
                                stream.close();
                                var lineEndPattern:RegExp = new RegExp(File.lineEnding, "g");
                                str = str.replace(lineEndPattern, "\n");
                                mainTextField.text = str;
                                stream.close();
                        }
**
                        * Called when the user clicks the "Save" button. The method sets up the stream object to point to
                        * the currentFile specified by the currentFile object, with save access. This method converts the "\r" and "\n" characters
                        * in the mainTextField.text data to the system-specific line ending character, and writes the data to the currentFile.
                        */
                        private function saveFile():void
                        {
                                if (currentFile) {
                                        if (stream != null)       
                                        {
                                                stream.close();
                                        }
                                        stream = new FileStream();
                                        stream.openAsync(currentFile, FileMode.WRITE);
                                        stream.addEventListener(IOErrorEvent.IO_ERROR, writeIOErrorHandler);
                                        var str:String = mainTextField.text;
                                        str = str.replace(/\r/g, "\n");
                                        str = str.replace(/\n/g, File.lineEnding);
                                        stream.writeUTFBytes(str);
                                        stream.close();
                                        dataChanged = false;
                                }
                                else
                                {
                                        saveAs();
                                }
                        }
                        /**
                        * Called when the user clicks the "Save As" button. Opens a Save As dialog box, in which the
                        * user selects a currentFile path. See the FileSavePanel.mxml currentFile.
                        */
                        private function saveAs():void
                        {
                                var fileChooser:File;
                                if (currentFile)
                                {
                                        fileChooser = currentFile;
                                }
                                else
                                {
                                        fileChooser = defaultDirectory;
                                }
                                fileChooser.browseForSave("Save As");
                                fileChooser.addEventListener(Event.SELECT, saveAsFileSelected);
                        }
                        /**
                        * Called when the user selects the file path in the Save As dialog box. The method passes the selected
                        * currentFile to the File object and calls the saveFile() method, which saves the currentFile.
                        */
                        private function saveAsFileSelected(event:Event):void
                        {
                                currentFile = event.target as File;
                                title = "Text Editor - " + currentFile.name;
                                saveFile();
                                dataChanged = false;
                                currentFile.removeEventListener(Event.SELECT, saveAsFileSelected);
                        }
                        /**
                        * Called when the user clicks the "New" button. Initializes the state, with an undefined File object and a
                        * blank text entry field.
                        */
                        private function newFile():void
                        {
                                currentFile = undefined;
                                dataChanged = false;
                                mainTextField.text = "";
                        }
                        /**
                        * Handles I/O errors that may come about when opening the currentFile.
                        */
                        private function readIOErrorHandler(event:Event):void
                        {
                                Alert.show("The specified currentFile cannot be opened.", "Error", Alert.OK, this);
                        }
                        /**
                        * Handles I/O errors that may come about when writing the currentFile.
                        */
                        private function writeIOErrorHandler(event:Event):void
                        {
                                Alert.show("The specified currentFile cannot be saved.", "Error", Alert.OK, this);
                        }
                ]]>
        </mx:Script>
        <mx:HBox width="100%">
                <mx:Button label="New" id="newBtn" click="newFile()"/>
                <mx:Button label="Open" id="openBtn" click="openFile()"/>
                <mx:Button label="Save" id="saveBtn" click="saveFile()" enabled="{dataChanged}"/>
                <mx:Button label="Save As" id="saveAsBtn" click="saveAs()"/>
        </mx:HBox>
        <mx:TextArea id="mainTextField" width="100%" height="100%" fontFamily="Courier, _typewriter" change="dataChanged = true;"/>
        </mx:WindowedApplication>



尚未测试~~~~~~~~~~~~~~

你可能感兴趣的:(Flash,Access,UP)