问题描述
我是新的iPhone编程。我想读取位于资源文件夹子文件夹中的文本文件的内容。
资源文件夹结构如下:
资源
- Folder1 ----> Data.txt
- Folder2 ----> Data.txt
- Folder3 ----> Folder1 ----> Data.txt
有多个名为Data.txt的文件,如何访问每个文件夹中的文件?我知道如何读取文本文件,但如果资源结构类似于上面的结构,那么如何获得路径?
例如,如果我想从文件夹3访问Data.txt文件,我如何获得文件路径?
请建议。
NSBundle * thisBundle = [NSBundle bundleForClass:[self class]];
NSString * filePath = nil;
if(filePath = [thisBundle pathForResource:@DataofType:@txtinDirectory:@Folder1]){
theContents = [[NSString alloc] initWithContentsOfFile:filePath];
//完成后,开发人员有责任释放内容
}
请注意,您可以使用-pathForResource:ofType:inDirectory访问子目录中的ressources。
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.
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.
这篇关于iPhone:获取位于资源文件夹子文件夹中的文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!