//創建文件的路徑
+(NSString *)createFilePath{
NSString * FileName=@"init.plist";//創建文件名
NSArray * Paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * Path=[Paths objectAtIndex:0];//系統會有一個路徑列表,第一個是系統路徑
NSString * FilePath=[Path stringByAppendingPathComponent:FileName];//將路徑和文件名連接起來
return FilePath;
}
//創建文件
-(void) createFile{
NSString * FilePath=[Test5AppDelegate createFilePath];//得到文件路徑
NSArray * array=[[NSArray alloc]initWithObjects:@"zhangsan",@"lisi",nil];//創建數據
[NSKeyedArchiver archiveRootObject:array toFile:FilePath];//將數據添加到文件中去
}
//讀取文件
+(NSArray *)getFile{
NSArray * array=nil;
if (!array) {
array=[NSKeyedUnarchiver unarchiveObjectWithFile:[Test5AppDelegate createFilePath]];//根據系統文件路徑得到文件中的數據
}
return array;
}