网游之凌十一少,连云港车祸,瞿塘峡口冷烟低
文件目录的访问
最简单的:(由于是沙盒关系,没有文件夹概念的)
还有一种就是需要指定文件路径的:
需要用到nsbundle.
如果需要指定路径,就要这样写:
你也可自己拼接:
一般来说应用程序有3个目录
documents,library,tmp
现在apple不允许把大数据保存在documents文档,如果你想把视频什么的保存在documents文档里面,需要备份到icould比较麻烦。
一般窝的做法就是保存在library的caches目录下面(不知道是否合理:))
tmp目录的话,保存一些临时文件,在退出程序的时候你可以把里面的缓存内容删除。
在应用程序中获得自己的documents目录:
文件目录管理
下面来看一些常用的文件目录管理方法
1、常见的nsfilemanager文件方法
-(bool)createfileatpath: path contents:(nsdata *)data attributes:attr //向一个文件写入数据
-(bool)removeitematpath:path error:err //删除一个文件
-(bool)moveitematpath:from topath:to error:err //重命名或者移动一个文件(to不能是已存在的)
-(bool)copyitematpath:from topath:to error:err //复制文件(to不能是已存在的)
-(bool)contentsequalatpath:path andpath:path2 //比较两个文件的内容
-(bool)fileexistatpath:path //测试文件是否存在
-(bool)isreadablefileatpath:path //测试文件是否存在,并且是否能执行读操作
-(bool)iswriteablefileatpath:path //测试文件是否存在,并且是否能执行写操作
-(nsdictionary *)attributesofitematpath:path error:err //获取文件的属性
-(bool)setattributesofitematpath:attr error:err //更改文件的属性
-(bool)changecurrentdirectorypath:path //更改当前目录
-(bool)copyitematpath:from topath:to error:err //复制目录结构(to不能是已存在的)
-(bool)createdirectoryatpath:path withintermediatedirectories:(bool)flag attribute:attr //创建一个新目录
-(bool)fileexistatpath:path isdirectory:(bool*)flag //测试文件是不是目录(flag中储存结果yes/no)
-(nsarray *)contentsofdirectoryatpath:path error:err //列出目录内容
-(nsdirectoryenumerator *)enumeratoratpath:path //枚举目录的内容
-(bool)removeitematpath:path error:err //删除空目录
-(bool)moveitematpath:from topath:to error:err //重命名或移动一个目录(to不能是已存在的)
-(nsarray *)pathcomponents //析构路径,获得组成此路径的各个部分
-(nsstring *)lastpathcomponent //提取路径的最后一个组成部分
-(nsstring *)pathextension //从路径的最后一个组成部分中提取其扩展名
-(nsstring *)stringbyappendingpathcomponent:path //将path添加到现有路径的末尾
-(nsstring *)stringbyappendingpathextension:ext //将指定的扩展名添加到路径的最后一个组成部分
-(nsstring *)stringbydeletinglastpathcomponent //删除路径的最后一个组成部分
-(nsstring *)stringbydeletingpathextension //从文件的最后一部分删除扩展名
-(nsstring *)stringbyexpandingtileinpath //将路径中代字符扩展成用户主目录(~)或指定用户的主目录(~user)
-(nsstring *)stringbyresolvingsymlinksinpath //尝试解析路径中的符号链接
-(nsstring *)stringbystandardizingpath //通过尝试解析~、..(父目录符号)、.(当前目录符号)和符号链接来标准化路径
nsstring* nsfullusername(void) //返回当前用户的完整用户名
nsstring* nshomedirectory(void) //返回当前用户主目录的路径
nsstring* nshomedirectoryforuser(nsstring* user) //返回用户user的主目录
nsstring* nstemporarydirectory(void) //返回可用于创建临时文件的路径目录
library/caches(nscachesdirectory) //用于写入应用支持文件的目录,保存应用程序再次启动需要的信息。itunes不会对这个目录的内容进行备份
tmp(use nstemporarydirectory()) //这个目录用于存放临时文件,只程序终止时需要移除这些文件,当应用程序不再需要这些临时文件时,应该将其从这个目录中删除
library/preferences //这个目录包含应用程序的偏好设置文件,使用 nsuserdefault类进行偏好设置文件的创建、读取和修改
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
网友评论