Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. //讀檔
  2. - (NSString *)filePath:(NSString*)name
  3. {
  4. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
  5. NSUserDomainMask, YES);
  6. NSString *documentsDirectory = [paths objectAtIndex:0];
  7. return [documentsDirectory stringByAppendingPathComponent:name];
  8. }
  9. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  10. {
  11. NSString *path = [self filePath:@"test.txt"];
  12. NSLog(@"%@",path);
  13. if ([[NSFileManager defaultManager] fileExistsAtPath:path])
  14. {
  15. NSLog(@"file exist");
  16. NSArray *array = [[NSArray alloc] initWithContentsOfFile: path];
  17. for(NSString *name in array)
  18. {
  19. NSLog(@"%@", name);
  20. }
  21. }
  22. return YES;
  23. }
  24. //存檔
  25. - (void)applicationDidEnterBackground:(UIApplication *)application
  26. {
  27. NSString *path = [self filePath:@"test.txt"];
  28. if ([[NSFileManager defaultManager] fileExistsAtPath:path])
  29. {
  30. NSLog(@"file exist");
  31. }
  32. else
  33. {
  34. NSLog(@"create file");
  35. NSArray *array = [[NSArray alloc] initWithObjects:@"andy", @"peter", @"kevin", nil];
  36. [array writeToFile:path atomically:YES];
  37. }
  38. }