IOS开发中判断文件是否存在,不存在则拷贝
2014-10-07 14:30
369 查看
首先,先总结一下如何获取Documents目录,在ios开发中,我们经常需要检索Documents目录的完整路径以便读取和写入文件,我总结了以下两种方法: 1、NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; 2、NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; 以上documentsDirectory就是获取的Documents的完整路径。 程序的所有资源文件,存储在程序包中,获取程序包路径的方法是: NSString *appDirectory = [[NSBundle mainBundle] bundlePath]; 在实际开发中我们有时需要判断Documents下的某个资源文件是否存在,如果不存在,则从程序包中拷贝进去,现在假设要判定的文件是"xxx.txt",判定代码如下: NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *filePath = [self [documentsDirectory stringByAppendingPathComponent:@"xxx.txt"]]; if(![fileManager fileExistsAtPath:filePath]) //如果不存在 { NSLog(@"xxx.txt is not exist"); NSString *dataPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:@"/xxx.txt"];//获取程序包中相应文件的路径 NSError *error; if([fileManager copyItemAtPath:dataPath toPath:filePath error:&error]) //拷贝 { NSLog(@"copy xxx.txt success"); } else { NSLog(@"%@",error); } }
相关文章推荐
- IOS开发中判断文件是否存在,不存在则拷贝
- IOS开发中判断文件是否存在,不存在则拷贝
- IOS开发中判断文件是否存在,不存在则拷贝
- IOS开发中判断文件是否存在,不存在则拷贝[转]
- iOS开发判断文件是否存在
- C#判断文件和文件夹是否存在 不存在则创建
- 通过文件锁实现,程序开始运行时,先判断文件是否存在,若存在则表明该程序已经在运行了,如果不存在就用open函数创建该文件,程序退出时关闭文件并删除文件
- win8 metro开发中,判断文件是否存在的方法
- 【转】通过文件锁实现,程序开始运行时,先判断文件是否存在,若存在则表明该程序已经在运行了,如果不存在就用open函数创建该文件,程序退出时关闭文件并删除文件
- iOS6.1 & iOS 7 & iOS8 判断沙盒文件或者目录是否存在,以及判断是文件还是目录的一个隐藏问题
- 【C#】C#判断文件路径是否存在,不存在则创建文件夹
- 判断XML文件是否存在,不存在就创建一个XMl文件,存在就在里面添加一条数据
- 判断本地系统目录下是否存在XML文件,如果不存在就创建一个XMl文件,若存在就在里面执行添加数据
- C# winfrom判断文件是否存在,不存在则创建,否则读取值显示到窗体
- 使用File类判断文件是否存在以及创建,拷贝和移动文件
- 判断本地系统目录下是否存在XML文件,如果不存在就创建一个XMl文件,若存在就在里面执行添加数据
- winform中判断文件夹以及文件是否存在,文件夹不存在进行创建,文件存在进行删除
- 判断本地是否存在Jquery文件,如果不存在则使用CDN加速的Jquery文件
- Linux下父进程通过管道发送文件名给子进程,子进程获取文件名后首先判断文件是否存在,不存在,通过管道返回错误信息,存在,将文件内容返回给父进程
- 根据文件获取文件夹,判断文件夹是否存在,不存在则创建