字數885 閱讀10 評論0 喜歡1
需要學習
1.什么是沙盒機制
2.數據持久化的方法
3.常用文件操作類
沙盒(sandbox)機制
它是一種安全體系。它規定了應用程序只能在為該應用創建的文件夾內讀取文件,不可以訪問其他地方的內容。所有的非代碼文件都保存在這個地方,比如圖片、聲音、plist,sqlite數據庫及其他文件等。這種“封閉獨立安全”的存儲空間就是沙盒。
每一個應用程序都有自己獨立的沙盒
應用程序不能越過自己的沙盒去訪問別人的沙盒
應用程序向外請求或接收數據都需要經過權限認證
模擬器的沙盒文件夾在電腦的存儲位置是隱藏的——輸入:
/Users/useName/Library/Application/Support/iPhone Simulator
或者在終端輸入命令: chflags nohidden~
數據持久化的方法
通常程序在運行中或者程序結束之后,需要保存一些信息,而且需要持久化存儲信息,比如登陸信息、視頻播放記錄、收藏記錄等等,那么我們可以采用以下幾種方式對數據進行持久化保存.
文件
plist
數據庫
常用文件操作類
NSFileManager(文件管理類)
NSFileManager可以用來進行常見的文件\文件夾操作的類(拷貝、剪切、創建、移動、檢查文件是否存在、刪除、重命名file等)。
OC中系統自帶的創建單例對象的方法,一般使用default開頭。NSFileManager使用了單例模式singleton。
使用defaultManager方法可以獲得那個單例對象,注意不用alloc。
//創建NSFileManager文件管理器類的單例對象
//使用defaultManager方法可以獲取單例對象
//fileManager是個單例對象
NSFileManager *fileManager=[NSFileManager defaultManager];
NSLog(@"%p",fileManager);
//NSError是專門用來存儲錯誤信息的。
NSError *error;
//淺度遍歷:返回當前目錄下,所有的一級目錄的文件夾名和文件名
NSString *path=@"/Users/qingmai/Desktop/file";
NSArray *arr1=[fileManager contentsOfDirectoryAtPath:path error:&error];
NSLog(@"%@",arr1);
//深度遍歷:返回當前目錄下所有的子文件夾的名和所有的文件名(注意:二級目錄以下,返回的時候是相對路徑)
NSArray *arr2=[fileManager subpathsOfDirectoryAtPath:path error:&error];
NSLog(@"%@",arr2);
//判斷文件(或目錄)是否存在
BOOL b1=[fileManager fileExistsAtPath:path];
NSLog(@"%d",b1);
//判斷該路徑是否是文件夾
BOOL b2; //b2用來記錄該路徑是否是文件夾
BOOL b3=[fileManager fileExistsAtPath:path isDirectory:&b2];
NSLog(@"%d",b2); //判斷結果:1
BOOL b4;
NSString *path1=@"/Users/qingmai/Desktop/file/wen.rtf";
BOOL b5=[fileManager fileExistsAtPath:path1 isDirectory:&b4];
NSLog(@"%d",b4); //判斷結果:0
//創建文件
[fileManager createFileAtPath:path contents:nil attributes:nil];
//創建文件夾
[fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
//文件/目錄的拷貝
//將file文件夾里的file2文件夾拷貝到file1里
//注意事項:1.不能有空格;2.目標路徑里必須包含要拷貝或者移動的文件的名字
NSString *fromPath=@"/Users/qingmai/Desktop/file/file2";
NSString *toPath=@"/Users/qingmai/Desktop/file/file1/file2";
[fileManager copyItemAtPath:fromPath toPath:toPath error:&error];
//文件/目錄的移動
//將file文件夾里的wen文件移動到file1文件夾里
NSString *fromPath1=@"/Users/qingmai/Desktop/file/wen.rtf";
NSString *toPath1=@"/Users/qingmai/Desktop/file/file1/wen.rtf";
[fileManager moveItemAtPath:fromPath1 toPath:toPath1 error:nil];
//刪除文件/目錄
[fileManager removeItemAtPath:toPath1 error:nil];
//打開文件,讀取文件內容,并把內容復制到另外一個文件中
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
NSFileHandle *inFile, *outFile;
NSData *buffer;
//打開讀取文件
inFile = [NSFileHandle fileHandleForReadingAtPath:@"testfile.txt"];
if (!inFile) {
NSLog(@"Open the testfile for reading failed");
return 1;
}else{
NSLog(@"Open the testfile for reading successful");
}
//判斷文件是否已經存在,如果沒有就創建
if (![[NSFileManager defaultManager] fileExistsAtPath:@"testfile2.txt"]) {
NSLog(@"testfile2.txt is not existed, and creat it.");
[[NSFileManager defaultManager] createFileAtPath:@"testfile2.txt" contents:nil attributes:nil];
}else{
NSLog(@"testfile2.txt is existed.");
}
//打開文件寫入
outFile = [NSFileHandle fileHandleForWritingAtPath:@"testfile2.txt"];
if (outFile) {
NSLog(@"Open of testout for writing successful");
}else{
NSLog(@"Open of testout for writing failed");
return 2;
}
//截斷輸出文件
[outFile truncateFileAtOffset:0];
//從inFile讀取數據到緩存中
buffer = [inFile readDataToEndOfFile];
//從緩存中讀取數據,寫入outFile
[outFile writeData:buffer];
//關閉兩個文件
[inFile closeFile];
[outFile closeFile];
//驗證文件內容
NSLog(@"%@", [NSString stringWithContentsOfFile:@"testfile2.txt" encoding:NSUTF8StringEncoding error:NULL]);
}
return 0;
}