iOS 沙盒文件增删

图片 写入 沙盒
//此处首先指定了图片存取路径(默认写到应用程序沙盒 中)    
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);    

//并给文件起个文件名   
NSString *uniquePath=[[paths objectAtIndex:0] stringByAppendingPathComponent:@"pin"];  
BOOL blHave=[[NSFileManager defaultManager] fileExistsAtPath:uniquePath];  
  if (blHave) { 
       NSLog(@"already have"); 
       return ;   
  }  

//此处的方法是将图片写到Documents文件中 如果写入成功会弹出一个警告框,提示图片保存成功   
NSString *strPathOld = [[NSBundle mainBundle] pathForResource:@"pin" ofType:@"png"];  
NSData *data = [NSData dataWithContentsOfFile:strPathOld]; 
BOOL result = [data writeToFile:uniquePath atomically:YES];    
if (result) {
        NSLog(@"success");    
}else {
        NSLog(@"no success");    
}
删除沙盒里的文件
NSFileManager* fileManager=[NSFileManager defaultManager];    
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);     
   
//文件名    
NSString *uniquePath=[[paths objectAtIndex:0] stringByAppendingPathComponent:@"pin.png"];    
BOOL blHave=[[NSFileManager defaultManager] fileExistsAtPath:uniquePath];    
if (!blHave) {
        NSLog(@"no  have");
        return ;    
}else {
        NSLog(@" have");
        BOOL blDele= [fileManager removeItemAtPath:uniquePath error:nil];
        if (blDele) {
            NSLog(@"dele success");
        }else {
            NSLog(@"dele fail");
        }    
}
向沙盒里 写入文件夹,并向文件夹里 写入东西
//创建文件夹
NSFileManager *fileManager = [NSFileManager defaultManager];    
NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];    
NSString *folder = [document stringByAppendingPathComponent:@"folder"];    
NSString *filePath = [folder stringByAppendingPathComponent:@"test.png"];        
if (![fileManager fileExistsAtPath:folder]) {
        BOOL blCreateFolder= [fileManager createDirectoryAtPath:folder withIntermediateDirectories:NO attributes:nil error:NULL];
        if (blCreateFolder) {
            NSLog(@" folder success");
        }else {
            NSLog(@" folder fial");
        }    
}else {
        NSLog(@" 沙盒文件已经存在");    
}        

//写入文件
if (![fileManager fileExistsAtPath:filePath]) {
        NSString *strPathOld = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"png"];
        NSData *data = [NSData dataWithContentsOfFile:strPathOld];
        BOOL result = [data writeToFile:filePath atomically:YES];
        if (result) {
            NSLog(@"success");
        }else {
            NSLog(@"no success");
        }    
}
得到沙盒文件夹 下的所有文件
NSFileManager *fileManager = [NSFileManager defaultManager];        
NSString *document=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];    
NSString *folder =[document stringByAppendingPathComponent:@"folder"];    
NSArray *fileList ;    
fileList =[fileManager contentsOfDirectoryAtPath:folder error:NULL];                
for (NSString *file in fileList) {
        NSLog(@"file=%@",file);
        NSString *path =[folder stringByAppendingPathComponent:file];
        NSLog(@"得到的路径=%@",path);    
}

来源地址:http://www.cocoachina.com/bbs/read.php?tid-78784.html

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 在此特此声明:一下所有链接均来自互联网,在此记录下我的查阅学习历程,感谢各位原创作者的无私奉献 ! 技术一点一点积...
    远航的移动开发历程阅读 13,893评论 12 197
  • 转载自cocoaChina http://www.cocoachina.com/bbs/read.php?tid...
    wzjmyff阅读 3,203评论 0 0
  • iOS功能 iOS 如何跳转到系统设置里的指定子功能界面 http://blog.csdn.net/jingfa1...
    EmmaLyx阅读 3,961评论 0 4
  • 不知不觉2017年的余额已经所剩无几了 下面是我这一年来收藏的关于IOS开发的一些知识点 . iOS功能 iOS ...
    临渊还在阅读 3,950评论 0 0
  • 那是一个冬天,我是在一个角落里发现刘华的,他那时正在"吃饭"呢,但是,所谓的饭,就是他那点儿从垃圾桶里挑出来的已经...
    孔雀公主阅读 1,315评论 0 0

友情链接更多精彩内容