iOS经典讲解之获取沙盒文件路径写入和读取简单对象

来源:互联网 发布:ubuntu下安装vmware12 编辑:程序博客网 时间:2024/06/05 17:42
#import <UIKit/UIKit.h>@interface RootViewController : UIViewController@end

#import "RootViewController.h"@interface RootViewController ()@end实现文件:@implementation RootViewController- (void)viewDidLoad {    [super viewDidLoad];    [self path];    [self writeFile];    [self readingFile];    }// 打印沙盒中的文件夹路径- (void)path{    // 每运行一次 相当于重新安装一次 重新安装就重新分配一个沙盒 所以你每次运行路径都不一样    //NSDocumentDirectory要打印文件夹地址    // NSUserDomainMask 搜索范围            //documents 路径    // 该文件夹 一般存储用户的一些数据    NSArray *documentsPathArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);    NSString *document = [documentsPathArr lastObject];    NSLog(@"%@", document);      // Caches缓存文件夹路径 // 该文件夹 一般存储缓存文件   NSArray *cachesPathArr = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);        NSString *cachesPath = cachesPathArr[0];    NSLog(@"%@", cachesPath);        // 打印tmp文件夹    // 该文件夹 一般存储临时文件    NSString *tempPath = NSTemporaryDirectory();    NSLog(@"%@", tempPath);        // 打印沙盒的主目录路径    NSLog(@"%@", NSHomeDirectory());   }// 简单对象引入文件// 注意如果你写入字典或者数组 那么字典数组中存储的必须是简单对象 无法写入复杂对象// 复杂对象//自定义的类 比如Person类- (void)writeFile{    //简单对象    // 字符串 字典 数组 data...系统提供的类    // 写入文件的路径    // 在documents路径下写入xiaoshuo.txt    NSString *str = @"第一章 ";    NSArray *documentsPathArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);    NSString *documentsPath = [documentsPathArr lastObject];    // 拼接要写入文件的路径    NSString *path = [documentsPath stringByAppendingPathComponent:@"xiaoshuo.txt"]; // stringByAppendingString:@"/xiaoshuo.txt" 也可以    //atomically 如果YES在你写入的过程中 出现程序的崩溃不影响写入    [str writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];    NSLog(@"%@", path);        // 简单对象写入步骤    // 1、拼写要写入的路径 (路径一定要拼对)    // 2、调用写入方法 完事        // 写入一个数组 shuzu.txt    // 必须给后缀类型 你不给那 就默认是txt格式的,如果是txt格式里面是xml结构,如果是plist格式里面就是plist结构    NSString *arrPath = [documentsPath stringByAppendingPathComponent:@"shuzu.plist"];    NSArray *array = @[@"八戒", @"悟空", @"三藏", @"悟净"];    // 调用写入方法    [array writeToFile:arrPath atomically:YES];    NSLog(@"%@", arrPath);        // 写入一个字典zidian.plist    NSString *dicPath = [documentsPath stringByAppendingPathComponent:@"zidian.plist"];    NSDictionary *dic = @{@"2":@""};    [dic writeToFile:dicPath atomically:YES];    NSLog(@"%@", dicPath);        // data的写入 后缀.da        NSString *dataPath = [documentsPath stringByAppendingPathComponent:@"data.da"];    NSString *dataStr = @"你猜我是谁";    NSData *data = [dataStr dataUsingEncoding:NSUTF8StringEncoding];    // 写入文件    [data writeToFile:dataPath atomically:YES];    NSLog(@"%@", dataPath);  }// 读取写入的文件- (void)readingFile{    // 读字符串    // 获取路径    NSArray *documentsPathArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);    NSString *documentsPath = [documentsPathArr lastObject];    // 拼接要写入文件的路径     NSString *path = [documentsPath stringByAppendingPathComponent:@"xiaoshuo.txt"];    // 从路径中读取字符串    NSString *str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];    NSLog(@"%@", str);        // 读数组文件    // 获取路径    NSString *arrPath = [documentsPath stringByAppendingPathComponent:@"shuzu.plist"];    NSArray *array = [NSArray arrayWithContentsOfFile:arrPath];    NSLog(@"%@", array);        // 读取字典    // 获取路径    NSString *dicPath = [documentsPath stringByAppendingPathComponent:@"zidian.plist"];    NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:dicPath];    NSLog(@"%@", dic);        //读data NSString *dataPath = [documentsPath stringByAppendingPathComponent:@"data.da"];    // 读取data    NSData *data = [NSData dataWithContentsOfFile:dataPath];    // 将data转化成字符串    NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];    NSLog(@"%@", dataStr);}@end


0 0