標籤:
一、檔案目錄
ios中每個應用程式都是一個沙箱,有自己的沙箱目錄,app之間無法訪問對方的沙箱檔案。
三種沙箱目錄:
library:系統存放檔案
documents:存放長期使用的檔案
temp:臨時目錄,app重啟時,該目錄下的檔案清空
擷取沙箱目錄,有兩種方式:
1、手動拼接
NSString *homePath = NSHomeDirectory();
NSLog(@"沙箱目錄:%@",homePath);
2、直接使用api
NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSArray *array1 = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
二、檔案操作
檔案操作涉及到兩個類
1、NSFileManager(用於檔案的建立、複製、剪下、刪除)
//------------------建立檔案/檔案夾
//擷取沙箱目錄
NSString *homePath = NSHomeDirectory();
//在沙箱目錄中建立一個檔案file.text
NSString *filePath = [homePath stringByAppendingPathComponent:@"Documents/file.text"];
//NSFileManager是單利模式,所以不能使用alloc+init建立
NSFileManager *manager = [NSFileManager defaultManager];
NSString *str = @"無線互聯";
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
//參數:檔案路徑、檔案內容、檔案的屬性
BOOL sucess = [manager createFileAtPath:filePath contents:data attributes:nil];
if(sucess){
NSLog(@"檔案建立成功");
}else{
NSLog(@"檔案建立失敗");
}
//建立檔案夾
NSString *filePaths = [homePath stringByAppendingPathComponent:@"Documents/file"];
NSError *error;
//需要傳遞一個建立失敗的指標對象,記錄建立失敗的資訊
BOOL success1 = [manager createDirectoryAtPath:filePaths withIntermediateDirectories:YES attributes:nil error:&error];
if(!success1){
NSLog(@"建立成功");
}else{
NSLog(@"建立失敗");
}
//--------------------讀取檔案
//根據路徑讀取檔案內容
NSData *datas = [manager contentsAtPath:filePath];
NSString *s = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@",s);
//--------------------移動檔案/剪下檔案
//NSFileManager中沒有提供重新命名的方法,所以我們可以藉助移動的api進行操作
//把filePath移動到targetPath目錄中
NSString *targetPath = [homePath stringByAppendingPathComponent:@"Documents/file/file2.text"];
BOOL sucess2 = [manager moveItemAtPath:filePath toPath:targetPath error:nil];
if(sucess2) {
NSLog(@"移動成功");
}else{
NSLog(@"移動失敗");
}
//--------------------複製檔案
BOOL sucess3 = [manager copyItemAtPath:filePath toPath:targetPath error:nil];
if(sucess3){
//複製成功
}else{
//複製失敗
}
//--------------------刪除檔案
//刪除之前需要判斷這個檔案是否存在
BOOL isExist = [manager fileExistsAtPath:filePath];//判斷檔案是否存在
if(isExist){
BOOL sucess4 = [manager removeItemAtPath:filePath error:nil];
if(sucess4){
//刪除成功
}else{
//刪除失敗
}
}
//--------------------擷取檔案的屬性
NSDictionary *dic = [manager attributesOfItemAtPath:filePath error:nil];
NSLog(@"%@",dic);//通過列印我們就可以查看檔案屬性的一些key
2、NSFileHandle(對檔案進行讀寫操作)
//1.-------------------字串讀寫檔案
NSString *str = @"無線互聯";
NSString *homePath = NSHomeDirectory();
NSString *filePath = [homePath stringByAppendingPathComponent:@"Documents/file.text"];
//現在有這樣的情境,第一次把字串寫入到檔案中,當我們修改字串之後,再次寫入的時候,但是可能會寫入失敗
//但是之前的內容也有可能丟失,因為每次在寫入新的內容的時候,會剪下之前的內容,所以這裡就有可能新的沒有寫
//成功,舊的檔案也丟失了
//所以這時候atomically參數:
//YES:會將新內容先寫入到一個快取檔案中,如果寫入緩衝成功之後,這時候就將這個快取檔案替換舊檔案,這樣就很安全了
BOOL sucess = [str writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
if(sucess){
//寫入成功
}else{
//寫入失敗
}
//讀取檔案內容到字串中
//類方法
NSString *str1 = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
//構造方法
//str1 = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",str1);
//2.--------------------NSData讀寫
//建立NSData的同時讀取檔案中的內容
NSData *data = [[NSData alloc] initWithContentsOfFile:filePath];
//NSData轉化成NSString
NSString *s = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@",s);
//寫檔案
BOOL sucess1 = [data writeToFile:filePath atomically:YES];
if(sucess1){
//寫入成功
}else{
//寫入失敗
}
//3.--------------------NSArray讀寫檔案
NSArray *array = @[@"zhangsan",@"lisi"];
//屬性檔案一般尾碼名為.plist
NSString *filePaths = [homePath stringByAppendingPathComponent:@"Documents/array.plist"];
BOOL sucess2 = [array writeToFile:filePaths atomically:YES];
if(sucess2){
//寫入成功
}else{
//寫入失敗
}
//讀檔案
NSArray *arrays = [[NSArray alloc] initWithContentsOfFile:filePath];
NSLog(@"%@",arrays);
//4.---------------------NSDictionary讀寫檔案
NSDictionary *dic = @{@"zhang":@"zhangsan",@"li":@"lisi"};
BOOL sucess3 = [dic writeToFile:filePath atomically:YES];
if(sucess3){
//寫入成功
}else{
//寫入失敗
}
//讀檔案
dic = [[NSDictionary alloc] initWithContentsOfFile:filePath];
NSLog(@"%@",dic);
//追加資料
NSString *str = @"無線互聯";
NSString *homePath = NSHomeDirectory();
NSString *filePath = [homePath stringByAppendingPathComponent:@"file.test"];
[str writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];//寫入檔案
NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:filePath];
//預設是從開始位置寫,所以我們需要將寫入遊標設定到尾部
//從檔案的末尾寫入
[handle seekToEndOfFile];
NSString *s = @"123";
NSData *data = [s dataUsingEncoding:NSUTF8StringEncoding];
[handle writeData:data];
//關閉檔案
[handle closeFile];
//讀取檔案
NSFileHandle *handles = [NSFileHandle fileHandleForReadingAtPath:filePath];
//擷取檔案的大小
NSFileManager *fileManager = [NSFileManager defaultManager];
NSDictionary *fileAttr = [fileManager attributesOfItemAtPath:filePath error:nil];
NSNumber *fileSize = [fileAttr objectForKey:NSFileSize];
long long sizeValue = [fileSize longLongValue];
//設定位移量
[handle seekToFileOffset:sizeValue/2];//將位移量設定到中間位置
//從當前位移量讀到檔案末尾
NSData *datas = [handle readDataToEndOfFile];
NSString *s2 = [[NSString alloc] initWithData:datas encoding:NSUTF8StringEncoding];
NSLog(@"%@",s2);
//實現複製檔案的功能
//使用NSFileHandle只能讀寫已經存在的檔案,不能建立檔案,建立檔案應該使用NSFileManager
NSString *targetPath = [homePath stringByAppendingPathComponent:@"files.test"];
NSFileManager *fileManagers = [NSFileManager defaultManager];
[fileManagers createFileAtPath:targetPath contents:nil attributes:nil];
//建立讀取檔案的handle
NSFileHandle *readHandles = [NSFileHandle fileHandleForReadingAtPath:filePath];
//建立寫檔案的handle
NSFileHandle *writeHandles = [NSFileHandle fileHandleForWritingAtPath:targetPath];
//從當前位移量讀取到檔案的末尾
NSData *datass = [readHandles readDataToEndOfFile];
//還有一種方式讀取檔案,既可以讀取檔案,也可以讀流,功能更強
//[readHandles availableData];
[writeHandles writeData:datass];
//關閉檔案
[readHandles closeFile];
[writeHandles closeFile];
//這裡有問題,就是讀取檔案的時候全部讀取了,這樣會很占記憶體的,所以我們應該將讀取內容進行分段
IOS 檔案系統