//讀檔
- (NSString *)filePath:(NSString*)name
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:name];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSString *path = [self filePath:@"test.txt"];
NSLog(@"%@",path);
if ([[NSFileManager defaultManager] fileExistsAtPath:path])
{
NSLog(@"file exist");
NSArray *array = [[NSArray alloc] initWithContentsOfFile: path];
for(NSString *name in array)
{
NSLog(@"%@", name);
}
}
return YES;
}
//存檔
- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSString *path = [self filePath:@"test.txt"];
if ([[NSFileManager defaultManager] fileExistsAtPath:path])
{
NSLog(@"file exist");
}
else
{
NSLog(@"create file");
NSArray *array = [[NSArray alloc] initWithObjects:@"andy", @"peter", @"kevin", nil];
[array writeToFile:path atomically:YES];
}
}