PDA

View Full Version : Read / Write


xwielder
01-15-2008, 07:04 PM
Suppose I have the following code:

var something:String = "";

myButton.addEventListener(MouseEvent.CLICK, iGotClicked);

function iGotClicked (event:MouseEvent)
{
something = "Happy Day";
writeLastLocation ();
}

// WRITE
var writeFileStream:FileStream = new FileStream();
function writeLastLocation ()
{
var writeFile:File = File.applicationStorageDirectory;
var wrietFileString:String = (writeFile.nativePath + "\\FileLastLocation.txt");
writeFile = writeFile.resolvePath(wrietFileString);
writeFileStream.addEventListener(Event.COMPLETE, completedWriteLastLocation);
writeFileStream.openAsync (writeFile, FileMode.WRITE);
}
function completedWriteLastLocation(event:Event):void
{
trace("Hello");
writeFileStream.writeUTFBytes (something);
writeFileStream.close ();
writeFileStream.removeEventListener(Event.COMPLETE , completedWriteLastLocation);
readLastLocation ();
}

// READ
var readString:String = "";
var readFileStream:FileStream = new FileStream();
function readLastLocation ()
{
var readFile:File = File.applicationStorageDirectory;
var readFileString:String = (readFile.nativePath + "\\FileLastLocation.txt");
readFile = readFile.resolvePath(readFileString);
readFileStream.addEventListener(Event.COMPLETE, completedReadLastLocation);
readFileStream.openAsync(readFile, FileMode.READ);
}
function completedReadLastLocation(event:Event):void
{
trace("World");
readString = readFileStream.readMultiByte(readFileStream.bytesA vailable, "iso-8859-1");
readFileStream.close ();
readFileStream.removeEventListener(Event.COMPLETE, completedReadLastLocation);
trace("readString = " + readString);
}


According to Adobe's documents: http://livedocs.adobe.com/labs/air/1/devappsflash/help.html?content=Filesystem_32.html

The above code should write a file, then read it back.

What's odd is I never get the "Hello" trace. Here's what's even more strange...

If I call the readLastLocation() function within iGotClicked (example:)

function iGotClicked (event:MouseEvent)
{
something = "Happy Day";
readLastLocation ();
}


I get the "World" trace.

So for some reason, writeFileStream.addEventListener(Event.COMPLETE, completedWriteLastLocation); is never getting fired, but readFileStream.addEventListener(Event.COMPLETE, completedReadLastLocation); is.

Any ideas on what I'm doing wrong?


NOTE:
There were a few times when I removed the COMPLETE eventListeners and forced the subfunctions, but I kept getting errors that the file was open and there was a sharing violation blah blah blah. Even though I .close()'d the filestream.

xwielder
01-16-2008, 04:52 PM
Got it.


// --------------------------------------------START-------------------------------------
// WRITE IT
var strr:String = "";
var writeFileStream:FileStream = new FileStream ();
function writeLastLocation ()
{
var writeFile:File = File.applicationStorageDirectory;
var writeFileString:String = (writeFile.nativePath + "\\JeopardyFileLastLocation.txt");
writeFile = writeFile.resolvePath(writeFileString);
writeFileStream.open (writeFile, FileMode.WRITE);
completedWriteLastLocation ();
}

function completedWriteLastLocation():void
{
var pattern1:RegExp = /(\\)/gi;
var thisString:String = fooBar.replace(pattern1, "/");
writeFileStream.writeUTFBytes (thisString);
writeFileStream.close ();
readLastLocation ();
}

// READ IT
var readFileStream:FileStream = new FileStream ();
function readLastLocation ()
{
var readFile:File = File.applicationStorageDirectory;
var readFileString:String = (readFile.nativePath + "\\JeopardyFileLastLocation.txt");
readFile = readFile.resolvePath(readFileString);
readFileStream.open(readFile, FileMode.READ);
completedReadLastLocation();
}
function completedReadLastLocation():void
{
readString = readFileStream.readMultiByte(readFileStream.bytesA vailable, "iso-8859-1");
readFileStream.close ();
writeFileStream.close();
}
// --------------------------------------------STOP--------------------------------------

xwielder
01-23-2008, 12:51 PM
Better yet:


// --------------------------------------------START-------------------------------------
// WRITE IT
function writeLastLocation ()
{
var writeFileStream:FileStream = new FileStream ();
var writeFile:File = File.applicationStorageDirectory;
var writeFileString:String = (writeFile.nativePath + "\\JeopardyFileLastLocation.txt");
writeFile = writeFile.resolvePath(writeFileString);
writeFileStream.open (writeFile, FileMode.WRITE);
var pattern1:RegExp = /(\\)/gi;
var thisString:String = fooBar.replace(pattern1, "/");
writeFileStream.writeUTFBytes (thisString);
writeFileStream.close ();
readLastLocation ();
}
// READ IT
function readLastLocation ()
{
var readFileStream:FileStream = new FileStream ();
var readFile:File = File.applicationStorageDirectory;
var readFileString:String = (readFile.nativePath + "\\JeopardyFileLastLocation.txt");
readFile = readFile.resolvePath(readFileString);
readFileStream.open(readFile, FileMode.READ);
readString = readFileStream.readMultiByte(readFileStream.bytesA vailable, "iso-8859-1");
readFileStream.close ();
}
// --------------------------------------------STOP--------------------------------------