文件操作常用操作記錄一下:
//獲取沙盒的根路徑 NSString *dirHome = NSHomeDirectory(); NSLog(@%@,dirHome); //獲取Documents目錄路徑 NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory,NSUserDomainMask, YES); NSString *documentsDirectory = [path objectAtIndex:0]; NSLog(@%@,documentsDirectory); //獲取Library 目錄路徑 path = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES); NSString *libraryDirectory = [path objectAtIndex:0]; NSLog(@%@,libraryDirectory); //獲取caches目錄路徑 path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSString *caches = [path objectAtIndex:0]; NSLog(@%@,caches); //獲取Tmp 目錄路徑 NSString *tmpDirectory = NSTemporaryDirectory(); NSLog(@%@,tmpDirectory); //創建文件夾 //1 獲得文件路徑 //2 創建文件管理器 //3 用文件管理器在目錄上創建文件 path = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory,NSUserDomainMask, YES); documentsDirectory = [path objectAtIndex:0]; documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@test]; NSFileManager *fm = [NSFileManager defaultManager]; BOOL res = [fm createDirectoryAtPath:documentsDirectory withIntermediateDirectories:YES attributes:nil error:nil]; if(!res) { NSLog(@文件夾創建成功); } else { NSLog(@文件夾創建失敗); } //創建文件(與文件夾的創建相同) path = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES); documentsDirectory = [path objectAtIndex:0]; documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@text]; documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@123.txt]; fm = [NSFileManager defaultManager]; res = [fm createDirectoryAtPath:documentsDirectory withIntermediateDirectories:YES attributes:nil error:nil]; if(res) { NSLog(@文件夾創建成功); } else { NSLog(@文件夾創建失敗); } //寫數據到文件 path = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES); documentsDirectory = [path objectAtIndex:0]; documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@text]; documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@123.txt]; NSString *content = @文件寫入的內容; res = [content writeToFile:documentsDirectory atomically:YES encoding:NSUTF8StringEncoding error:nil]; if(res) { NSLog(@文件夾創建成功); } else { NSLog(@文件夾創建失敗); } //讀文件數據 path = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES); documentsDirectory = [path objectAtIndex:0]; documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@text]; documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@123.txt]; content = [NSString stringWithContentsOfFile:documentsDirectory encoding:NSUTF8StringEncoding error:nil]; NSLog(@文件讀取成功:%@,content); //文件的屬性 path = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES); documentsDirectory = [path objectAtIndex:0]; documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@text]; documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@123.txt]; fm = [NSFileManager defaultManager]; NSDictionary *fileAttributes = [fm attributesOfItemAtPath:documentsDirectory error:nil]; NSArray *keys; id key,value; keys = [fileAttributes allKeys]; int count = [keys count]; for (int i= 0; i