iPhone:获取资源文件夹子文件夹内的文件路径

发布于 2024-08-06 16:09:23 字数 335 浏览 7 评论 0原文

我是 iPhone 编程新手。我想读取位于资源文件夹子文件夹中的文本文件的内容。

资源文件夹结构如下:

资源

  1. 文件夹1---->Data.txt
  2. 文件夹2---->Data.txt
  3. 文件夹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

  1. Folder1---->Data.txt
  2. Folder2---->Data.txt
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不再见 2024-08-13 16:09:23

您的“资源文件夹”实际上是主包(也称为应用程序包)的内容。您可以使用 pathForResource:ofType:pathForResource:ofType:inDirectory: 获取资源的完整路径。

将文件内容加载为字符串是使用 stringWithContentsOfFile:encoding:error: 方法来完成的,对于带有 initWithContentsOfFile:encoding:error: 的自动释放字符串,如果您想要保留的字符串。

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Data" 
                                                     ofType:@"txt"
                                                inDirectory:@"Folder1"];
if (filePath != nil) {
  theContents = [NSString stringWithContentsOfFile:filePath
                                          encoding:NSUTF8StringEncoding
                                             error:NULL];
  // Do stuff to theContents
}

这与 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: or pathForResource: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 with initWithContentsOfFile:encoding:error: if you want a retained string.

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Data" 
                                                     ofType:@"txt"
                                                inDirectory:@"Folder1"];
if (filePath != nil) {
  theContents = [NSString stringWithContentsOfFile:filePath
                                          encoding:NSUTF8StringEncoding
                                             error:NULL];
  // Do stuff to theContents
}

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.

宣告ˉ结束 2024-08-13 16:09:23

要继续 Psychotiks 的回答,完整的示例如下所示:

NSBundle *thisBundle = [NSBundle bundleForClass:[self class]];
NSString *filePath = nil;

if (filePath = [thisBundle pathForResource:@"Data" ofType:@"txt" inDirectory:@"Folder1"])  {

    theContents = [[NSString alloc] initWithContentsOfFile:filePath];

    // when completed, it is the developer's responsibility to release theContents

}

请注意,您可以使用 -pathForResource:ofType:inDirectory 来访问子目录中的资源。

To continue psychotiks answer a full example would look like this:

NSBundle *thisBundle = [NSBundle bundleForClass:[self class]];
NSString *filePath = nil;

if (filePath = [thisBundle pathForResource:@"Data" ofType:@"txt" inDirectory:@"Folder1"])  {

    theContents = [[NSString alloc] initWithContentsOfFile:filePath];

    // when completed, it is the developer's responsibility to release theContents

}

Notice that you can use -pathForResource:ofType:inDirectory to access ressources in sub directories.

铜锣湾横着走 2024-08-13 16:09:23

Shirkrin 的回答PeyloW 的回答<上面的 /a> 都很有用,我设法使用 pathForResource:ofType:inDirectory: 访问应用程序包中不同文件夹中具有相同名称的文件。

我还在此处找到了一个更适合我的要求的替代解决方案,所以我想我会分享它。特别是,请参阅此链接

例如,假设我有以下文件夹引用(蓝色图标,组为黄色):

在此处输入图像描述

然后我可以像这样访问图像文件:

NSString * filePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"pin_images/1/2.jpg"];
UIImage * image = [UIImage imageWithContentsOfFile:filePath];

作为旁注,pathForResource:ofType:inDirectory: 等效项如下所示:

NSString * filePath = [[NSBundle mainBundle] pathForResource:@"2" ofType:@"jpg" inDirectory:@"pin_images/1/"];

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

enter image description here

Then I can access the image files like this:

NSString * filePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"pin_images/1/2.jpg"];
UIImage * image = [UIImage imageWithContentsOfFile:filePath];

As a side note, the pathForResource:ofType:inDirectory: equivalent looks like this:

NSString * filePath = [[NSBundle mainBundle] pathForResource:@"2" ofType:@"jpg" inDirectory:@"pin_images/1/"];
你的他你的她 2024-08-13 16:09:23
    NSBundle* bundle = [NSBundle mainBundle];
    NSString* path = [bundle bundlePath];

这将为您提供捆绑包的路径。从那里开始,您可以导航您的文件夹结构。

    NSBundle* bundle = [NSBundle mainBundle];
    NSString* path = [bundle bundlePath];

This gives you the path to your bundle. From there on, you can navigate your folder structure.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文