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

    
    
    
    
    
}
复制代码

 

posted @   cocoajin  阅读(9830)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示