iOS经典讲解之获取沙盒文件路径写入和读取简单对象
来源:互联网 发布:java语言程序代码培训 编辑:程序博客网 时间:2024/06/08 12:33
- #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
- iOS经典讲解之获取沙盒文件路径写入和读取简单对象
- iOS经典讲解之获取沙盒文件路径写入和读取简单对象
- 获取DocumentsPath路径 简单文件写入
- IOS之数据持久化 (简单对象写入文件)
- iOS 获取沙盒文件路径及 写入/删除 沙盒文件
- iOS编程------初级数据持久化/沙盒机制/NSFileManager/简单对象写入文件/复杂对象写入文件
- ios NSUserDefaults读取和写入自定义对象
- iOS进阶——沙盒及沙盒路径、简单文件的写入
- C#文件写入和读取简单使用
- iOS 文件的读取和写入
- iOS 文件的读取和写入
- iOS plist文件写入和读取
- HDFS 读取、写入、遍历目录获取文件全路径
- iOS沙盒机制 、对象写入及读取
- iOS文件写入读取
- iOS 通过路径对文件写入数据 读取数据
- iOS经典讲解之清除缓存文件
- iOS 字典和数组写入plist文件存到应用沙盒当中(含读取)
- Dubbo架构设计详解
- iOS 自定义UITableViewCell上添加UIButton按钮如何确定点击了哪一行,代码块实现
- 最大公约数和快速gcd
- 关于任正非和华为,这10条必看!最后一条看哭无数人。。。
- Codevs4880 位数问题 数位dp?
- iOS经典讲解之获取沙盒文件路径写入和读取简单对象
- android设置时间回拨后让闹钟重新启动
- JavaScript 数据类型
- Codeforces Round #378 (Div. 2)题解报告
- Apache Thrift-可伸缩的跨语言服务开发框架
- IOS开发-使用Storyboard进行界面跳转及传值
- LoadRunner与encode64加密解密
- 欢迎使用CSDN-markdown编辑器
- Java基本类型注意点