zoukankan      html  css  js  c++  java
  • IOS 从Resource文件夹下Copy文件到沙盒

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        self.title =  @"拷贝文件到Sandbox";
        
        //文件类型
        NSString * docPath = [[NSBundle mainBundle] pathForResource:@"save1" ofType:@"dat"];
        
        // 沙盒Documents目录
    //    NSString * appDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        
        // 沙盒Library目录
        NSString * appDir = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
        //appLib  Library/Caches目录
        NSString *appLib = [appDir stringByAppendingString:@"/Caches"];
        
        BOOL filesPresent = [self copyMissingFile:docPath toPath:appLib];
        if (filesPresent) {
            NSLog(@"OK");
        }
        else
        {
            NSLog(@"NO");
        }
        
        // 创建文件夹
        NSString *createDir =  [NSHomeDirectory() stringByAppendingString:@"/test"];
        [self createFolder:createDir];
        
        // 把文件拷贝到Test目录
        BOOL filesPresent1 = [self copyMissingFile:docPath toPath:createDir];
        if (filesPresent1) {
            NSLog(@"OK");
        }
        else
        {
            NSLog(@"NO");
        }
    
    
    }
    
    /**
     *    @brief    把Resource文件夹下的save1.dat拷贝到沙盒
     *
     *    @param     sourcePath     Resource文件路径
     *    @param     toPath     把文件拷贝到XXX文件夹
     *
     *    @return    BOOL
     */
    - (BOOL)copyMissingFile:(NSString *)sourcePath toPath:(NSString *)toPath
    {
        BOOL retVal = YES; // If the file already exists, we'll return success…
        NSString * finalLocation = [toPath stringByAppendingPathComponent:[sourcePath lastPathComponent]];
        if (![[NSFileManager defaultManager] fileExistsAtPath:finalLocation])
        {
            retVal = [[NSFileManager defaultManager] copyItemAtPath:sourcePath toPath:finalLocation error:NULL];
        }
        return retVal;
    }
    
    /**
     *    @brief    创建文件夹
     *
     *    @param     createDir     创建文件夹路径
     */
    - (void)createFolder:(NSString *)createDir
    {
        BOOL isDir = NO;
        NSFileManager *fileManager = [NSFileManager defaultManager];
        BOOL existed = [fileManager fileExistsAtPath:createDir isDirectory:&isDir];
        if ( !(isDir == YES && existed == YES) )
        {
            [fileManager createDirectoryAtPath:createDir withIntermediateDirectories:YES attributes:nil error:nil];
        }
    }
  • 相关阅读:
    PVLAN 简介
    SFP光模块与SFP+、XFP、QSFP、GBIC、BIDI的区别
    IP防护等级简介
    工业交换机和普通交换机的区别
    博客园 添加 “返回顶部” 按钮
    Linux SHELL中sh和bash的区别
    vpshere6 ESXI 禁止登陆 "执行此操作的权限被拒绝"
    python icmpdnshttp监控网络各个节点状态,并记录日志
    Python Threading问题:TypeError in Threading. function takes 1 positional argument but 100 were given
    django信号
  • 原文地址:https://www.cnblogs.com/joesen/p/3356977.html
Copyright © 2011-2022 走看看