`
#天琪#
  • 浏览: 159298 次
  • 性别: Icon_minigender_2
  • 来自: 长沙
社区版块
存档分类
最新评论

文本内容保存问题

    博客分类:
  • FLEX
阅读更多
将文本编辑器内容保存到本地的一个地址,转自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>



尚未测试~~~~~~~~~~~~~~
分享到:
评论

相关推荐

    网页一拖就保存文本内容

    标题"网页一拖就保存文本内容"所暗示的技术实现,主要是通过浏览器扩展或者特定软件工具来实现的。这种技术允许用户通过简单的操作,如鼠标拖拽,快速抓取网页上的文本并进行存储。 首先,我们来理解一下这个过程的...

    文本保存工具文本保存工具文本保存工具文本保存工具

    1. **分类管理**:用户可以根据内容类型或项目创建不同的文件夹或标签,将文本信息进行分类存储,便于日后快速定位所需内容。 2. **全文搜索**:内置的搜索功能使得用户可以通过关键词快速找到保存的文本,节省了...

    C#保存txt文本文件

    如果你需要追加文本而不是覆盖原有内容,可以使用`StreamWriter`的另一个构造函数,传入`true`作为第二个参数,表示以追加模式打开文件: ```csharp StreamWriter writer = new StreamWriter(filePath, true); ``` ...

    Java文本区内容保存为文件

    ### Java文本区内容保存为文件的知识点解析 #### 一、概述 在Java开发中,经常需要处理用户界面(UI)中的数据操作,如读取文本框中的内容并将其保存到文件中。本案例展示了如何使用Swing库创建一个简单的应用程序,...

    分类保存文本内容

    在IT行业中,文本内容的管理和保存是一项基础但至关重要的任务。尤其在大数据时代,高效地组织和分类文本信息能够极大地提升工作效率。"分类保存文本内容"这个主题涉及到软件技术的应用,它强调了如何通过特定的工具...

    AS3将文本文件保存到本地 仅5行代码 多么简单

    - **定义保存逻辑**:在事件处理函数中调用`fileRef.save`方法,传递需要保存的文本内容作为参数。 ### 五、注意事项 - 在实际应用中,还需要考虑权限问题。默认情况下,Flash Player可能不允许应用程序访问用户的...

    使用JavaScript保存文本文件到本地的两种方法

    1. 创建一个Blob对象,传入需要保存的内容和指定的内容类型(如果是要保存文本文件,则内容类型为"text/plain")。 2. 使用URL.createObjectURL方法创建一个指向该Blob对象的URL,这个URL可以用来指定元素的href...

    富文本编辑器保存网络图片到本地

    3. **图片的获取与保存**:当富文本编辑器中包含来自网络的图片时,为了确保最终生成的内容能够正常显示这些图片,通常需要将这些网络图片下载并保存到服务器的本地文件系统上,并更新 HTML 中的图片源地址。...

    火山安卓文本的保存与读取.rar

    values.put("content", "文本内容"); SQLiteDatabase db = openOrCreateDatabase("TextDB", MODE_PRIVATE, null); db.insert("TextTable", null, values); ``` 4. **ContentProvider**: 对于跨应用数据共享,...

    ABAP长文本的读取与插入

    长文本是指一段较长的文本内容,通常用于存储大段的文字信息。 读取长文本的方法是通过调用函数模块“READ_TEXT”来实现的。该函数模块将长文本数据读取到内存中,以便进行后续处理。读取长文本的过程可以分为以下...

    网页文本保存网页文本保存

    在这个过程中,我们经常需要将网页上的文本内容保存下来,以便后续查看或分析。以下是一些关于如何有效保存网页文本的知识点: 1. **浏览器内置功能**:大多数现代浏览器(如Chrome、Firefox、Safari)都提供了内置...

    C# Winform 发票信息修改 文本内容修改 指定文本覆盖 文件名修改 另存 保存

    在C# Winform开发中,有时我们需要处理一些与文件操作相关的任务,比如发票信息的修改、文本内容的替换、文件名的更改以及文件的另存和保存。这些功能在日常的业务系统中非常常见,特别是在财务软件或者文档管理软件...

    vb将文本框内容保存到txt文本.rar_VB TXT 文本_VB保存txt文本_vb TXT文本框_vb6_vb保存文本

    它有一个`Text`属性,可以读取或设置文本框内的文本内容。例如,如果你有一个名为`txtInput`的文本框,你可以通过`txtInput.Text`获取用户输入的内容。 2. **保存到TXT文件**: 要将文本框的内容保存到TXT文件,首先...

    文本内容保存EXCEL.py

    python实现文本内容按行转换成excel # 读取文件内容 def read(file): list=[] fo = open(file, "r") for line in fo: strinfo=re.compile(reStr) line = strinfo.sub('',line) # print(line) # 将字符串中=用...

    C#保存listbox中数据到文本文件的方法

    5. **遍历ListBox并写入数据**:通过`for`循环遍历`ListBox.Items`集合,将每一项的文本内容写入到文本文件中。`WriteLine`方法会在每行末尾添加换行符。 ```csharp int iCount = lst.Items.Count - 1; for (int i ...

    XML文件,批量进行替换文本内容

    2、可以支持文本替换,就是说,只要匹配其中的内容,就可以实现一键将原本的文本内容或者节点的内容,替换成需要替换的内容 可扩展的功能: 1、可以修改代码,将XML文件,替换成txt文件,或者其余类型的文件,都可以...

    基于串口屏LUA脚本—-文本保存功能V1.0

    根据给定文件信息,我们将详细介绍基于串口屏LUA脚本的文本保存功能V1.0。本文档涵盖了在设备掉电状态下如何保存数据的技术细节,以及如何通过用户界面输入和操作数据,确保数据在设备断电后仍能被保存和重新读取。 ...

    易语言源码取指定文本行内容.rar

    该程序的核心功能可能是通过读取用户输入的行号或者指定条件,从文本文件中定位到相应的行,并将其内容展示或保存。这涉及到文件流的读取、字符串的分割、行号的计算等技术。例如,程序可能会先打开文本文件,然后...

    matlab开发-保存字文本

    `saveWordAsText.m` 是一个MATLAB函数,它的功能是读取Word文档并将其内容保存为文本文件。这个功能通常由`docx2txt`或者`comms`工具箱中的COM接口来实现。MATLAB通过这些接口与Word应用程序进行交互,读取文档内容...

    Wireshark保存文档转为纯文本数据

    保存wireshark抓包的数据为txt文本,然后更改cpp文件中的文件路劲并运行

Global site tag (gtag.js) - Google Analytics