IOS读写沙盒文件数据 博客分类: IOS、沙盒、 objective-c
程序员文章站
2024-03-15 16:45:48
...
-
读写沙盒文件中Documents文件下的数据
//获取Documents目录路径 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //获取沙盒中Documents文件的路径 NSString *str_file_path = [paths objectAtIndex:0]; //将自己想创建的文件名添加到Documents录后,拼成一整个字符串 NSString *str_data_file_path = [str_file_path stringByAppendingFormat:@"/person_info.plist"]; //创建需要保存到沙盒的数组数据 NSDictionary *dic_data = [[NSDictionary alloc]initWithObjectsAndKeys:@"Mary",@"name",@"18",@"age", nil]; NSDictionary *dic_data1 = [[NSDictionary alloc]initWithObjectsAndKeys:@"Jack",@"name", nil]; NSArray *array = [[NSArray alloc]initWithObjects:dic_data,dic_data1, nil]; //将数组数据写入到拼接好的沙盒文件中 [array writeToFile:str_data_file_path atomically:YES]; //读取沙盒文件中的数据 NSArray *arr = [[NSArray alloc]initWithContentsOfFile:str_data_file_path]; NSLog(@"%@",arr);
- 读写沙盒目录下Library文件中的Caches文件的路径
NSArray *arr_paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSString *str_cachesDir = [arr_paths objectAtIndex:0]; //将自己想创建的文件名添加到Caches目录后,拼成一整个字符串 NSString *str_data_file_path = [str_cachesDir stringByAppendingFormat:@"/person_info.plist"]; //创建需要保存到沙盒的数组数据 NSDictionary *dic_data = [[NSDictionary alloc]initWithObjectsAndKeys:@"Mary",@"name",@"18",@"age", nil]; NSDictionary *dic_data1 = [[NSDictionary alloc]initWithObjectsAndKeys:@"Jack",@"name", nil]; NSArray *array = [[NSArray alloc]initWithObjects:dic_data,dic_data1, nil]; //将数组数据写入到拼接好的沙盒文件中 [array writeToFile:str_data_file_path atomically:YES]; //读取沙盒文件中的数据 NSArray *arr = [[NSArray alloc]initWithContentsOfFile:str_data_file_path]; NSLog(@"%@",arr);
- 获取沙盒文件的主目录
// 获取沙盒主目录路径 NSString *homeDir = NSHomeDirectory();
- 获取沙盒文件的tmp目录
NSString *tmpDir = NSTemporaryDirectory();