NSFileManager檔案管理器、NSFileHandle檔案控制代碼 - iOS-目錄(系統目錄/指定路徑子目錄的建立)、檔案的建立和刪除、檔案的讀取和寫入
阿新 • • 發佈:2021-02-03
技術標籤:iOS檔案上傳/下載
- 一、.m類裡標頭檔案如下
#define DOWNLOAD_URL @"http://localhost:8080/iOSDemo/app_ios.zip" #define DOWNLOAD_URL_TEST @"/Users/nixinsheng/Library/Developer/CoreSimulator/Devices/9BA7EC04-1CAD-4026-A1BA-C9B851FCE6B7/data/Containers/Data/Application/CC318755-19A8-484C-B612-A621F2F8E01A/Library/Caches/iOSDemo/file"
- 二、ViewDidLoad裡程式碼示例 「NSFileManager檔案管理器、NSFileHandle檔案控制代碼」
- (void)viewDidLoad { [super viewDidLoad]; NSLog(@"---DOWNLOAD_URL:%@",DOWNLOAD_URL); NSString *filename = [DOWNLOAD_URL lastPathComponent]; NSLog(@"---fileName:%@",filename); NSString *path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject; NSLog(@"---Library/Caches:%@",path); NSString *fullpath = [path stringByAppendingPathComponent:filename]; NSLog(@"---fullpath:%@",fullpath); NSFileManager *fileManager = [NSFileManager defaultManager]; NSError *error = nil; //判斷當前目錄是否存在 BOOL isExist = [fileManager contentsOfDirectoryAtPath:DOWNLOAD_URL_TEST error:&error]; NSLog(@"---isExist:%d",isExist); //建立子目錄 BOOL createDirectory = [fileManager createDirectoryAtPath:DOWNLOAD_URL_TEST withIntermediateDirectories:YES attributes:nil error:&error]; NSLog(@"---createDirectory:%d",createDirectory); //判斷目標路徑下的目標檔案 是否存在 if (![fileManager fileExistsAtPath:[DOWNLOAD_URL_TEST stringByAppendingPathComponent:@"smartposapp_ios.zip"]]) { //建立一個空的檔案到沙盒中 //在指定路徑下建立檔案內容 //如果沒有下載檔案的話,就建立一個檔案。如果有下載檔案的話,則不用重新建立(不然會覆蓋掉之前的檔案) [fileManager createFileAtPath:[DOWNLOAD_URL_TEST stringByAppendingPathComponent:@"smartposapp_ios.zip"] contents:nil attributes:nil]; NSLog(@"---檔案建立完成:%@",[DOWNLOAD_URL_TEST stringByAppendingPathComponent:@"smartposapp_ios.zip"]); }else{ NSLog(@"---檔案已存在"); } BOOL removeItemAtPath = [fileManager removeItemAtPath:[DOWNLOAD_URL_TEST stringByAppendingPathComponent:@"smartposapp_ios.zip"] error:&error]; NSLog(@"---removeItemAtPath:%d",removeItemAtPath); //2、檔案的讀取和寫入 NSFileHandle *handleRead = [NSFileHandle fileHandleForReadingAtPath:[DOWNLOAD_URL_TEST stringByAppendingPathComponent:@"nixs.txt"]]; //讀取檔案內容-直到結尾 NSData *fileData = [handleRead readDataToEndOfFile]; NSString *fileStr = [[NSString alloc] initWithData:fileData encoding:NSUTF8StringEncoding]; NSLog(@"---fileStr:%@",fileStr); //關閉檔案 [handleRead closeFile]; NSFileHandle *handleWrite = [NSFileHandle fileHandleForWritingAtPath:[DOWNLOAD_URL_TEST stringByAppendingPathComponent:@"nixs.txt"]]; NSData *dataW = [@"\n加菲貓 2號 2021年01月20日16:03:18" dataUsingEncoding:NSUTF8StringEncoding]; //定位到檔案最後 [handleWrite seekToEndOfFile]; //寫入檔案 [handleWrite writeData:dataW]; //關閉檔案 [handleWrite closeFile]; }
- 三、執行結果
2021-01-20 16:07:18.812529+0800 AFNDemo_Example[15638:180053] ---DOWNLOAD_URL:http://localhost:8080/iOSDemo/app_ios.zip 2021-01-20 16:07:18.812747+0800 AFNDemo_Example[15638:180053] ---fileName:app_ios.zip 2021-01-20 16:07:18.812946+0800 AFNDemo_Example[15638:180053] ---Library/Caches:/Users/nixinsheng/Library/Developer/CoreSimulator/Devices/9BA7EC04-1CAD-4026-A1BA-C9B851FCE6B7/data/Containers/Data/Application/A13130AC-C2F0-4574-ACFD-2D2BE8BAA35B/Library/Caches 2021-01-20 16:07:18.813098+0800 AFNDemo_Example[15638:180053] ---fullpath:/Users/nixinsheng/Library/Developer/CoreSimulator/Devices/9BA7EC04-1CAD-4026-A1BA-C9B851FCE6B7/data/Containers/Data/Application/A13130AC-C2F0-4574-ACFD-2D2BE8BAA35B/Library/Caches/sapp_ios.zip 2021-01-20 16:07:18.813552+0800 AFNDemo_Example[15638:180053] ---isExist:1 2021-01-20 16:07:18.814022+0800 AFNDemo_Example[15638:180053] ---createDirectory:1 2021-01-20 16:07:18.815018+0800 AFNDemo_Example[15638:180053] ---檔案建立完成:/Users/nixinsheng/Library/Developer/CoreSimulator/Devices/9BA7EC04-1CAD-4026-A1BA-C9B851FCE6B7/data/Containers/Data/Application/CC318755-19A8-484C-B612-A621F2F8E01A/Library/Caches/iOSDemo/file/app_ios.zip 2021-01-20 16:07:18.815939+0800 AFNDemo_Example[15638:180053] ---removeItemAtPath:1 2021-01-20 16:07:18.816836+0800 AFNDemo_Example[15638:180053] ---fileStr:加菲貓 2021年01月20日15:57:22 加菲貓 2號 2021年01月20日16:03:18加菲貓 2號 2021年01月20日16:03:18 加菲貓 2號 2021年01月20日16:03:18