【问题标题】:How to save a string into file in applicationdirectory and get its real location? (Adobe Air)如何将字符串保存到应用程序目录中的文件中并获取其真实位置? (土坯空气)
【发布时间】:2011-02-22 13:19:19
【问题描述】:

如何将字符串保存到applicationdirectory 中的文件(file.superlongextention) 并获取其在硬盘驱动器上的真实位置(如C:/files/...)?

【问题讨论】:

    标签: apache-flex flash actionscript-3 file air


    【解决方案1】:

    但您可以将文件保存在应用程序目录中

    var appPath:File = File.applicationDirectory.resolvePath("");
    var fromUserPath:File = File.userDirectory.resolvePath(appPath.nativePath);
    

    感谢 Tanel Teemusk

    http://blog.teemusk.com/2011/10/writing-to-applicationdirectory-with-adobe-air/

    【讨论】:

      【解决方案2】:

      出于安全原因,您不能写入Application directory(可以进行测试,但不建议这样做)。

      您可以写入应用程序存储目录来存储您的应用程序数据。

      • 使用File 然后FileStream 写入数据。
      • 要获取文件的路径,请使用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);
        

      【讨论】:

      • 嗯...我使用 Flash builder 4。我将您的代码(当然不是导入)包装到公共函数中。但是当我尝试运行它时,它给了我 SecurityError: fileWriteResource at flash.filesystem::FileStream/open()
      • 所以我将 applicationDirectory 更改为 userDirectory,它现在可以工作了......)
      • 但是如何去掉行首的“0”呢?
      • 是的,对不起,我说过你不能写到 applicationDirectory,我把它留在了代码中,我的错。它是我的代码中的 applicationStorageDirectory。 writeUTF 还写入字符串的长度,因此请改用 writeUTFBytes。我已经修改了代码。
      猜你喜欢
      • 2011-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-07
      • 1970-01-01
      相关资源
      最近更新 更多