首页 > 其他 > 详细

iOS开发判断文件是否存在

时间:2014-01-21 19:04:43      阅读:488      评论:2      收藏:0      [点我收藏+]

iOS的沙盒存在两个目录,一个是【沙盒的.app】,一个是【沙盒的document】,一般情况数据文件、资源文件都默认放在【沙盒的.app】中,但是这里的文件是不允许编辑,所以需要复制到【沙盒的document】中进行操作。获取文件存放路径的方法如下:

- (NSString*)plistPath{

    NSFileManager *fileManager = [NSFileManagerdefaultManager];

    //访问【沙盒的document】目录下的问题件,该目录下支持手动增加、修改、删除文件及目录

    NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/user_info.plist"];

    if(![fileManager fileExistsAtPath:filePath]) //如果不存在 {

         //访问【沙盒的.app】目录下的文件,这个目录是程序安装目录,程序运行时不允许手动修改该目录下的文件。

        NSString *dataPath = [[[NSBundlemainBundle] resourcePath] stringByAppendingPathComponent:@"user_info.plist"];//获取程序包中相应文件的路径

        NSError *error;

        //拷贝文件到沙盒的document下

        if([fileManager copyItemAtPath:dataPath toPath:filePath error:&error]) {

            NSLog(@"copy success");

        } else{

            NSLog(@"%@",error);

        }  

    }

    return filePath;

}

iOS开发判断文件是否存在

原文:http://www.cnblogs.com/ranger-cc/p/3527890.html

(3)
(3)
   
举报
评论 一句话评论(1
2016-11-11 16:30:35
iOS 如何判断文件是否存在?

http://www.dashen100.com/question/238
回复
 (8)
 (3)
1条  
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!