iOS NSFileManager對沙盒文件及目錄添加刪除操作


iOS 使用 NSFileManager對沙盒里面的文件和目錄,增加,修改,刪除操作;

- (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

    
    
    
    
    
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM