這是我個(gè)人的學(xué)習(xí)筆記 , 如有不同見解歡迎評論交流 .
( 我的微博 : http://weibo.com/JohnnyB0Y )
-
沙盒機(jī)制
1.每個(gè)應(yīng)用都有自己的沙盒,不能越界讀寫其他應(yīng)用的數(shù)據(jù)忙干。
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會同步睡雇,存放重要的數(shù)據(jù),希望同步到其他設(shè)備的數(shù)據(jù)。 |
Library | iTunes不會同步,設(shè)置程序的默認(rèn)設(shè)置和其他狀態(tài)信息。 |
Library/Caches | 緩存文件夾英上,保存可以從網(wǎng)絡(luò)上重復(fù)獲取的資源(節(jié)省流量)。 |
Library/Preferences | 保存一些用戶信息配置文件啤覆,LaunchScreen生成的啟動圖片苍日。 |
tmp | 創(chuàng)建臨時(shí)文件的目錄,當(dāng)iOS設(shè)備重啟時(shí)窗声,文件會被自動清除相恃。 |
-
NSFileManager
1.創(chuàng)建一個(gè)文件并寫入數(shù)據(jù)
- (BOOL)createFileAtPath:(NSString *)path contents:(NSData *)data attributes:(NSDictionary *)attr;
2.從一個(gè)文件中讀取數(shù)據(jù)
- (NSData *)contentsAtPath:(NSString *)path;
3.scrPath路徑上的文件移動到dstPath路徑上,注意這里的路徑是文件路徑而不是目錄
- (BOOL)moveItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **) error;
4.scrPath路徑上的文件復(fù)制到dstPath路徑上
- (BOOL)copyItemAtPath:(NSString *)scrPath toPath:(NSString *)dstPath error:(NSError **) error;
5.比較兩個(gè)文件的內(nèi)容是否一樣
- (BOOL)contentsEqualAtPath:(NSString *)path1 andPath:(NSString *)path2;
6.文件是否存在
- (BOOL)fileExistsAtPath:(NSString *)path;
7.移除文件
- (BOOL)removeItemAtPath:(NSString *)path error:(NSError **) error;
8.創(chuàng)建文件管理
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *path = [NSHomeDirectory( ) stringByAppendingPathComponent:@"holyBible.txt"];
NSString *text = @"abcdefg";
9.將字符串轉(zhuǎn)成NSData類型
NSData *data = [text dataUsingEncoding: NSUTF8StringEncoding];
10.寫入文件
BOOL success = [fileManager createFileAtPath:path contents:data attributes:nil];
11.創(chuàng)建文件夾
NSString *filePath = [path stringByAppendingPathComponent:@"holyBible.txt"];
NSString *contect = @"abcdefg”;
BOOL success = [fm createFileAtPath:filePath contents:[content dataUsingEncoding: NSUTF8StringEncoding] attributes:nil];
12.NSFileManager-讀取內(nèi)容
NSData *fileData = [fileManager contentsAtPath:filePath];
NSString *content = [[NSString alloc] initWithData:fileData dataUsingEncoding: NSUTF8StringEncoding];
13.NSData-讀取內(nèi)容
NSString *filePath = [path stringByAppendingPathComponent:@"holyBible.txt"];
NSData *data = [NSData dataWithContentOfFile:filePath];
13.1 NSString-讀取內(nèi)容
NSString *filePath = [path stringByAppendingPathComponent:@"holyBible.txt"];
NSString *content = [[NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
移動笨觅、復(fù)制文件
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.復(fù)制文件(重命名)
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數(shù)據(jù)歸檔
#######Plist就是數(shù)組、字典(xml的組織形式)直接寫到沙盒中杀糯。
字典:[dictionary writeToFile:寫入文件的路徑 atomically:是否原子寫入];
數(shù)組:[array writeToFile:寫入文件的路徑 atomically:是否原子寫入];
#######Archive歸檔解檔扫俺。
1.要?dú)w檔的類需要實(shí)現(xiàn)<NSCoding>協(xié)議
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];
// 保存數(shù)據(jù)(其中有很多類型的數(shù)據(jù))
[userDefaults setObject:@"對象" forKey:objKey];
// 讀取數(shù)據(jù)
[userDefaults objectForKey:objKey];
-
SQLite3應(yīng)用