0
votes

Need to load a file change it and then write the changes back into it. I get a File in use error. How do I make sure the file is closed before I can write on it:

        protected function getData():void
        {
            var urlLoader:URLLoader = new URLLoader();
            urlLoader.addEventListener(Event.COMPLETE, onComplete);
            urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
            urlLoader.load(new URLRequest(filePath));
        }

        protected function onComplete(e:Event):void
        {
            var data:XML = new XML((e.target as URLLoader).data);
            data.replace('User',<User></User>); 

            var stream:FileStream = new FileStream();
            stream.open(new File(filePath),FileMode.WRITE);
            stream.writeMultiByte('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\n<?xml-stylesheet type="text/xsl" href="template.xsl"?>\n'+data.toXMLString(),File.systemCharset);
            stream.close();
        }

I get the error Error #2044: Unhandled IOErrorEvent:. text=Error #3013: File or directory is in use. at views::JobsManagement/readyToClean()[C:\Users\David\Adobe Flash Builder 4.7\NxMobileInspect_FixingMemoryIssues\src\views\JobsManagement.mxml:190] at flash.events::EventDispatcher/dispatchEventFunction() at flash.events::EventDispatcher/dispatchEvent() at flash.net::URLLoader/onComplete()

Already tried opening with method openAsync did not work. Error comes as a error window instead of coming from debugger. Any ideas?

1
Could you open the file in read/write mode instead of using URLLoader and then reopening the file with new File()? - Brian
I've got a similar prob. How did you resolve this? Thanks, c.u. - Craig

1 Answers

0
votes

You can try using urlLoader.close() but personally I use a try catch and then a Timer dealy until the file is available for writing. You can of course limit the number of try catch used. Something like:

protected function onComplete(e:Event):void
{
    var data:XML = new XML((e.target as URLLoader).data);
    data.replace('User',<User></User>); 
    saveFile();
}

private function saveFile(e:Event = null):void
{
    try
    {
        //FileStream write
    }
    catch(er:Error)
    {
        //set a timer with listener saveFile and try again
    }
}