android文件与java IO

在操作系统级别,android运行在Linux内核之上,所以文件系统类似linux系统。

除了标准的java io库之外,另外提供了更便捷的访问应用文件和目录的方法:

标准的java io库详见 https://developer.android.com/reference/java/io/package-summary.html

标准的android目录的组织结构 详见我的另外一篇blog: http://zjianjia.blog.163.com/blog/static/174089475201482312897/


这里讲下Context提供的方法,由于context是所有关键组件(application、Activity、service)的父类,所以在子类中可以更简单的使用context的方法访问文件和目录




File getFileDir()
      获取/data/data/<packageName>/files目录
 
FileInputStream openFileInput(String name)
         打开现有文件进行读取
 
FileOutputStream openFileOutput(String name,int mode)
打开文件进行写入,没有则创建它
 
File getDir(String name,int mode)
获取/data/data/<packageName>/目录的绝对路径
 
String[] fileList()
获取/data/data/<packageName>/files目录下的文件列表
 
File getCacheDir()   
获取/data/data/<packageName>/cache目录,该目录系统会自动清理,当存储满时,系统会首先清理该目录
 
 File Context.getExternalCacheDir() /  File[] Context.getExternalCacheDirs()

这些获取为SD卡上的由操作系统自动管理的文件夹,同样会再应用卸载时被清理掉

还有一个游戏文件常用的

File getObbDir() 和File[] getObbDirs()

Return the primary external storage directory where this application's OBB files (if there are any) can be found. Note if the application does not have any OBB files, this directory may not exist.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章