as3 URLRequest and file stream saving xml data - Error #3013: File or directory is in use

回眸只為那壹抹淺笑 提交于 2019-12-20 05:13:24

问题


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);
            }

        }


/////////
    }

}

回答1:


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.




回答2:


A slight delay is needed BEFORE using the FileStream class to open, write, close. Again, Windows only! I used a timer to call the function that writes to the location.

The answer can was found on this link: Adobe Air FileStream Error #3013: File or directory is in use



来源:https://stackoverflow.com/questions/20201099/as3-urlrequest-and-file-stream-saving-xml-data-error-3013-file-or-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!