天天看點

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 =[[NSBundlemainBundle]  resourcePath];

NSString *f=[ProductPath stringByAppendingPathComponent:FileUrl];
           

[list writeToFile:f atomically:YES];

}

//寫入檔案 Array 工程

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

NSString *ProductPath =[[NSBundlemainBundle]  resourcePath];

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;
           

}