views:

3263

answers:

3

hi all, i am new to iphone programming. i want to read the content of text file which is located in subfolder of Resourse folder

Resourse Folder Structure is following

Resourse

  1. Folder1---->Data.txt
  2. Folder2---->Data.txt
  3. Folder3--->Folder1----->Data.txt

There are multiple file named "Data.txt". so how can i access each folder file. i know how can i read the text file. but if Resourse structure is related to above structure then how can i get the path? for example if i want to access the "Data.txt" from folder3, then how can i get the file path. Please suggest.

A: 
    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.

psychotik
+2  A: 

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.

Shirkrin
But there are multiple Folder with same Name in different folder. so in this case how can achieve the path
Rupesh
@Rupesh: For the second folder you would need to use: `[thisBundle pathForResource:@"Data" ofType:@"txt" inDirectory:@"Folder3/Folder1"]`. Notice that `inDirectory:` argument is relative to the bundle root.
PeyloW
You should really use `[NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL]`, this way the memory is _"managed"_, and more importantly `initWithContentsOfFile:` is deprecated since Mac OS X 10.4, and **not available on iPhone OS**. So the code will only work in simulator.
PeyloW
@Peylow - I agree with you.The example is a modified version from the NSBundle documentation which does not use autorelase pools.
Shirkrin
+2  A: 

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.

PeyloW