这是我个人的学习笔记 , 如有不同见解欢迎评论交流 .
( 我的微博 : http://weibo.com/JohnnyB0Y )
-
沙盒机制
1.每个应用都有自己的沙盒,不能越界读写其他应用的数据。
2.获取Documents文件夹路径
NSArray *pathsList = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *documentPath = [pathsList lastObject];
3.获取Library文件夹路径
NSArray *pathsList = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,
NSUserDomainMask,
YES);
NSString *libraryPath = [pathsList lastObject];
4.获取tmp文件夹路径
NSString *tmpPath = NSTemporaryDirectory();
沙盒文件夹 | 使用场景 |
---|---|
Documents | iTunes会同步,存放重要的数据,希望同步到其他设备的数据。 |
Library | iTunes不会同步,设置程序的默认设置和其他状态信息。 |
Library/Caches | 缓存文件夹,保存可以从网络上重复获取的资源(节省流量)。 |
Library/Preferences | 保存一些用户信息配置文件,LaunchScreen生成的启动图片。 |
tmp | 创建临时文件的目录,当iOS设备重启时,文件会被自动清除。 |
-
NSFileManager
1.创建一个文件并写入数据
- (BOOL)createFileAtPath:(NSString *)path contents:(NSData *)data attributes:(NSDictionary *)attr;
2.从一个文件中读取数据
- (NSData *)contentsAtPath:(NSString *)path;
3.scrPath路径上的文件移动到dstPath路径上,注意这里的路径是文件路径而不是目录
- (BOOL)moveItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **) error;
4.scrPath路径上的文件复制到dstPath路径上
- (BOOL)copyItemAtPath:(NSString *)scrPath toPath:(NSString *)dstPath error:(NSError **) error;
5.比较两个文件的内容是否一样
- (BOOL)contentsEqualAtPath:(NSString *)path1 andPath:(NSString *)path2;
6.文件是否存在
- (BOOL)fileExistsAtPath:(NSString *)path;
7.移除文件
- (BOOL)removeItemAtPath:(NSString *)path error:(NSError **) error;
8.创建文件管理
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *path = [NSHomeDirectory( ) stringByAppendingPathComponent:@"holyBible.txt"];
NSString *text = @"abcdefg";
9.将字符串转成NSData类型
NSData *data = [text dataUsingEncoding: NSUTF8StringEncoding];
10.写入文件
BOOL success = [fileManager createFileAtPath:path contents:data attributes:nil];
11.创建文件夹
NSString *filePath = [path stringByAppendingPathComponent:@"holyBible.txt"];
NSString *contect = @"abcdefg”;
BOOL success = [fm createFileAtPath:filePath contents:[content dataUsingEncoding: NSUTF8StringEncoding] attributes:nil];
12.NSFileManager-读取内容
NSData *fileData = [fileManager contentsAtPath:filePath];
NSString *content = [[NSString alloc] initWithData:fileData dataUsingEncoding: NSUTF8StringEncoding];
13.NSData-读取内容
NSString *filePath = [path stringByAppendingPathComponent:@"holyBible.txt"];
NSData *data = [NSData dataWithContentOfFile:filePath];
13.1 NSString-读取内容
NSString *filePath = [path stringByAppendingPathComponent:@"holyBible.txt"];
NSString *content = [[NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
移动、复制文件
14.移动文件(重命名)
NSString *toPath = [NSHomeDirectory( ) stringByAppendingPathComponent:@"hellogod/New Testament.txt"];
[fm createDirectoryAtPath:[toPath stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:nil];
NSError *error;
BOOL isSuccess = [fm moveItemAtPath:filePath toPath:toPath error:&error];
15.复制文件(重命名)
NSString *copyPath = [NSHomeDirectory( ) stringByAppendingPathComponent:@"备份/Old Testament.txt"];
[fm createDirectoryAtPath:[toPath stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:nil];
BOOL success = [fm copyItemAtPath:toPath toPath:toPath error:nil];
删除文件、获取文件大小
16.判断文件是否存在和删除文件
if([fm fileExistsAtPath])
{
if ([fm removeItemAtPath:copyPath])
{
NSLog(@"remove success");
}
}
17.获得文件的属性字典
NSFileManager *fileManager = [NSFileManager defaultManager];
NSDictionary *attrDic = [fileManager attributesOfItemAtpath:sourcePath error:nil];
18.获取文件大小
NSNumber *fileSize = [attrDic objectForKey:NSFileSize];
19.获取目录文件信息
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *enuPath = [NSHomeDirectoty( ) stringByAppendingPathComponent:@"Test"];
NSDictionaryEnumerator *dirEnum = [fileManager enumeratorAtPath:enuPath];
NSString *path = nil;
while ((path = [dirEnum nextObject]} != nil){
NSLog(@"%@",path);
}
-
Plist、Archive数据归档
#######Plist就是数组、字典(xml的组织形式)直接写到沙盒中。
字典:[dictionary writeToFile:写入文件的路径 atomically:是否原子写入];
数组:[array writeToFile:写入文件的路径 atomically:是否原子写入];
#######Archive归档解档。
1.要归档的类需要实现<NSCoding>协议
1.1 解档属性:重写
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super init];
if (self) {
self.requestData = [aDecoder decodeObjectForKey:@"requestData"];
}
return self;
}
1.2 归档属性:重写
- (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:_requestData forKey:@"requestData"];
}
2.对类完整归档解档
NSString *objKey = @"objKey";
NSString *savePath = [NSString stringWithFormat:@"%@/save.data", NSTemporaryDirectory()];
// 1 归档
NSMutableData *dataM = [NSMutableData data];
NSKeyedArchiver *keyedArchiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:dataM];
[keyedArchiver encodeObject:@"对象" forKey:objKey];
[keyedArchiver finishEncoding];
// 归档后把dataM保存到沙盒
[dataM writeToFile:savePath atomically:YES];
// 2 解档
// 从沙盒读取data
NSData *data = [[NSData alloc] initWithContentsOfFile:savePath];
// 开始解档
NSKeyedUnarchiver *keyedUnarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
NSString *string = [keyedUnarchiver decodeObjectForKey:objKey];
[keyedUnarchiver finishDecoding];
-
NSUserDefaults
1.属于简化版的Archive归解档
NSString *objKey = @"objKey";
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
// 保存数据(其中有很多类型的数据)
[userDefaults setObject:@"对象" forKey:objKey];
// 读取数据
[userDefaults objectForKey:objKey];
-
SQLite3应用