PDA

View Full Version : re-writing to an existing text file from AIR


eliddell
01-11-2008, 02:26 PM
i am trying to write text to an existing text file in the same folder as my air app..


import flash.filesystem.File;
import flash.filesystem.FileStream;
import flash.filesystem.FileMode;

var docDir:File = File.documentsDirectory;
var dskTopFileStream:FileStream = new FileStream();
var fileString:String = docDir.nativePath;
var dskTopFile:File = File.documentsDirectory;

dskTopFile = dskTopFile.resolvePath(fileString+"test.txt");
dskTopFileStream.openAsync (dskTopFile, FileMode.UPDATE);
dskTopFileStream.writeUTFBytes ("Hello World!");
dskTopFileStream.close ();


the code doesnt do anything to the file

xwielder
01-11-2008, 02:33 PM
dskTopFile = dskTopFile.resolvePath(fileString+"test.txt");
trace(fileString);
trace(fileString + "test.txt");
dskTopFileStream.openAsync (dskTopFile, FileMode.UPDATE);


Run those traces and let me know what you come up with. I think you might need to do this:

dskTopFile = dskTopFile.resolvePath(fileString+"\test.txt");

But I'm not sure.

eliddell
01-11-2008, 02:47 PM
traces don't seem to work at all

xwielder
01-11-2008, 02:49 PM
Yeah... I'm working on it. Gimme a minute. :)

xwielder
01-11-2008, 02:51 PM
Well,.. actually, "trace" will work, but you have to go into debug mode to see your traces when working with AIR files. Basically, instead of hitting CTRL+Enter to test your movie, just use CRTL+SHIFT+Enter. You'll see your traces then.

eliddell
01-11-2008, 02:56 PM
weird.. i couldn't get the trace to work so i created a dynamic text field that was my fileString variable... found the file!!!! but thats not where i wanted it.. it is going to the My Documents folder.. while my actual air file is in in the programs/writeTest folder.. i wanted the file to be in the same folder as the AIR file.. ho do i do that?


Erik

xwielder
01-11-2008, 02:57 PM
I got this to work:


import flash.filesystem.File;
import flash.filesystem.FileStream;
import flash.filesystem.FileMode;

var docDir:File = File.documentsDirectory;
var dskTopFileStream:FileStream = new FileStream();
var fileString:String = (docDir.nativePath + "\\test.txt");
var dskTopFile:File = File.documentsDirectory;
dskTopFile = dskTopFile.resolvePath(fileString);

dskTopFileStream.openAsync (dskTopFile, FileMode.WRITE);
dskTopFileStream.writeUTFBytes ("Hello World!");
dskTopFileStream.close ();

trace(fileString);

xwielder
01-11-2008, 03:02 PM
...i wanted the file to be in the same folder as the AIR file.. ho do i do that?


Change

var docDir:File = File.documentsDirectory;


to:

var docDir:File = File.applicationResourceDirectory;

xwielder
01-11-2008, 03:07 PM
You may find this link quite useful for making AIR apps. I personally reference it on a daily basis.

http://www.actionscriptcheatsheet.com/pdf/AIRcheatsheet.pdf

eliddell
01-11-2008, 03:08 PM
that gives me an error...

1119: Access of possibly undefined property applicationResourceDirectory through a reference with static type Class.

xwielder
01-11-2008, 03:14 PM
that gives me an error...

1119: Access of possibly undefined property applicationResourceDirectory through a reference with static type Class.

Sorry, things have changed since AIR Beta 2.

Use: applicationStorageDirectory


( http://www.adobe.com/cfusion/webforums/forum/messageview.cfm?catid=641&threadid=1305641#4731583 )

eliddell
01-11-2008, 03:33 PM
no error now.. but its going to : C:\Documents and Settings\eliddell\Application Data\com.adobe.example.writeTest.6868061C66A981952 4B813F28A02E42F57A07C82.1\Local Store

when the application is actually installed in c:\programfiles\writeTest\

so confused..

hangalot
01-11-2008, 03:43 PM
you gave to learn how to use the debugger.
traces don't work because you are compiling and not debugging and in general your questions indicate that you are not analyzing your app at runtime.

being able to use the debugger will impove your productivity a heck of a lot

hangalot
01-11-2008, 03:46 PM
also those application paths in air are for security reasons (especially on a mac) where writing to the file system will only be allowed in the directory where the app is installed in this case for you:
C:\Documents and Settings\eliddell\Application Data\com.adobe.example.writeTest.6868061C66A981952 4B813F28A02E42F57A07C82.1

it looks like your app name is com.adobe.example.writeTest.6868061C66A981952B813F 28A02E42F57A07C82.1
which is a bit weird as well to say the least but i am prob missing something

eliddell
01-11-2008, 03:57 PM
i agree with what you are saying about using the debugger.. but i am using the debugger.. but don't know how to trace the variables in it..
it only spits out errors.. please advise

erik