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

#import

@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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值