Ultrashock Forums > Flash > AIR
AIR in Flash CS3 save text input to file

You are currently viewing our website as a guest which gives you limited access to forums, files and other resources.

Click here to join now for free, and start interacting with our members, download files and much more!

Click here if you are looking for our Flash files and other professional assets.
 
Post Reply | View first unread | Rate Thread Search this Thread | Thread Tools | Display Modes

#1
Bookmark and Share!
AIR in Flash CS3 save text input to file
Old 2008-06-06

Hi,

I'm working on a simple AIR (Building in Flash CS3) application and was wondering if some one on this forum could help me with a solution. I have like 4 text input fields that will collect user input and I'd like to save the data to a text file. Any help would be much appreciated.

Cheers

Mit
postbit arrow 14 comments | 8794 views postbit arrow Reply: with Quote   
Registered User
mit_51 is offline
seperator
Posts: 54
2003-03-05
Age: 27
seperator

Ultrashock Member Comments:
Nutrox's Avatar Nutrox Nutrox is offline Super Moderator Nutrox lives in United Kingdom 17 Creative Assets 2008-06-06 #2 Old  
Here is an example of how to write a simple text file...

ActionScript 3.0 Code:
  1. import flash.filesystem.File;
  2. import flash.filesystem.FileMode;
  3. import flash.filesystem.FileStream;
  4.  
  5. // Create a file.
  6. // For this example the file will be created on the desktop.
  7. var file:File = File.desktopDirectory.resolvePath( "my-new-file.txt" );
  8.  
  9. // Create a file stream to write stuff to the file.
  10. var stream:FileStream = new FileStream();
  11.  
  12. // Open the file stream.
  13. stream.open( file, FileMode.WRITE );
  14.  
  15. // Write stuff to the file.
  16. stream.writeUTFBytes( "Hello. This is some text in a file." );
  17.  
  18. // Close the file stream.
  19. stream.close();
  20.  
  21. // Clean up.
  22. file = null;
  23. stream = null;
Getting the text values from TextFields should be easy enough for you.
Reply With Quote  
mit_51 mit_51 is offline 2008-06-08 #3 Old  
Hey Nutrox, alright there. Thanks for the code. Will have a crack at it later today.

Cheers
Reply With Quote  
mit_51 mit_51 is offline 2008-06-09 #4 Old  
Hi Nutrox, Just tried the code. I'm sure it's right having looked at the file api but I'm getting a 1046 error (type was not found or was not a compile-time constant). Is this down to how the file stuff is imported at the top?

Cheers
Reply With Quote  
Nutrox's Avatar Nutrox Nutrox is offline Super Moderator Nutrox lives in United Kingdom 17 Creative Assets 2008-06-09 #5 Old  
I think that is probably the cause of it. Flash doesn't automatically import the flash.filesystem.* classes, so you need to do that manually.
Reply With Quote  
mit_51 mit_51 is offline 2008-06-09 #6 Old  
Thanks for the quick response. I've added the path to the classes manually from the publish settings but that's giving the same error. Is it added in a different way?

Thanks
Reply With Quote  
mit_51 mit_51 is offline 2008-06-09 #7 Old  
I've tried different import rules and gone to the publish setting and changed the path and made sure that it points to the right classes folder. Still no luck I keep getting the 1046 error. I'd appreciate any help on this.

Cheers
Reply With Quote  
Nutrox's Avatar Nutrox Nutrox is offline Super Moderator Nutrox lives in United Kingdom 17 Creative Assets 2008-06-09 #8 Old  
Have you managed to publish any AIR SWF files before? It sounds strange that Flash should be moaning about those classes if you are importing them. Are you sure the problem isn't somewhere else in your code?

Reply With Quote  
mit_51 mit_51 is offline 2008-06-09 #9 Old  
Yeah I've managed to publish air files before, based on tutorials and sample apps from adobe's website. I know it is a bit wierd no one seems to be having this problem. I wonder if it needs to use the package { and then inside,

Code:
package {
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	
	public class Main {
		public var file:File = File.desktopDirectory.resolvePath( "my-new-file.txt" );
		public var stream:FileStream = new FileStream();
		
		public function Main(){
			stream.open( file, FileMode.WRITE );
			stream.writeUTFBytes( "Hello. This is some text in a file." );
			stream.close();
			file = null;
			stream = null;
		}
	}
}
The 1046 error is gone, but now i'm getting a 5000 error saying the script must subclass. Completely lost
Reply With Quote  
Nutrox's Avatar Nutrox Nutrox is offline Super Moderator Nutrox lives in United Kingdom 17 Creative Assets 2008-06-09 #10 Old  
Ok, keep that class because that is the way you should be doing things. You have forgot to extend Sprite or MovieClip that is all, document/main classes need to extend one of those.

Code:
import flash.display.Sprite;

public class Main extends Sprite {
Reply With Quote  
mit_51 mit_51 is offline 2008-06-09 #11 Old  
Yeay thanks, I've got no errors now and it's all working. Much appreciated and good learning experience.

Cheers
Reply With Quote  
ecm08 ecm08 is offline ecm08 lives in United States 2008-07-29 #12 Old  
this overwrites whatever text is in my text file..how can i add to the file each time it is written to instead?
Reply With Quote  
Nutrox's Avatar Nutrox Nutrox is offline Super Moderator Nutrox lives in United Kingdom 17 Creative Assets 2008-07-29 #13 Old  
Change the file mode to "append"

Code:
stream.open( file, FileMode.APPEND );
Reply With Quote  
ecm08 ecm08 is offline ecm08 lives in United States 2008-07-29 #14 Old  
thanks for the quick reply.

now i just need to figure out how to get the values of my username and email forms to save to the file rather than static text.

i tried:

var Email=Email;
stream.writeUTFBytes(Email);
Reply With Quote  
ravipchandra ravipchandra is offline ravipchandra lives in India 2009-02-28 #15 Old  
Hi,

Could you please tell us how to import the flash.filesystem.* classes manually in flash cs3.


I am eagerly waiting for your reply
Chand
Reply With Quote  
Thread Tools
Display Modes Rate This Thread
Rate This Thread: