-(NSString *)dataFilePath{
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory =[paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:kFilename];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSString * filePath = [self dataFilePath];
if ([[NSFileManager defaultManager]fileExistsAtPath:filePath]) {
NSArray * array_ = [[NSArray alloc]initWithContentsOfFile:filePath];
text1.text = [array_ objectAtIndex:0];
text2.text = [array_ objectAtIndex:1];
text3.text = [array_ objectAtIndex:2];
text4.text = [array_ objectAtIndex:3];
}
UIApplication * app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:app];// Do any additional setup after loading the view.
}
-(void)applicationWillResignActive:(NSNotification *)notifacation{
NSMutableArray * array_ = [[NSMutableArray alloc]init];
[array_ addObject:text1.text];
[array_ addObject:text2.text];
[array_ addObject:text3.text];
[array_ addObject:text4.text];
[array_ writeToFile:[self dataFilePath] atomically:YES];
}
读取或者新创建的文件在application下得documents里面;
NSFileManager常见方法:
(1)-(NSString *)currentDirectoryPath 获取当前目录
(2)-(BOOL)changeCurrentDiretoryPath:path 更改当前目录
(3)-(BOOL)createDirectoryAtPath:path withIntermediate Directories:(BOOL)flag attributes:attr 创建一个新目录
(4)-(BOOL)copyItemAtPath:from toPath: to error:err 复制目录结构(to不能已经存在)
(5)-(NSArray *)contentsOfDIrectoryAtPath:path error:err 列出目录内容
常用查看ios目录
(1)Documents (NSDocumentDirectory) 用于写入应用相关数据文件
(2)Library/Cashes(NSCachesDirectory) 写入应用支持文件的目录
(3)tmp(use NSTemporaryDirecory())) 临时文件