iphone storing and then reading a file from Documents folder

this must be easy but I want to put a file in the Documents folder which is read in at start up. I have the code on how to read and have confirmed its looking in the correct directory. However the file I have ,RootList.txt when saved in the Resources folder in xcode, is stored under the Root.app folder and the Documents folder is empty. Therefore its not finding this file when I start the app.

Is there a way to ensure a file is built into the Documents directory at start up (I’m running this in the simulator).

  • What is NSZombie?
  • How can I increase the Tap Area for UIButton?
  • Issue with getting cell's string using a button in a custom UITableViewCell
  • Draw polyline using google map in iOS 7?
  • How do I enter RGB values into Interface Builder?
  • Round double value to 2 decimal places
  • The alternative is a plist which works fine as well but I’m just curious.

    Solutions Collect From Internet About “iphone storing and then reading a file from Documents folder”

    In these situations I follow this approach:

    First save your RootList.txt in Resources folder in xCode.You have nothing in your Documents folder, yet.

    In the beginning of your applicationDidLaunch call, do:

    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *docsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *path = [docsDirectory stringByAppendingPathComponent:@"RootList.txt"];
    if(![fileManager fileExistsAtPath:path])
    NSData *data = [NSData dataWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingString:@"/RootList.txt"]];
    [data writeToFile:path atomically:YES];

    Now, your file is in Documents folder at startup.