views:

227

answers:

1

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).

The alternative is a plist which works fine as well but I'm just curious.

+2  A: 

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.

ahmet emrah
thanks ahmet that makes sense