//NSFileManager 文件管理器類 用於操作文件或目錄
NSFileManager *file = [NSFileManager defaultManager];
NSFileManager *file2 = [NSFileManager defaultManager];
//通過驗證NSFileManager是一個單例類
NSLog(@"file=%p,file2=%p",file,file2);
//1. 遍歷文件路徑: 淺度遍歷、深度遍歷
//a. 淺度遍歷: 只遍歷當前這一層的文件夾的所有文件或目錄
NSError *error = nil;
NSArray *arr = [file contentsOfDirectoryAtPath:PATH error:&error];
if(error){ //判斷如果error有值,則打印錯誤
NSLog(@"error = %@",error);
}else{ //沒有錯誤,則打印獲取的信息
NSLog(@"arr = %@",arr);
}
//b.深度遍歷:遍歷當前層與當前層下的子層
arr = [file subpathsOfDirectoryAtPath:PATH error:&error];
if(error){
NSLog(@"error = %@",error);
}else{
NSLog(@"arr = %@",arr);
}
//2.創建文件或目錄
//a. 創建文件路徑: createFileAtPath
//判斷文件是否已存在:fileExistsAtPath
BOOL bval = [file fileExistsAtPath:CREATE_FILE];
if(bval){ //返回值:YES 文件存在 NO 不存在
NSLog(@"文件已存在");
}else{
//參數1:文件路徑 參數2:內容(nil) 參數3:屬性(nil)
bval = [file createFileAtPath:CREATE_FILE contents:nil attributes:nil];
if(bval){ //返回值: YES 成功 NO 失敗
NSLog(@"文件創建成功");
}else{
NSLog(@"文件創建失敗");
}
}
//b.創建目錄路徑:
bval = [file fileExistsAtPath:CREATE_PATH];
if(bval){ //返回值:YES 目錄存在 NO 不存在
NSLog(@"目錄已存在");
}else{
//參數1:路徑 參數2:中間路徑(YES) 參數3:屬性(nil) 參數4:錯誤
bval = [file createDirectoryAtPath:CREATE_PATH withIntermediateDirectories:YES attributes:nil error:nil];
if(bval){
NSLog(@"目錄創建成功");
}else{
NSLog(@"目錄創建失敗");
}
}
//獲取路徑的屬性
NSDictionary *dic = [file attributesOfItemAtPath:CREATE_FILE error:nil];
NSLog(@"dic = %@",dic);
//NSNumber: 數字類 專門存儲數字的類
NSNumber *numA = dic[@"NSFileSize"];
NSNumber *numB = @5; //直接初始化數字類
NSLog(@"fileSize = %@,numB = %@",numA,numB);
//間接初始化數字類: 對象方法、類型方法
//通過一個整型,初始化數字類對象方法
NSNumber *numC = [[NSNumber alloc] initWithInt:6];
NSLog(@"numC=%@",numC);
//通過一個浮點型,初始化數字類類方法
NSNumber *numD = [NSNumber numberWithFloat:4.5];
NSLog(@"numD=%@",numD);
NSFileManager *file = [NSFileManager defaultManager];
//參數1:源路徑 參數2: 目標路徑 參數3:錯誤提示
//1. 文件或目錄的拷貝:
//拷貝時:源文件存在,目標文件不存在,才能拷貝成功
NSError *error = nil;
if([file fileExistsAtPath:DST_PATH])
{
NSLog(@"目標文件存在,無須拷貝");
}else{
[file copyItemAtPath:SRC_PATH toPath:DST_PATH error:&error];
if(error){
NSLog(@"error = %@",error);
}else{
NSLog(@"拷貝成功");
}
}
//2. 文件或目錄的移動:
//移動時:源文件存在,目標文件不存在,才能移動成功
if([file fileExistsAtPath:SRC_PATH]&&![file fileExistsAtPath:DST_PATH])
{
[file moveItemAtPath:SRC_PATH toPath:DST_PATH error:&error];
if(error){
NSLog(@"error = %@",error);
}else{
NSLog(@"文件的移動成功");
}
}else{
NSLog(@"不能確保源文件存在,目標文件不存在");
}
//3. 刪除
//刪除時:指定的文件存在,即可刪除成功成功
[file removeItemAtPath:DEL_PATH error:&error];
if(error){
NSLog(@"error=%@",error);
}else{
NSLog(@"刪除成功");
}
//test
NSFileManager *file = [NSFileManager defaultManager];
NSArray *array = [file contentsOfDirectoryAtPath:PATH error:nil];
NSLog(@"array = %@",array);
for (NSString *obj in array) {
//if([obj hasSuffix:@"png"])
//[obj pathExtension]: 求文件擴展名
if([[obj pathExtension] isEqualToString:@"png"])
{
//stringByAppendingPathComponent:字符串文件路徑的拼接
NSString *path = [PATH stringByAppendingPathComponent:obj];
if([file removeItemAtPath:path error:nil])
{
NSLog(@"%@刪除成功",obj);
}
}
}
//NSFileHandle 文件句柄類,用於操作文件內容(讀/寫)
//fileHandleForReadingAtPath: 以只讀方式獲取句柄對象
//fileHandleForWritingAtPath: 以只寫方式獲取句柄對象
//fileHandleForUpdatingAtPath: 以讀寫方式獲取句柄對象(推薦)
//獲取文件句柄的對象
NSFileHandle *handle = [NSFileHandle fileHandleForUpdatingAtPath:FILE_P];
if(handle){ //如果獲取句柄對象有值,則可進行讀寫文件
//-------讀--------
//讀取指定長度的內容,存儲到NSData: readDataOfLength
//默認情況下,讀取文件的指定位置從起始位置開始
//設置偏移位置; 默認位置為0
[handle seekToFileOffset:3];
//偏移到文件末尾
//[handle seekToEndOfFile];
//NSData: 二進制數據流類(非文本文件必須用NSData,如視圖流。。)
NSData *data = [handle readDataOfLength:5];
//從指定位置讀取文件內容,一直到末尾:readDataToEndOfFile
NSData *data2 = [handle readDataToEndOfFile];
//NSData轉NSString
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"str = %@",str);
NSString *str2 = [[NSString alloc] initWithData:data2 encoding:NSUTF8StringEncoding];
NSLog(@"str2 = %@",str2);
//————–寫————-
NSString *str3 = @"hello_1605";
//NSString轉NSData
NSData *data3 = [str3 dataUsingEncoding:NSUTF8StringEncoding];
//將游標偏移位置起始位置
[handle seekToFileOffset:0];
[handle writeData:data3]; //將數據流寫入文件中
}
//文本文件的讀寫操作
//參數1:路徑 參數2:編碼 參數3: 錯誤
//---讀取文件---
NSString *str = [NSString stringWithContentsOfFile:PATH encoding:NSUTF8StringEncoding error:nil];
NSLog(@"str = %@",str);
//-----寫入文件------
NSString *str2 = @"hello world!";
//字符串寫入後,替換原先文件中的內容
if([str2 writeToFile:PATH atomically:YES])
{
NSLog(@"文件寫入成功");
}
NSFileManager *file = [NSFileManager defaultManager];
//1. 創建目錄
if([file fileExistsAtPath:DIR_PATH])
{
NSLog(@"該目錄已存在");
}else{
BOOL bval =[file createDirectoryAtPath:DIR_PATH withIntermediateDirectories:YES attributes:nil error:nil];
if(bval){
NSLog(@"目錄創建成功");
}else{
NSLog(@"目錄創建失敗");
return 0;
}
}
//2. 將字符串寫入到目錄所在的文件中
NSString *str = @"I am a handsome man";
if([str writeToFile:FILE_PATH atomically:YES])
{
NSLog(@"文件寫入成功");
}else{
NSLog(@"文件創建失敗");
}
//3. 讀取文件中的內容
NSFileHandle *handle = [NSFileHandle fileHandleForUpdatingAtPath:FILE_PATH];
if(handle){
//偏移7個字符串長度
[handle seekToFileOffset:7];
NSData *data = [handle readDataToEndOfFile];
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"str = %@",str);
}
//plist:
//1. 以列表的方式存儲數據的文件
//2. 文件只能以數組或字典的類型進行存儲
//3. 用字典的方式寫入,則用字典的方式讀
//用數組的方式寫入,則用數組的方式讀
//——-plist文件的寫入——-
//1. 手動寫入(新建文件-resource-property List-新建plist文件)
//寫入時按字典類型寫入,按字典類型進行接收
//---按字典方式讀取----
//dictionaryWithContentsOfFile: 以字典方式讀取plist文件的方法
NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:PATH];
NSLog(@"dic = %@",dic);
//2. 代碼寫入
NSNumber *numA = @333;
NSArray *array = @[@"111",@"222",numA];
//writeToFile: 字典數組都用該方法寫入數據到plist文件
if([array writeToFile:PLIST atomically:YES])
{
NSLog(@"寫入成功");
}else{
NSLog(@"寫入失敗");
}
//---按數組方式讀取----
//arrayWithContentsOfFile: 以數組方式讀取plist文件的方法
NSArray *array2 = [NSArray arrayWithContentsOfFile:PLIST];
NSLog(@"array = %@",array2);
//1.單例
//a.概念:在程序的執行過程中,只初始化一次
//b.作用:節約內存; 共享內存,方便傳值
//2.NSFileManage--文件管理器類--操作文件或目錄
//常用方法:
//a. 遍歷: 深度/淺度遍歷
//b. 創建文件/目錄、 文件是否存在,獲取屬性
//c. 拷貝/移動/刪除文件或目錄
//3.NSfileHandle --文件句柄類--操作文件內容
//a.主要操作文件的讀和寫
//b.讀寫過程如何偏移
//c.NSData類與NSString間的轉換
//4. 文本文件的讀寫操作
//a.直接通過字符串進行讀寫
//b.讀取時只能讀整體數據,寫入時會覆蓋文件的原始內容
//4.plist
//a. 以列表的方式存儲數據的文件
//b. 文件只能以數組或字典的類型進行存儲
//c. 用字典的方式寫入,則用字典的方式讀
//用數組的方式寫入,則用數組的方式讀
如果這篇文章對你有幫助,記得點贊哦~O(∩_∩)O哈!