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

     1 - (void)viewDidLoad
     2 {
     3     [super viewDidLoad];
     4     // Do any additional setup after loading the view.
     5     self.title =  @"拷贝文件到Sandbox";
     6     
     7     //文件类型
     8     NSString * docPath = [[NSBundle mainBundle] pathForResource:@"save1" ofType:@"dat"];
     9     
    10     // 沙盒Documents目录
    11 //    NSString * appDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    12     
    13     // 沙盒Library目录
    14     NSString * appDir = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
    15     //appLib  Library/Caches目录
    16     NSString *appLib = [appDir stringByAppendingString:@"/Caches"];
    17     
    18     BOOL filesPresent = [self copyMissingFile:docPath toPath:appLib];
    19     if (filesPresent) {
    20         NSLog(@"OK");
    21     }
    22     else
    23     {
    24         NSLog(@"NO");
    25     }
    26     
    27     // 创建文件夹
    28     NSString *createDir =  [NSHomeDirectory() stringByAppendingString:@"/test"];
    29     [self createFolder:createDir];
    30     
    31     // 把文件拷贝到Test目录
    32     BOOL filesPresent1 = [self copyMissingFile:docPath toPath:createDir];
    33     if (filesPresent1) {
    34         NSLog(@"OK");
    35     }
    36     else
    37     {
    38         NSLog(@"NO");
    39     }
    40 
    41 
    42 }
    43 
    44 /**
    45  *    @brief    把Resource文件夹下的save1.dat拷贝到沙盒
    46  *
    47  *    @param     sourcePath     Resource文件路径
    48  *    @param     toPath     把文件拷贝到XXX文件夹
    49  *
    50  *    @return    BOOL
    51  */
    52 - (BOOL)copyMissingFile:(NSString *)sourcePath toPath:(NSString *)toPath
    53 {
    54     BOOL retVal = YES; // If the file already exists, we'll return success…
    55     NSString * finalLocation = [toPath stringByAppendingPathComponent:[sourcePath lastPathComponent]];
    56     if (![[NSFileManager defaultManager] fileExistsAtPath:finalLocation])
    57     {
    58         retVal = [[NSFileManager defaultManager] copyItemAtPath:sourcePath toPath:finalLocation error:NULL];
    59     }
    60     return retVal;
    61 }
    62 
    63 /**
    64  *    @brief    创建文件夹
    65  *
    66  *    @param     createDir     创建文件夹路径
    67  */
    68 - (void)createFolder:(NSString *)createDir
    69 {
    70     BOOL isDir = NO;
    71     NSFileManager *fileManager = [NSFileManager defaultManager];
    72     BOOL existed = [fileManager fileExistsAtPath:createDir isDirectory:&isDir];
    73     if ( !(isDir == YES && existed == YES) )
    74     {
    75         [fileManager createDirectoryAtPath:createDir withIntermediateDirectories:YES attributes:nil error:nil];
    76     }
    77 }
  • 相关阅读:
    爱情三十七课,恩情仪式
    爱情三十二课,幽默的用法
    爱情四十二课,距离就是问题
    爱情二十八课,你为什么爱
    爱情三十四课,放手的时机
    爱情三十九课,爱的礼物
    爱情三十三课,读懂愤怒
    爱情三十一课,先信自己
    爱情三十课,爱情整理术
    爱情二十四课,妥协50分
  • 原文地址:https://www.cnblogs.com/W-Kr/p/5379535.html
Copyright © 2011-2022 走看看