As I need to read a file when my app is launched and write it sometimes while using it, I tried to reach it with :
NSString *dataFile = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"txt"];
NSLog(@"%@",dataFile);
And the file, that should be in my project folder, is instead in the simulator folder :
2012-06-13 17:36:56.398 MyFileApp[610:15203] /Users/Rob/Library/Application Support/iPhone Simulator/5.1/Applications/1FFD4436-DCCA-4280-9E47-F6474BEE0183/MyFileApp.app/myFile.txt
So if I want to read/write it in using the simulator as well as the real device, what should I do ?
Thanks for your advices
Here is what you need to write and read a file text:
Write a file:
and read a file:
and finally I test my code at
viewDidLoad
:For write files the methods are
-[NSData writeToURL:atomically:]
and-[NSString writeToURL:atomically:encoding:error:]
can be used if you link to the Foundation framework.For reading files the methods are
-[NSData initWithContentsOfURL:options:error:]
and-[NSString initWithContentsOfURL:encoding:error:]
.To read the file from the bundle do the following
To read it from your sandbox storage (documents)
To write to document folder
Please note you will not be able to write the file in the bundle folder of your application