首页 > 解决方案 > Animate CC AS3 如何在写入文件时管理数据

问题描述

我正在 Animate CC 2019 上构建一个程序,该程序需要访问一个可以读取和写入的文本文件。

除了已经存在的内容之外,我想存储来自输入文本的字符串和数字,然后将它们写入文件中。

我还需要能够管理数据在文件中的写入方式。因此,例如,我想要 2 个数组:Array1 存储名称,Array2 存储数字。所以当程序写入文件时,它需要在每一行写入数组中特定索引的对应值。所以如果Array1(0) = name1 & Array2(0) = 52,在文件中应该写:

name1 52

如果不完全像这样,一种可以以可管理的方式写入数据的方式。我正在 Windows 10/AIR 上尝试这个。

我的问题:

如何管理数据在文件中的写入方式?

我应该使用哪个代码?我找到了有关解决此问题的两种方法的信息:

一个。使用“文件”和“文件流”的代码不起作用!我收到错误消息:“找不到类型或不是编译时常量”,就像系统无法识别命令一样。这些命令在当前版本的 Animate CC 上是否仍然可用?

湾。确实有效的代码片段/AIR代码,但是,如上所述,我找不到管理数据如何写入文件的方法。每次在文件中写入内容时,先前的数据都会被覆盖。

下面的代码片段代码有效。

/* Click to Write to a Text File

Clicking on the specified object will prompt the user to select a location and file name and save a text string to it.

Instructions:

To set what data is saved to the file change "Text to save to file." to the data you wish to save.

*/

import flash.filesystem.File;

import flash.filesystem.FileMode;

import flash.filesystem.FileStream;

movieClip_1.addEventListener(MouseEvent.CLICK, fl_SaveFile);

var fl_FileDataToSave:String = "Text to save to file.";

var fl_SaveFileStream:FileStream = new FileStream(); // FileStream used to write to the file

var fl_SaveFileChooser:File = File.documentsDirectory; // Default to the documents directory

fl_SaveFileChooser.addEventListener(Event.SELECT, fl_WriteFileHandler);

// Opens a Save As dialog box for user to specify a file name

function fl_SaveFile(event:MouseEvent):void

{

fl_SaveFileChooser.browseForSave("Save As:");

}

// Write data in fl_FileDataToSave variable

function fl_WriteFileHandler(event:Event):void

{

fl_SaveFileChooser = event.target as File;

fl_SaveFileStream = new FileStream();

fl_SaveFileStream.openAsync(fl_SaveFileChooser, FileMode.WRITE);

fl_SaveFileStream.writeMultiByte(fl_FileDataToSave, File.systemCharset);

fl_SaveFileStream.close();

}

标签: actionscript-3animate-cc

解决方案


推荐阅读