iOS 使用 NSFileManager對沙盒裏面的文件和目錄,增長,修改,刪除操做;atom
- (void)viewDidLoad { [super viewDidLoad]; self.title = @"NSFileManager"; NSFileManager *defauleManager = [NSFileManager defaultManager]; NSString *tempPath = NSTemporaryDirectory(); NSLog(@"%@",tempPath); NSString *thePath = nil; //建立一個目錄: 在temp下建立 MyFoler目錄 // thePath = [NSString stringWithFormat:@"%@/MyFolder",tempPath]; // [defauleManager createDirectoryAtPath:thePath withIntermediateDirectories:YES attributes:nil error:nil]; //寫入一個文件 在 temp目錄下 // NSString *aStr = @"hello world"; // thePath = [NSString stringWithFormat:@"%@hello.txt",tempPath]; // [aStr writeToFile:thePath atomically:YES encoding:NSUTF8StringEncoding error:nil]; //顯示目錄內容 //NSLog(@"temp目錄內容:%@",[defauleManager contentsOfDirectoryAtPath:tempPath error:nil]); //刪除一個文件 // NSString *deleteStr = @"delete Str"; // thePath = [NSString stringWithFormat:@"%@delete.txt",tempPath]; // [deleteStr writeToFile:thePath atomically:YES encoding:NSUTF8StringEncoding error:nil]; // NSLog(@"temp目錄內容:刪除以前:%@",[defauleManager contentsOfDirectoryAtPath:tempPath error:nil]); // [defauleManager removeItemAtPath:thePath error:nil]; // NSLog(@"temp目錄內容:刪除以後:%@",[defauleManager contentsOfDirectoryAtPath:tempPath error:nil]); //刪除一個目錄: // thePath = [NSString stringWithFormat:@"%@/TestFoler",tempPath]; // [defauleManager createDirectoryAtPath:thePath withIntermediateDirectories:YES attributes:nil error:nil]; // NSLog(@"temp目錄內容:%@",[defauleManager contentsOfDirectoryAtPath:tempPath error:nil]); // [defauleManager removeItemAtPath:thePath error:nil]; // NSLog(@"temp目錄內容:%@",[defauleManager contentsOfDirectoryAtPath:tempPath error:nil]); //獲取目錄下的全部文件列表: NSArray *fileList = [defauleManager contentsOfDirectoryAtPath:tempPath error:nil]; NSLog(@"%@",fileList); //判斷一個目錄是不是文件夾 //- (BOOL)fileExistsAtPath:(NSString *)path isDirectory:(BOOL *)isDirectory }