Flex手机开发-读写文件

调用FlexApi可以获取data/data目录 或者 /mnt/sdcard/目录,因此读写文件可以在这两个地方进行,对sdcard写文件时需要打开android对应的读写权限

  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

flex提供的几个系统目录

File.applicationDirectory.nativePath
File.applicationStorgeDirectory.nativePath
File.desktopDirectory.nativePath
File.documentsDirectory.nativePath
File.userDirectory.nativePath

往sdcard中写文件

public function writeFile():void{
//获取应用程序文档的目录  windows上  c:\documents and settings\username\documents  mac \user\username\documents
var targetFile:File = File.documentsDirectory.resolvePat("test.txt");
var fileStream:FileStream = new FileStream();
fileStream.open(targetFile,FileMode.WRITE);
fileStream.writeUTFBytes("hello mobile");
fileStream.close();
}

从sdcard中读文件

//读取文件
public function readFile():void{
   var file:File = File.documentsDirectory.resolvePath("test.txt");
   var file:File = new File(url);
   if(file.exists){
    var fs:FileStream = new FileStream();
    fs.open(file,FileMode.READ);
    var content:String = fs.readMultiByte(fs.bytesAvailable,"utf-8");
    fs.close();					
   }			
}

 

 

你可能感兴趣的:(Flex手机开发-读写文件)