Search code examples
apache-flexflashactionscript-3fileair

How to save a string into file in applicationdirectory and get its real location? (Adobe Air)


How to save a string into file (file.superlongextention) in applicationdirectory and get its real locationon Hard drive (like C:/files/...)?


Solution

  • For security reason you can't write into the Application directory (for testing you can but it 's realy not recommended).

    You can write into the Application Storage directory to store your application data.

    • Use File then FileStream to write your data.
    • To get the path of the file use nativePath.

      import flash.filesystem.File;
      import flash.filesystem.FileStream;
      import flash.filesystem.FileMode;
      import mx.controls.Alert;
      
      // get a file reference to the storage directory
      var file:File=File.applicationStorageDirectory.resolvePath("myfile.withextension");
      
      // create a file stream
      var fs:FileStream=new FileStream();
      
      // open the stream for writting
      fs.open(file, FileMode.WRITE);
      
      // write the string data down to the file
      fs.writeUTFBytes("my string to save");
      
      // ok close the file stream
      fs.close();
      
      // show the path to the file we have saved using Alert
      Alert.show(file.nativePath);