AS3的URLRequest和文件流保存XML数据 - 错误#3013:文件或目录在使用中 [英] as3 URLRequest and file stream saving xml data - Error #3013: File or directory is in use

查看:952
本文介绍了AS3的URLRequest和文件流保存XML数据 - 错误#3013:文件或目录在使用中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的最终目标是要打开一个XML文件,修改数据,并保存它。我有一个URLRequest打开它。我能不过来改变XML的,我的问题是::当我试图用流打开,保存文件,然后它说:

 错误:错误#3013:文件或目录在使用中。
 

我想我可以用相同的流开,​​但那时我不知道如何让XML瓦尔就像我在我的剧本时,我使用的URLRequest。

所以,我要么需要一个)关闭XML文件,所以我可以用流打开它,或者b)用流和一些如何得到我所需要的增值经销商和操纵它们保存打开XML。有可能是AC),但我不认为它了。

下面是我的code:

 包COM
{
    进口flash.filesystem *。
    进口flash.system.System;

    进口的flash.display.Stage;
    进口flash.events *。
    进口flash.net *。
    导入flash.display使用*。


    公共类userSettings
    {

        公共变种pathToFile:字符串= File.desktopDirectory.resolvePath('userSettings.xml')nativePath的;
        公共变种someFile:文件=新的文件(pathToFile);
        公共变种流:的FileStream =新的FileStream();



        公共变种myLoader =新的URLLoader();

        公共职能userSettings()
        {
            loadSettings();
        }

        公共职能saveSettings(daFile:字符串)
        {
            stream.open(someFile,FileMode.WRITE);
            stream.writeUTFBytes(daFile);
            跟踪(!保存的设定+ daFile);
            //stream.close();
        }




公共职能loadSettings()
        {




            跟踪(pathToFile);


            myLoader.load(新的URLRequest(pathToFile));


            myLoader.addEventListener(引发Event.COMPLETE,的processXML);

            功能的processXML(五:事件):无效
            {
                VAR与myXML =新的XML(e.target.data);

                //获取从XML瓦尔。我需要这个选项。
                VAR homeT = myXML.Settings(@标题==homeT)。
                跟踪(和它的作品?+ homeT);

                            //改变瓦尔在XML
                            myXML.Settings(@标题==homeT)* =家庭2。;

                // Trace原理与改变XML
                             跟踪(与myXML);
                            ///现在试图挽救这是行不通的。
                saveSettings(与myXML);
            }

        }


/////////
    }

}
 

解决方案

据我的了解,你可以只读取流中的字节数,是这样的:

  VAR FILESTREAM:的FileStream =新的FileStream();
fileStream.open(文件,FileMode.READ);
VAR与myXML:XML = XML(fileStream.readUTFBytes(fileStream.bytesAvailable));
 

然后你有一个XML对象,你在你的例子一样,你可以操纵。

My end goal is to open an xml file, change data and save it. I am opening it with a URLRequest. I am able to change the xml, however, MY ISSUE IS: When I try to open it with a stream to save the file then it says:

Error: Error #3013: File or directory is in use.

I suppose I could open it with the same stream but then I don't know how to get the xml vars like I do in my script when I am using the URLRequest.

So I either need to a) close the xml file so I can open it with a stream or b) open the xml with a stream and some how get the vars I need and manipulate them for saving. There may be a c) but I can't think of it now.

Here is my code:

package com
{
    import flash.filesystem.*;
    import flash.system.System;

    import flash.display.Stage;
    import flash.events.*;
    import flash.net.*;
    import flash.display.*;


    public class userSettings
    {

        public var pathToFile:String = File.desktopDirectory.resolvePath('userSettings.xml').nativePath;
        public var someFile:File = new File(pathToFile);
        public var stream:FileStream = new FileStream();



        public var myLoader = new URLLoader();

        public function userSettings()
        {
            loadSettings();
        }

        public function saveSettings(daFile:String)
        {
            stream.open(someFile, FileMode.WRITE);
            stream.writeUTFBytes(daFile);
            trace("Settings SAVED! " +daFile);
            //stream.close();
        }




public function loadSettings()
        {




            trace(pathToFile);


            myLoader.load(new URLRequest(pathToFile));


            myLoader.addEventListener(Event.COMPLETE, processXML);

            function processXML(e:Event):void
            {
                var myXML = new XML(e.target.data);

                // Gets vars from xml. I need this option. 
                var homeT = myXML.Settings.(@Title=="homeT");
                trace("And it works? " + homeT);

                            // Changes vars in xml                   
                            myXML.Settings.(@Title == "homeT").* = "Home2";

                // trace works with changed xml
                             trace(myXML);
                            /// Now trying to save it doesn't work. 
                saveSettings(myXML);
            }

        }


/////////
    }

}

解决方案

As far I as know you could just read the bytes from the stream, something like:

var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.READ); 
var myXML:XML = XML(fileStream.readUTFBytes(fileStream.bytesAvailable)); 

Then you have an XML object that you can manipulate as you did in your example.

这篇关于AS3的URLRequest和文件流保存XML数据 - 错误#3013:文件或目录在使用中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆