日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 默认站点!

默认站点

當前位置: 首頁 >

iOS开发学无止境 - NSFileManager文件操作的十个小功能

發布時間:2023/11/30 32 豆豆
默认站点 收集整理的這篇文章主要介紹了 iOS开发学无止境 - NSFileManager文件操作的十个小功能 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

(配圖的小故事還記得嘛)

?

?

NSFileManager是一個單列類,也是一個文件管理器。可以通過NSFileManager創建文件夾、創建文件、寫文件、讀文件內容等等基本功能。

下面將介紹NSFileManager文件操作的十個小功能。我們在Documents里面進行舉例,首先是獲取Documents的路徑。這個在iOS開發之沙盒機制(SandBox)已經詳細講解過了。獲取Documents路徑方法如下:

- (NSString *)getDocumentsPath {//獲取Documents路徑NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);NSString *path = [paths objectAtIndex:0];NSLog(@"path:%@", path);return path; }

創建文件夾

-(void)createDirectory{NSString *documentsPath =[self getDocumentsPath];NSFileManager *fileManager = [NSFileManager defaultManager];NSString *iOSDirectory = [documentsPath stringByAppendingPathComponent:@"iOS"];BOOL isSuccess = [fileManager createDirectoryAtPath:iOSDirectory withIntermediateDirectories:YES attributes:nil error:nil];if (isSuccess) {NSLog(@"success");} else {NSLog(@"fail");} }

創建文件

-(void)createFile{NSString *documentsPath =[self getDocumentsPath];NSFileManager *fileManager = [NSFileManager defaultManager];NSString *iOSPath = [documentsPath stringByAppendingPathComponent:@"iOS.txt"];BOOL isSuccess = [fileManager createFileAtPath:iOSPath contents:nil attributes:nil];if (isSuccess) {NSLog(@"success");} else {NSLog(@"fail");} }

寫文件

-(void)writeFile{NSString *documentsPath =[self getDocumentsPath];NSString *iOSPath = [documentsPath stringByAppendingPathComponent:@"iOS.txt"];NSString *content = @"我要寫數據啦";BOOL isSuccess = [content writeToFile:iOSPath atomically:YES encoding:NSUTF8StringEncoding error:nil];if (isSuccess) {NSLog(@"write success");} else {NSLog(@"write fail");} }

讀取文件內容

-(void)readFileContent{NSString *documentsPath =[self getDocumentsPath];NSString *iOSPath = [documentsPath stringByAppendingPathComponent:@"iOS.txt"];NSString *content = [NSString stringWithContentsOfFile:iOSPath encoding:NSUTF8StringEncoding error:nil];NSLog(@"read success: %@",content); }

判斷文件是否存在

- (BOOL)isSxistAtPath:(NSString *)filePath{NSFileManager *fileManager = [NSFileManager defaultManager];BOOL isExist = [fileManager fileExistsAtPath:filePath];return isExist; }

計算文件大小

- (unsigned long long)fileSizeAtPath:(NSString *)filePath{NSFileManager *fileManager = [NSFileManager defaultManager];BOOL isExist = [fileManager fileExistsAtPath:filePath];if (isExist){unsigned long long fileSize = [[fileManager attributesOfItemAtPath:filePath error:nil] fileSize];return fileSize;} else {NSLog(@"file is not exist");return 0;} }

計算整個文件夾中所有文件大小

- (unsigned long long)folderSizeAtPath:(NSString*)folderPath{NSFileManager *fileManager = [NSFileManager defaultManager];BOOL isExist = [fileManager fileExistsAtPath:folderPath];if (isExist){NSEnumerator *childFileEnumerator = [[fileManager subpathsAtPath:folderPath] objectEnumerator];unsigned long long folderSize = 0;NSString *fileName = @"";while ((fileName = [childFileEnumerator nextObject]) != nil){NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];folderSize += [self fileSizeAtPath:fileAbsolutePath];}return folderSize / (1024.0 * 1024.0);} else {NSLog(@"file is not exist");return 0;} }

刪除文件

-(void)deleteFile{NSString *documentsPath =[self getDocumentsPath];NSFileManager *fileManager = [NSFileManager defaultManager];NSString *iOSPath = [documentsPath stringByAppendingPathComponent:@"iOS.txt"];BOOL isSuccess = [fileManager removeItemAtPath:iOSPath error:nil];if (isSuccess) {NSLog(@"delete success");}else{NSLog(@"delete fail");} }

移動文件

- (void)moveFileName {NSString *documentsPath =[self getDocumentsPath];NSFileManager *fileManager = [NSFileManager defaultManager];NSString *filePath = [documentsPath stringByAppendingPathComponent:@"iOS.txt"];NSString *moveToPath = [documentsPath stringByAppendingPathComponent:@"iOS.txt"];BOOL isSuccess = [fileManager moveItemAtPath:filePath toPath:moveToPath error:nil];if (isSuccess) {NSLog(@"rename success");}else{NSLog(@"rename fail");} }

重命名

- (void)renameFileName {//通過移動該文件對文件重命名NSString *documentsPath =[self getDocumentsPath];NSFileManager *fileManager = [NSFileManager defaultManager];NSString *filePath = [documentsPath stringByAppendingPathComponent:@"iOS.txt"];NSString *moveToPath = [documentsPath stringByAppendingPathComponent:@"rename.txt"];BOOL isSuccess = [fileManager moveItemAtPath:filePath toPath:moveToPath error:nil];if (isSuccess) {NSLog(@"rename success");}else{NSLog(@"rename fail");} }

以上文件操作代碼都在iOSStrongDemo,可以直接通過git拉下來進行測試。關于NSFileManager的用法還有很多,一起接續探索吧!

轉載于:https://www.cnblogs.com/xvewuzhijing/p/5003862.html

總結

以上是默认站点為你收集整理的iOS开发学无止境 - NSFileManager文件操作的十个小功能的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得默认站点網站內容還不錯,歡迎將默认站点推薦給好友。