titanium中的文件操作 ( titanium file operations)

发布时间: 2015-02-14 03:09:00

refer to:!/guide/Filesystem_Access_and_Storage

titanium 可以对文件增删改查,  


Ti.FileSystem , Ti.FileSystem.File


Ti.Filesystem.applicationDataDirectory: A read/write directory accessible by your app. Place your application-specific files in this directory. The contents of this directory persist until you remove the files or until the user uninstalls the application.

对于app的数据,放在这里. 这里的文件会一直存在, 啥时候用户把你的app删掉了,这里的数据才会消失. 

Ti.Filesystem.resourcesDirectory: A read-only directory where your application resources are located; this directory corresponds to the project/Resources directory in Studio. The contents of this directory persist until the user uninstalls the application.

只读文件夹. app在, 它就在.  (小心了,这个文件夹在模拟器中是可写的)

Ti.Filesystem.tempDirectory: A read-write directory where your application can place temporary files. The contents of this directory persist until your application fully closes, at which time the operating system could delete your files.

临时文件夹. app 一停止, 里面的内容就会被删掉. 

Ti.Filesystem.externalStorageDirectory: A read-write directory on the external storage device (SD card) accessible by your app, if such a location exists. Check first with Ti.Filesystem.isExternalStoragePresent() (which returns a Boolean). Available only for the Android platform.

外部的SD卡 文件夹. 可写.  这个class 只对 android生效. 

Ti.Filesystem.applicationCacheDirectory: A read-write directory where your application can cache data. The contents of this directory persist after your application fully closes but at the discretion of the operating system. For the Android platform, the cache is limited to 25 MB and the files remain for the lifetime of the application. For the iOS platform, there is no size limit but the data only remains there until iOS cleans the directory if it requires the disk space.

缓存文件夹. 可写. app 一停止就没了. 对于安卓平台不超过25MB. 对于ios不好说, 由系统控制.

获取 file handle:  


var f = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory,'yourfile.txt');


var f = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory,'yourfile.txt');
var contents =;'Output as a blob: '+contents); // useful if contents are binary'Output text of the file: '+contents.text);'Output the file\'s MIME type: '+contents.mimeType); // e.g. text/plain


var f = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory,'emptyfile.txt');
f.write('The file is no longer empty!'); // write to the file


var f = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory,'nonexistent_file.txt');
if(f.exists()===false) {
 // you don't need to do this, but you could...
f.write('writing to the file would be enough to create it');


var f = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory,'delete_me.txt');
if(f.exists() && f.writeable) {
	var success = f.deleteFile(); ? 'success' : 'fail'); // outputs 'success'