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
- iOS经典讲解之获取沙盒文件路径写入和读取简单对象
- iOS经典讲解之获取沙盒文件路径写入和读取简单对象
- 获取DocumentsPath路径 简单文件写入
- IOS之数据持久化 (简单对象写入文件)
- iOS 获取沙盒文件路径及 写入/删除 沙盒文件
- iOS编程------初级数据持久化/沙盒机制/NSFileManager/简单对象写入文件/复杂对象写入文件
- ios NSUserDefaults读取和写入自定义对象
- iOS进阶——沙盒及沙盒路径、简单文件的写入
- C#文件写入和读取简单使用
- iOS 文件的读取和写入
- iOS 文件的读取和写入
- iOS plist文件写入和读取
- HDFS 读取、写入、遍历目录获取文件全路径
- iOS沙盒机制 、对象写入及读取
- iOS文件写入读取
- iOS 通过路径对文件写入数据 读取数据
- iOS经典讲解之清除缓存文件
- iOS 字典和数组写入plist文件存到应用沙盒当中(含读取)
- add binary
- 反汇编android apk文件
- C语言堆栈入门——堆和栈的区别
- Java集合框架特点简单总结
- 今日问题汇总-WPF中更改Image控件的source、WPF 去掉窗体边框
- iOS经典讲解之获取沙盒文件路径写入和读取简单对象
- Netty5入门学习笔记001
- 字符集之间转换实现函数
- 仿射变换公式推导
- cocos2d-x 3.2 物理小游戏教程4 block it 刚体的隐藏和显示 源码资源下载
- 独立组件之间重叠放置时,OnTouch事件的响应顺序
- BZOJ 1128 [POI2008]Lam 高精度
- iOS实现一个颜色渐变的弧形进度条
- java笔记(多线程+包)-第21天