iOS 沙盒文件操作


//獲得document

+(NSString *)documentsPath {

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

return [paths objectAtIndex:0];

}

 

//讀取工程文件

+(NSString *) ProductPath:(NSString*)filename{

 

    NSString *path = [[NSBundlemainBundle] pathForResource:filename ofType:@""];

    return  path;

}

 

//獲得document文件路徑,名字方便記憶

+(NSString *) DocumentPath:(NSString *)filename {

NSString *documentsPath = [self documentsPath];

    // NSLog(@"documentsPath=%@",documentsPath);

return [documentsPath stringByAppendingPathComponent:filename];

}

 

 

//獲得document文件路徑

+(NSString *)fullpathOfFilename:(NSString *)filename {

NSString *documentsPath = [self documentsPath];

   // NSLog(@"documentsPath=%@",documentsPath);

return [documentsPath stringByAppendingPathComponent:filename];

}

 

 

//寫入文件沙盒位置NSDictionary

+(void)saveNSDictionaryForDocument:(NSDictionary *)list  FileUrl:(NSString*) FileUrl  {

 

    NSString *f = [self fullpathOfFilename:FileUrl];

    

[list writeToFile:f atomically:YES];

}

 

//寫入文件存放到工程位置NSDictionary

+(void)saveNSDictionaryForProduct:(NSDictionary *)list  FileUrl:(NSString*) FileUrl  {

 

    NSString *ProductPath =[[NSBundlemainBundleresourcePath];

    NSString *f=[ProductPath stringByAppendingPathComponent:FileUrl];

    

[list writeToFile:f atomically:YES];

}

 

//寫入文件 Array 工程

+(void)saveOrderArrayListProduct:(NSMutableArray *)list  FileUrl :(NSString*) FileUrl {

 

    NSString *ProductPath =[[NSBundlemainBundleresourcePath];

    NSString *f=[ProductPath stringByAppendingPathComponent:FileUrl];

    

[list writeToFile:f atomically:YES];

}

//寫入文件 Array 沙盒

+(void)saveOrderArrayList:(NSMutableArray *)list  FileUrl :(NSString*) FileUrl {

NSString *f = [self fullpathOfFilename:FileUrl];

    

[list writeToFile:f atomically:YES];

}

 

 

//加載文件沙盒NSDictionary

+(NSDictionary *)loadNSDictionaryForDocument  : (NSString*) FileUrl {

 

NSString *f = [self fullpathOfFilename:FileUrl];

NSDictionary *list = [ [NSDictionaryalloc] initWithContentsOfFile:f];

 

return [list autorelease];

}

 

//加載文件工程位置NSDictionary

+(NSDictionary *)loadNSDictionaryForProduct   : (NSString*) FileUrl {

   

NSString *f = [self ProductPath:FileUrl];

    NSDictionary *list =[NSDictionarydictionaryWithContentsOfFile:f];

    

return list;

}

 

 

//加載文件沙盒NSArray

+(NSArray *)loadArrayList   : (NSString*) FileUrl {

    

NSString *f = [self fullpathOfFilename:FileUrl];

    

NSArray *list = [NSArray  arrayWithContentsOfFile:f];

    

return list;

}

 

//加載文件工程位置NSArray

+(NSArray *)loadArrayListProduct   : (NSString*) FileUrl {

    

NSString *f = [self ProductPath:FileUrl];

    

    NSArray *list = [NSArray  arrayWithContentsOfFile:f];

    

return list;

}

 

//拷貝文件到沙盒

+(int) CopyFileToDocument:(NSString*)FileName{

    

 

    NSString *appFileName =[self fullpathOfFilename:FileName];

 

    

    NSFileManager *fm = [NSFileManagerdefaultManager];  

    

    //判斷沙盒下是否存在 

    BOOL isExist = [fm fileExistsAtPath:appFileName];  

    

    if (!isExist)   //不存在,把工程的文件復制document目錄下

    {  

        

        //獲取工程中文件

        NSString *backupDbPath = [[NSBundle mainBundle]  

                                  pathForResource:FileName  

                                  ofType:@""];  

    

        

        //這一步實現數據庫的添加,  

        // 通過NSFileManager 對象的復制屬性,把工程中數據庫的路徑復制到應用程序的路徑上  

        BOOL cp = [fm copyItemAtPath:backupDbPath toPath:appFileName error:nil];  

      

    

        return cp;

        

    } else {

        

        return  -1; //已經存在

    } 

    

}

 

//判斷文件是否存在

+(BOOL) FileIsExists:(NSString*) checkFile{

     

    if([[NSFileManagerdefaultManager]fileExistsAtPath:checkFile])

    {

        return true;

    }

    return  false;

 

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM