【问题标题】:AS3: How do I save out the data from a datagrid into a line-by-line text file?AS3:如何将数据网格中的数据保存到逐行文本文件中?
【发布时间】:2012-12-27 14:28:41
【问题描述】:

我有一个填充了文本文件的数据网格,其中包含逐行单词(即每行一个)。数据网格有 5 列。文本文件中的每 5 个单词放在一个新行中。然后可以添加或删除数据网格。我将如何使用数据网格中的所有值重写文本文件?

这就是填充数据网格的方式。如何遍历整个数据网格单元格并重写文本文件中的数据?

var loadFavourites: URLLoader = new URLLoader();;
var arFavourites = new Array();

loadFavourites.addEventListener(Event.COMPLETE, onLoadedFavourites);
loadFavourites.load(new URLRequest("lists/Favourites.txt"));

function onLoadedFavourites(event:Event):void
{
    arFavourites = event.target.data.split("\r\n");

    for (var i:int = 0; i <= arFavourites.length; i++)
    {
        if (i != 0 && i % 5 == 0)
        {
            dg.addItem({Place: arFavourites[i-5],Subject:arFavourites[i-4],Object:arFavourites[i-3],Feeling:arFavourites[i-2],Action:arFavourites[i-1]});

        }
    }
}

添加记录:

var i:int=0;
    var tPlace = mc_places.mc_places_text.txtPlaces.text;
    var tSubject = mc_subject.mc_subject_text.txtSubject.text;
    var tObject = mc_object.mc_object_text.txtObject.text;
    var tFeeling = mc_feeling.mc_feeling_text.txtFeeling.text;
    var tAction = mc_action.mc_action_text.txtAction.text;
    arFavourites[FavCount+1]=tPlace;
    arFavourites[FavCount+2]=tSubject;
    arFavourites[FavCount+3]=tObject;
    arFavourites[FavCount+4]=tFeeling;
    arFavourites[FavCount+5]=tAction;
    dg.addItem({Place: arFavourites[FavCount+1],Subject:arFavourites[FavCount+2],Object:arFavourites[FavCount+3],Feeling:arFavourites[FavCount+4],Action:arFavourites[FavCount+5]});
    dg.scrollToIndex(dg.length-1);
    FavCount=FavCount+5;
    for (i=0; i<arFavourites.length; i++) {
                trace(arFavourites[i]);
            }

删除记录:

var k:int;
    var i:int;
    trace("Selected Index: " + dg.selectedIndex);
    if (dg.length == 0) {
        return;
    }
    for (k=0; k<dg.length; k++) {
        if (dg.isItemSelected(dg.getItemAt(k))) {
            for (i=0; i<arFavourites.length; i++) {
                if (arFavourites[i] == dg.getItemAt(k)[1]) {
                    arFavourites.splice(i,1);
                    }
                if (arFavourites[i] == dg.getItemAt(k)[2]) {
                    arFavourites.splice(i,1);
                    }
                if (arFavourites[i] == dg.getItemAt(k)[3]) {
                    arFavourites.splice(i,1);
                    }
                if (arFavourites[i] == dg.getItemAt(k)[4]) {
                    arFavourites.splice(i,1);
                    }
                if (arFavourites[i] == dg.getItemAt(k)[5]) {
                    arFavourites.splice(i,1);
                    }
            }
            dg.removeItemAt(k);
            dg.scrollToIndex(k);
            dg.clearSelection();
            break;
        }
    }

【问题讨论】:

  • 是否可以发布有关如何访问文本文件或填充DataGrid 的任何代码?另外,这是一个 AIR 应用程序还是只是一个 SWF?
  • 我希望对您有所帮助 :) 哦,它是一个空中应用程序。

标签: actionscript-3 datagrid air text-files save


【解决方案1】:

嗯,我会做的基本上是相反的。首先,您将获取 DataGrid 的值,然后按照与之前相同的顺序将它们放回 Array:

var newFavArray:Array = new Array();

for (var i:int = 1; i <= dg.length; i++)
{
    newFavourites[i * 5 - 5] = dg.getItemAt(i - 1).Place;
    newFavourites[i * 5 - 4] = dg.getItemAt(i - 1).Subject;
    newFavourites[i * 5 - 3] = dg.getItemAt(i - 1).Object;
    newFavourites[i * 5 - 2] = dg.getItemAt(i - 1).Feeling;
    newFavourites[i * 5 - 1] = dg.getItemAt(i - 1).Action;
}

(如果你能找到更好的方法来使用i 访问Array 和DataGrid,我很快就完成了。)

然后你把它转换成String:

var newFavString:String = newFavArray.join("\r\n");

然后最后将它保存到您想要的文件中(当然会覆盖旧值)。由于它是一个 AIR 应用程序,因此最好使用 File 和 FileStream 类:

var favFile:File = File.applicationStorageDirectory.resolvePath("lists/Favourites.txt");
var favStream:FileStream = new FileStream();

favStream.open(favFile, FileMode.WRITE);
favStream.writeUTFBytes(newFavString);
favStream.close();

我不确定您的工作流程是什么,但我个人会将所有这些都放在一个函数中:

function saveFavourites():void
{
    var newFavArray:Array = new Array();

    for (var i:int = 1; i <= dg.length; i++)
    {
        newFavArray[i * 5 - 5] = dg.getItemAt(i - 1).Place;
        newFavArray[i * 5 - 4] = dg.getItemAt(i - 1).Subject;
        newFavArray[i * 5 - 3] = dg.getItemAt(i - 1).Object;
        newFavArray[i * 5 - 2] = dg.getItemAt(i - 1).Feeling;
        newFavArray[i * 5 - 1] = dg.getItemAt(i - 1).Action;
    }

    var newFavString:String = newFavArray.join("\r\n");

    var favFile:File = File.applicationStorageDirectory.resolvePath("lists/Favourites.txt");
    var favStream:FileStream = new FileStream();

    favStream.open(favFile, FileMode.WRITE);
    favStream.writeUTFBytes(newFavString);
    favStream.close();
}

你可以随时运行。

如果您以前没有使用过File 和FileStream,那么不妨看看它们是如何工作的。它们仅在 AIR 中可用,但它们比 URLRequest 和 URLLoader 要好得多。您可能还想使用它们来加载文件。它们只对本地文件有用,如果文件在线或其他什么,那么我确定URLLoader 很好。

【讨论】:

  • 非常感谢!我正在做这样的事情。 (即使用数组作为主要来源)。删除记录和添加记录功能见上文。我也想知道,如何将返回到文本文件中的值分隔为每行一个?
  • 我在尝试写入文本文件时遇到此错误:SecurityError: fileWriteResource at flash.filesystem::FileStream/open() at TheIdeaFactoryv1_1_fla::MainTimeline/another()[TheIdeaFactoryv1_1_fla.MainTimeline::frame1:292] 位于这一行 favStream.open(favFile, FileMode.WRITE);
  • 好的,我做了更多研究,发现写入应用程序文件夹(即/lists/Favourites.txt)存在限制。我将var favFile:File = File.applicationDirectory.resolvePath("lists/Favourites.txt"); 更改为var favFile:File = File.documentsDirectory.resolvePath("lists/Favourites.txt"); 并在我的iOS 设备上对其进行了测试,但什么也没有……不工作……请你帮我找到解决方案。
  • 抱歉,完全忘记了这个限制(虽然在使用 browseForSave 时它似乎有效)。对于配置文件等,我假设 Favourites.txt 是,你应该使用 File.applicationStorageDirectory 代替。它基本上是您的应用程序的“AppData”文件夹。我以前从未为 iOS 开发过,但希望它可以工作(无论哪种方式,它都比File.documentsDirectory 好用得多)。
  • 还在答案中将applicationDirectory 更改为applicationStorageDirectory。
猜你喜欢
  • 1970-01-01
  • 2021-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多