goggle pink
goggle pink

Reputation: 132

How to create NEW txt file using Adobe AIR

How to create a REALY NEW text file (.txt) using AS3 adobe AIR. Most articles are writing to EXISTING text file (text file already exist).

like this one: How to create new File txt by using Adobe Air

Thank you.

Upvotes: 0

Views: 7233

Answers (1)

Marijn
Marijn

Reputation: 810

//create a reference to the file in the applicationStorage Directory 
//(for more directories, look at http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/filesystem/File.html
var fileToCreate:File = File.applicationStorageDirectory;
fileToCreate          = fileToCreate.resolvePath("myfile.txt");

//create a filestream to write to the file
//the write and/or creating the file is done with the FileMode in the open() function
//look at the table that describes what FileMode does what here: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/filesystem/FileMode.html
var fileStream:FileStream = new FileStream();
fileStream.open(fileToCreate, FileMode.WRITE);

//write some string to the file
fileStream.writeUTF('this is a string to write to the file');

//finally, close the filestream
fileStream.close();

EDIT: changed filemode from READ to WRITE. READ does not create the file ofcourse :)

Upvotes: 2

Related Questions