views:

24

answers:

1

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

+1  A: 

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);
    
Patrick
Hm... I use Flash builder 4. I wrapped your code (not imports ofcourse) into public function. But when I try to run it it gives me SecurityError: fileWriteResource at flash.filesystem::FileStream/open()
Blender
So I changed applicationDirectory to userDirectory and it now works...)
Blender
But how to get rid of "0" in the begining of line?
Blender
Yes sorry as i said you can't write to applicationDirectory, and i left it in the code, my bad. It 's applicationStorageDirectory in my code. writeUTF write also the length of the string so use writeUTFBytes instead. I have modified the code.
Patrick