开发者

How to save to a file? Using AS3 on iOS (beginner)

I am trying to save a single word to a file, t开发者_开发技巧hen read that word to confirm success. When the code to read the file is run, it returns no data. And, I am not able to verify that any file is created.

Here is the code:

// This is run at the beginning of the frame, and this does display Hello in a textArea

var str:String = "Hello\n";
feedback.text = str;



// This is run when the user taps a button

var myFile:File = File.applicationStorageDirectory.resolvePath("./eFiveV3/text.txt");

var fileStream:FileStream = new FileStream();


fileStream.open(myFile,FileMode.WRITE);

fileStream.writeUTF(str);

fileStream.close();

str = "Goodbye\n";


// if the code below is run, then the feedback box will be empty. if the code below is commented out, then the feedback box will contain the text: Goodbye

fileStream.open(myFile,FileMode.READ);

str = fileStream.readMultiByte(myFile.size,File.systemCh arset);

fileStream.close();

feedback.text = str;

Any help would be greatly appreciated :)


I would try a couple diagnostic things, but I can wager a guess why you are seeing that problem.

But first, I would try setting feedback.text to myFile.size.toString() and see if it says there are any bytes at all in the file. If that comes back with something > 0 then I would wager that the following is going on:

You are using writeUTF() when writing the string, but you are using readMultiByte() when reading the string back in. When you use writeUTF() it doesn't just write the string to the file directly, it prepends the stream with the length of the string (as a 16 bit integer), where as readMultiByte() is trying to coerce those bytes directly into a string with the given charset. Those extra bytes at the beginning might be causing readMultiByte() to fail or screw up (it would be trying to read 0x06,0x00,0x00,0x00,H,e,l,l,o,\n rather than just H,e,l,l,o,\n). writeUTF() can be used to write multiple strings to the same file, and then read them back out of the stream with readUTF().

You probably want to pair your read and write methods correct, either use writeUTF() and readUTF() or use writeMultiByte() and readMultiByte(). If you are trying to write a pure text file, then writeMultiByte() is going to the be the way to go. If you are trying to serialize a bunch of different data into a single file, then writeUTF() is the way to go.

If the file size is in fact zero, then I'll have to dig a little more and see whats going on.


Here's some code with some event listeners that verify when the file has been written and read.

function saveFile():void{  

    var file:File = File.documentsDirectory.resolvePath("text.txt");

    var fileStream:FileStream = new FileStream();  

    fileStream.openAsync(file, FileMode.WRITE);  
    fileStream.writeUTFBytes("Hi this file was saved from AIR application without dialog");  
    fileStream.addEventListener(Event.CLOSE, fileClosed);  
    fileStream.close();  

    function fileClosed(event:Event):void { 

        trace("File Saved");  

    }        
}  

function readFile():void{  

    var file:File = File.documentsDirectory.resolvePath("text.txt");

    var fileStream:FileStream = new FileStream();  

    fileStream.openAsync(file, FileMode.READ);  
    var str:String = fileStream.readMultiByte(file.size,File.systemCharset);
    fileStream.addEventListener(Event.CLOSE, fileClosed);  
    fileStream.close();  

    function fileClosed(event:Event):void { 

        trace("File Contents:" + str);  

    }        
} 

saveFile();

readFile();

Don't thank me, thank Ray: source

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜