iOS经典讲解之获取沙盒文件路径写入和读取简单对象
2015-09-15 20:40
573 查看
#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
相关文章推荐
- iOS软件开发 设置cell动画效果
- iOS软件开发 设置启动页面时间
- ios 开发小记 (三)
- ios 开发小记 (二)
- IOS一些界面刷新思路
- iOS——越狱与免证书调试
- iOS 常用的几个第三方库
- iOS程序启动的完整过程
- iOS开发--多线程 并行开发
- ios7 侧滑返回
- ios数据初级持久化 存储复杂对象
- iOS项目开发实战——使用手势识别判断用户操作
- ios 定位 监听是否跨入某个指定的区域
- iOS绘图教程 各种绘制原理
- ios 数据持久化初级
- iOS 手机短信验证码等待时间
- 判断iOS设备型号
- 最近总结关于找iOS的工作的体会,大家可以补充
- IOS 图片上传处理 图片压缩 图片处理
- IOS动效设计