iOS開發判斷文件是否存在

iOS的沙盒存在兩個目錄,一個是【沙盒的.app】,一個是【沙盒的document】,通常狀況數據文件、資源文件都默認放在【沙盒的.app】中,可是這裏的文件是不容許編輯,因此須要複製到【沙盒的document】中進行操做。獲取文件存放路徑的方法以下:app

- (NSString*)plistPath{spa

    NSFileManager *fileManager = [NSFileManagerdefaultManager];資源

    //訪問【沙盒的document】目錄下的問題件,該目錄下支持手動增長、修改、刪除文件及目錄string

    NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/user_info.plist"];class

    if(![fileManager fileExistsAtPath:filePath]) //若是不存在 {file

         //訪問【沙盒的.app】目錄下的文件,這個目錄是程序安裝目錄,程序運行時不容許手動修改該目錄下的文件。程序

        NSString *dataPath = [[[NSBundlemainBundle] resourcePath] stringByAppendingPathComponent:@"user_info.plist"];//獲取程序包中相應文件的路徑方法

        NSError *error;error

        //拷貝文件到沙盒的document下數據

        if([fileManager copyItemAtPath:dataPath toPath:filePath error:&error]) {

            NSLog(@"copy success");

        } else{

            NSLog(@"%@",error);

        }  

    }

    return filePath;

}

相關文章
相關標籤/搜索