iPhone:获取资源文件夹子文件夹内的文件路径
我是 iPhone 编程新手。我想读取位于资源文件夹子文件夹中的文本文件的内容。
资源文件夹结构如下:
资源
- 文件夹1---->Data.txt
- 文件夹2---->Data.txt
- 文件夹3---->文件夹1---->Data.txt
有多个文件名为“Data.txt”,那么如何访问每个文件夹中的文件呢?我知道如何读取文本文件,但是如果资源结构与上述结构类似,那么我如何获取路径?
例如,如果我想访问Folder3中的“Data.txt”文件,如何获取文件路径?
请建议。
I am new to iPhone programming. I want to read the content of a text file located in a subfolder of the Resource folder.
The Resource folder structure is the following:
Resource
- Folder1---->Data.txt
- Folder2---->Data.txt
- Folder3---->Folder1---->Data.txt
There are multiple files named "Data.txt", so how can I access the files in each folder? I know how to read the text file, but if the Resource structure is similar to the above structure then how can I get the path?
For example, if I want to access the "Data.txt" file from Folder3, how can I get the file path?
Please suggest.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的“资源文件夹”实际上是主包(也称为应用程序包)的内容。您可以使用
pathForResource:ofType:
或pathForResource:ofType:inDirectory:
获取资源的完整路径。将文件内容加载为字符串是使用
stringWithContentsOfFile:encoding:error:
方法来完成的,对于带有initWithContentsOfFile:encoding:error:
的自动释放字符串,如果您想要保留的字符串。这与 Shirkrin 之前给出的答案几乎相同,但略有不同,它在目标上起作用。这是因为
initWithContentsOfFile:
在 Mac OS X 上已被弃用,并且在所有 iPhone 操作系统上均不可用。Your "resource folder" is actually the contents of your main bundle, also know as the application bundle. You use
pathForResource:ofType:
orpathForResource:ofType:inDirectory:
to get the full path for a resource.Loading the contents of a file as a string is done with the
stringWithContentsOfFile:encoding:error:
method for an autoreleased string of withinitWithContentsOfFile:encoding:error:
if you want a retained string.This is almost the same answer as given by Shirkrin previously, but with the slight difference that it works on target. This is because
initWithContentsOfFile:
is deprecated on Mac OS X, and not available at all iPhone OS.要继续 Psychotiks 的回答,完整的示例如下所示:
请注意,您可以使用 -pathForResource:ofType:inDirectory 来访问子目录中的资源。
To continue psychotiks answer a full example would look like this:
Notice that you can use -pathForResource:ofType:inDirectory to access ressources in sub directories.
Shirkrin 的回答和PeyloW 的回答<上面的 /a> 都很有用,我设法使用
pathForResource:ofType:inDirectory:
访问应用程序包中不同文件夹中具有相同名称的文件。我还在此处找到了一个更适合我的要求的替代解决方案,所以我想我会分享它。特别是,请参阅此链接。
例如,假设我有以下文件夹引用(蓝色图标,组为黄色):
然后我可以像这样访问图像文件:
作为旁注,
pathForResource:ofType:inDirectory:
等效项如下所示:Shirkrin's answer and PeyloW's answer above were both useful, and I managed to use
pathForResource:ofType:inDirectory:
to access files with the same name in different folders in my app bundle.I also found an alternative solution here that suited my requirements slightly better, so I thought I'd share it. In particular, see this link.
For example, say I have the following Folder References (blue icons, Groups are yellow):
Then I can access the image files like this:
As a side note, the
pathForResource:ofType:inDirectory:
equivalent looks like this:这将为您提供捆绑包的路径。从那里开始,您可以导航您的文件夹结构。
This gives you the path to your bundle. From there on, you can navigate your folder structure.