使用一个文件流保存两种数据?

save 2 kinds of data using one filestream?

我正在保存 2 种数据(文本和图像)。是否可以使用 1 个文件流同时保存 2 种类型的数据?例子

var myTextFileObj:Object = new Object()
myTextFileObj["Name"]="George Borming" 
var imgByteData:ByteArray = jpgEncoder.encode(myBitmapData);//lets assume i have a bitmap data here

var folder:File = File.documentsDirectory.resolvePath("myApp/images/image.jpg");
var folder2:File = File.documentsDirectory.resolvePath("myApp/textfiles/mytext.txt");

fileStream = new FileStream();  
fileStream.addEventListener(Event.CLOSE, saveImg_FS_fileClosed);
fileStream.addEventListener(Event.COMPLETE, saveImg_FS_completeHandler); 
fileStream.addEventListener(ProgressEvent.PROGRESS, saveImg_FS_progressHandler); 
fileStream.addEventListener(IOErrorEvent.IO_ERROR, saveImg_FS_errorHandler); 

fileStream.openAsync(folder, FileMode.UPDATE);  
fileStream.writeBytes(imgByteData)

fileStream.openAsync(folder2, FileMode.UPDATE);  
fileStream.writeObject(myTextFileObj);
fileStream.close();

这行得通吗?如果我只创建 2 个文件流并单独保存数据,是否可能或更好。如果可以,我的代码有误,请教我正确的代码。

当然有可能。不过,可能的问题是您正在使用 openAsync,因此当您尝试打开另一个文件并且在打开第二个文件之前没有关闭第一个文件时,文件流操作仍在进行中。

您需要等待第一个操作完成,然后再开始第二个操作,或者使用[=16]同步打开文件 =] 而不是 openAsync

例如:

var folder:File = File.documentsDirectory.resolvePath("myApp/images/image.jpg");

fileStream = new FileStream();  
fileStream.addEventListener(Event.CLOSE, saveImg_FS_fileClosed);
fileStream.addEventListener(Event.COMPLETE, saveImg_FS_completeHandler); 
fileStream.addEventListener(ProgressEvent.PROGRESS, saveImg_FS_progressHandler); 
fileStream.addEventListener(IOErrorEvent.IO_ERROR, saveImg_FS_errorHandler); 

fileStream.open(folder, FileMode.UPDATE);  //use open instead of open Async
fileStream.writeBytes(imgByteData)
fileStream.close(); //close the current File so you can open a new one

folder = File.documentsDirectory.resolvePath("myApp/textfiles/mytext.txt");
fileStream.open(folder, FileMode.UPDATE);  
fileStream.writeObject(myTextFileObj);
fileStream.close();

如果您的两个文件总是一起打开/保存,您可能会发现为要保存的项目使用自定义 class/model,然后使用 FileStream.writeObject 序列化 class 到一个文件。

像这样:

package {
    import flash.utils.ByteArray;

    public class SaveData {
        public var textFile:Object;
        public var imageByteData:ByteArray;
        //any other properties
    }
}

然后,您可以这样做:(在您的构造函数中或任何地方)

flash.net.registerClassAlias("SaveData",SaveData);
flash.net.registerClassAlias("flash.utils.ByteArray",ByteArray);
//you have to register every non primitive class referenced by SaveData (including nested classes)

然后要将其保存到文件中,您需要按照以下步骤进行操作:

var myTextFileObj:Object = new Object()
myTextFileObj["Name"]="George Borming";

var mySaveData:SaveData = new SaveData();
mySaveData.textFile = myTextFileObj;
mySaveData.imageByteData = jpgEncoder.encode(myBitmapData);//lets assume i have a bitmap data here

var folder:File = File.documentsDirectory.resolvePath("myApp/mySaveData.data");

fileStream = new FileStream();  
fileStream.addEventListener(Event.CLOSE, saveImg_FS_fileClosed);
fileStream.addEventListener(Event.COMPLETE, saveImg_FS_completeHandler); 
fileStream.addEventListener(ProgressEvent.PROGRESS, saveImg_FS_progressHandler); 
fileStream.addEventListener(IOErrorEvent.IO_ERROR, saveImg_FS_errorHandler); 

fileStream.openAsync(folder, FileMode.WRITE);  

//serialize it to the file with writeObject
fileStream.writeObject(mySaveData);
fileStream.close();

//then later when you want to read it back in
var mySaveData:SaveData = fileStream.readObject();
trace(mySaveData.textFile["Name"]); //George Borming

这样您就可以在 1 个文件中拥有任意数量的对象。