ios沙盒机制---基本数据类型的存取和文件的基本操作
沙盒快速存储及读取
存储: [[NSUserDefaults standardUserDefaults] setObject:@"abc" forKey:@"1"];
[[NSUserDefaults standardUserDefaults] synchronize];
读取: NSString *str = [[NSUserDefaults standardUserDefaults] objectForKey:@"1"];
//获取一些目录的位置
//1. 获取Document
NSString *document = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
NSLog(@"%@", document);
//2.获取cache
NSString *cache = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
NSLog(@"%@", cache);
//3.获取preference
NSString *preference = NSSearchPathForDirectoriesInDomains(NSPreferencePanesDirectory, NSUserDomainMask, YES).lastObject;
NSLog(@"%@",preference);
//4.获取tmp
NSString *tmp = NSTemporaryDirectory();
NSLog(@"%@", tmp);
//5.获取Bundle的路径
NSString *bundle = [NSBundle mainBundle].bundlePath;
NSLog(@"%@", bundle);
//把文件写入到沙盒中
//1. 字符串写入
//1.1 拼接好路径
NSString *stringFilePath = [document stringByAppendingPathComponent:@"string.text"];
NSLog(@"%@", stringFilePath);
//1.2 准备要写入的文字
NSString *str = @"我是要写入的文字";
//1.3 将文字写入到文价中
[str writeToFile:stringFilePath //写入文件的位置
atomically:YES //安全
encoding:NSUTF8StringEncoding //编码
error:nil]; //错误
//1.4 读取文件中的内容
NSString *newStr = [NSString stringWithContentsOfFile:stringFilePath encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@", newStr);
//2.数组
NSString *arrayFilePath = [cache stringByAppendingPathComponent:@"array.text"];
NSArray *arr = @[@"gou onr", @"大煞风景", @"dhfjsk"];
[arr writeToFile:arrayFilePath atomically:YES];
NSArray *newAarry = [NSArray arrayWithContentsOfFile:arrayFilePath];
NSLog(@"%@", newAarry);
//3. 字典
NSString *dictFilePath = [cache stringByAppendingPathComponent:@"dict.text"];
NSDictionary *dict = @{
@"name":@"gold one",
@"age" :@"18",
@"gender":@"女"
};
[dict writeToFile:dictFilePath atomically:YES];
NSDictionary *newDict = [NSDictionary dictionaryWithContentsOfFile:dictFilePath];
NSLog(@"%@", newDict);
//4.图片
//1.拼接地址
NSString *imageFilePath = [document stringByAppendingPathComponent:@"guoguo.png"];
//2.获取数据
UIImage *image = [UIImage imageNamed:@"guoguo"];
//3.写入
//3.1先将image转为NSData数据类型
NSData *imageData = UIImagePNGRepresentation(image); //将图片转为data
//3.2写入data
[imageData writeToFile:imageFilePath atomically:YES];
//4.读取
UIImage *newImage = [UIImage imageWithContentsOfFile:imageFilePath];
NSLog(@"%@", newImage);
// NSFileManager
//单例设计模式:单例类创建出来的对象,只会有一个,不管创建多少次,都是同一个对象
NSFileManager *fileManager = [NSFileManager defaultManager];
//1.创建文件夹,拼接新文件夹的路径
NSString *filePath = [tmp stringByAppendingString:@"test2/test3/test4"];
//2.创建
[fileManager createDirectoryAtPath:filePath //
withIntermediateDirectories:YES //决定是是否可以创建包含关系的文件夹
attributes:nil //
error:nil]; //
NSLog(@"%@", filePath);
//3.删除(这样只会删除最里层的一个文件夹)
[fileManager removeItemAtPath:filePath error:nil];
//4.判断文件是否存在
if ([fileManager fileExistsAtPath:stringFilePath]) {
NSLog(@"文件存在");
} else {
NSLog(@"no");
}
//5.删除全部文件
NSString *filePath2 = [tmp stringByAppendingString:@"test2"];
[fileManager removeItemAtPath:filePath2 error:nil];
//6..获取某个文件夹下的所有文件
NSArray *fileArray = [fileManager contentsOfDirectoryAtPath:document error:nil];
NSLog(@"fileArray = %@", fileArray);
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。