如何将Array写入文件

时间:2012-07-25 09:21:29

标签: actionscript-3 flash air builder

我为android制作一个简单的程序,可以添加,读取和写入文本行到txt文件中并在List上显示它。 我得到了saveFile();函数是什么将todo_items数组写入文件todo.txt。 但由于某些原因而不是在列表中显示真实的文本行,它给了我数字...... 例如,当我添加“饲料狗,饲料猫,饲料鱼,饲料鸟”,然后删除结果中的3d项目,它给我“0,1,2”。不知道为什么。这是函数的代码

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
           xmlns:s="library://ns.adobe.com/flex/spark"
           height="494" creationComplete="readFile()"
           >
<fx:Script>
    <![CDATA[
        public var intTemp:int;
        import mx.collections.ArrayCollection;
        [Bindable] public var todo_items:ArrayCollection;   
        private function readFile():void
        {
            var todoFile:File =File.applicationStorageDirectory.resolvePath("todo.txt");


                var fs:FileStream = new FileStream();
                fs.open(todoFile, FileMode.READ);
                var result:String = fs.readUTFBytes(fs.bytesAvailable);
                var items:Array = result.split("\n");
                items.pop();
                todo_items = new ArrayCollection(items);
                fs.close();

        }
        private function writeFile():void
        {
            var todoFile:File = File.applicationStorageDirectory.resolvePath("todo.txt");


            var fs:FileStream = new FileStream();
            fs.open(todoFile, FileMode.APPEND);


            fs.writeUTFBytes(task_txt.text + "\n");


            fs.close();
            readFile();
        }

        private function deleteItem():void
        {           
                var todoFile:File =File.applicationStorageDirectory.resolvePath("todo.txt");
                var fs:FileStream = new FileStream();
                fs.open(todoFile, FileMode.READ);
                var result:String = fs.readUTFBytes(fs.bytesAvailable);
                var items:Array = result.split("\n");
                items.pop();
                todo_items = new ArrayCollection(items);
                todo_items.removeItemAt(intTemp);
                fs.close();
                safeFile();
        }
        private function safeFile():void
        {

            var todoFile:File = File.applicationStorageDirectory.resolvePath("todo.txt"); 


            var fs:FileStream = new FileStream(); 
            fs.open(todoFile, FileMode.WRITE); 

            for(var item:String in todo_items)
            {
                fs.writeUTFBytes(item + "\n")
            }

            fs.close(); 
            readFile(); 



        }   

    ]]>
</fx:Script>


<s:List id="todo_list" left="10" right="10" top="142" bottom="87" dataProvider="{todo_items}"/>
<s:Button left="11" right="10" top="69" height="65" label="Save task" click="writeFile(); task_txt.text = null"
          enabled="{task_txt.text.length > 0}"/>
<s:TextInput id="task_txt" left="10" right="10" top="10" height="51" prompt="Specify a task"/>
<s:Button left="10" right="10" bottom="14" label="Delete"
          click="intTemp = todo_list.selectedIndex; todo_items.removeItemAt(todo_list.selectedIndex); deleteItem()"
          enabled="{todo_list.selectedIndex != -1}"/>


</s:Application>

1 个答案:

答案 0 :(得分:0)

好吧,for(var item in array)item设置为数组中的所有键,即0,1,2,......

要获取实际项目,您需要执行以下操作之一:

for(var item:String in todo_items)
{
    fs.writeUTFBytes(todo_items[item] + "\n")
}

for each(var item:String in todo_items)
{
    fs.writeUTFBytes(item + "\n")
}

我推荐第二个,因为它更干净。