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

    
    
    
    
    
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值