當(dāng)前位置:
首頁 >
iOS开发学无止境 - NSFileManager文件操作的十个小功能
發(fā)布時(shí)間:2023/11/30
58
豆豆
生活随笔
收集整理的這篇文章主要介紹了
iOS开发学无止境 - NSFileManager文件操作的十个小功能
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
?
(配圖的小故事還記得嘛)
?
?
NSFileManager是一個(gè)單列類,也是一個(gè)文件管理器。可以通過NSFileManager創(chuàng)建文件夾、創(chuàng)建文件、寫文件、讀文件內(nèi)容等等基本功能。
下面將介紹NSFileManager文件操作的十個(gè)小功能。我們?cè)贒ocuments里面進(jìn)行舉例,首先是獲取Documents的路徑。這個(gè)在iOS開發(fā)之沙盒機(jī)制(SandBox)已經(jīng)詳細(xì)講解過了。獲取Documents路徑方法如下:
- (NSString *)getDocumentsPath {//獲取Documents路徑NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);NSString *path = [paths objectAtIndex:0];NSLog(@"path:%@", path);return path; }創(chuàng)建文件夾
-(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");} }創(chuàng)建文件
-(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 = @"我要寫數(shù)據(jù)啦";BOOL isSuccess = [content writeToFile:iOSPath atomically:YES encoding:NSUTF8StringEncoding error:nil];if (isSuccess) {NSLog(@"write success");} else {NSLog(@"write fail");} }讀取文件內(nèi)容
-(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; }計(jì)算文件大小
- (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;} }計(jì)算整個(gè)文件夾中所有文件大小
- (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");} }移動(dòng)文件
- (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 {//通過移動(dòng)該文件對(duì)文件重命名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拉下來進(jìn)行測(cè)試。關(guān)于NSFileManager的用法還有很多,一起接續(xù)探索吧!
轉(zhuǎn)載于:https://www.cnblogs.com/xvewuzhijing/p/5003862.html
總結(jié)
以上是生活随笔為你收集整理的iOS开发学无止境 - NSFileManager文件操作的十个小功能的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用flot.js 发现x轴y轴无法显
- 下一篇: Ajax请求利用jsonp实现跨域