iphone存储然后从Documents文件夹中读取文件 [英] iphone storing and then reading a file from Documents folder

查看:347
本文介绍了iphone存储然后从Documents文件夹中读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这一定很简单,但我想把一个文件放在Documents文件夹中,该文件夹在启动时读入。我有关于如何阅读并确认其查找正确目录的代码。但是我保存在xcode的Resources文件夹中的文件RootList.txt存储在Root.app文件夹下,而Documents文件夹是空的。因此,当我启动应用程序时它找不到这个文件。

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.

有没有办法确保文件在启动时内置到Documents目录中(我正在运行这个在模拟器中)。

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

替代方案是一个工作正常的plist,但我只是很好奇。

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

推荐答案

在这些情况下我遵循这种方法:

In these situations I follow this approach:

首先将您的RootList.txt保存在xCode的Resources文件夹中。你什么都没有在你的Documents文件夹中。

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

在你的applicationDidLaunch调用开始时,执行:

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];
}

现在,您的文件在启动时位于Documents文件夹中。

Now, your file is in Documents folder at startup.

这篇关于iphone存储然后从Documents文件夹中读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆