问题
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