iphone storing and then reading a file from Documents folder

后端 未结 1 401
庸人自扰
庸人自扰 2021-01-07 08:59

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

相关标签:
1条回答
  • 2021-01-07 09:47

    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.

    0 讨论(0)
提交回复
热议问题