这一定很容易,但我想将一个文件放在“文档”文件夹中,该文件在启动时读入。我有关于如何阅读的代码,并已确认其在正确的目录中查找。但是,当我保存在 xcode 中的 Resources 文件夹中时,我的文件 RootList.txt 存储在 Root.app 文件夹下,而 Documents 文件夹为空。因此,当我启动应用程序时,它找不到该文件。
有没有办法确保在启动时将文件内置到文档目录中(我在模拟器中运行它)。
另一种方法是使用 plist,它也可以正常工作,但我只是很好奇。
在这些情况下,我遵循以下方法:
首先将 RootList.txt 保存在 xCode 的 Resources 文件夹中。您的 Documents 文件夹中还没有任何内容。
在 applicationDidLaunch 调用开始时,执行以下操作:
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];
}
现在,您的文件在启动时位于 Documents 文件夹中。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)