文件路径两种获取方式

来源:互联网 发布:暴力破解rar密码软件 编辑:程序博客网 时间:2024/05/18 08:24
第一种类型:文件在工程中

    NSString *path=[[NSBundle mainBundle]pathForResource:@"chat" ofType:@"plist"];
    chatArray=[[NSArray alloc]initWithContentsOfFile:path];

    1
    2

第二种类型:文件在模拟器后台

//方法一
 NSString *documents = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *path = [documents stringByAppendingPathComponent:@"person.arch"];


//方法二
-(NSString *)getDBpath{
    //获取documents目录
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory=[paths objectAtIndex:0];
    NSString *dbPath = [documentsDirectory stringByAppendingPathComponent:@"data.sqlite"];
    NSLog(@"db path:%@",dbPath);
    return dbPath;
}



//方法三,也是最常用的方法
-(NSString *)getPlishpathbyname:(NSString*)name
{
    //    首先获取根目录的路径
    NSString *documentspath=[NSHomeDirectory()stringByAppendingPathComponent:@"Documents"];
    //    创建一个name.plist(USER.plist)的文件
    //    将传入字符为名字的文件路径找出来,拼接在根目录后面,传入的文件名字用方法实现[NSStrin stringWithFormat:@"%@.plist",name]
    NSString *filepath=[documentspath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",name]];
    NSLog(@"%@",filepath);
    return filepath;
}

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
0 0