Read and write file in iPhone - iphone

How to make file read/write operation on iPhone?which is the path i need to specify to save the file? how can i get the current working directory in iPhone?
Thank You

Write to a file:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// the path to write file
NSString *myFile = [documentsDirectory stringByAppendingPathComponent:#"myFile"];
[data writeToFile:myFile atomically:YES];
Read a file:
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"MyFile" ofType:#"txt"];
NSData *myData = [NSData dataWithContentsOfFile:filePath];
if (myData) {
// do something useful
}

Related

Error while writing data to a file in iphone

in my application, i have placed an empty
myFile.txt
when i have internet connection , i get json data from internet and save the json string in it with following code
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"myFile" ofType:#"txt"];
[myString writeToFile:filePath automatically:YES encoding:NSUTF... error:nil];
//now while retrieving it when no internet connection
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"myFile" ofType:#"txt"];
NSString *myString = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF.. error:nil];
now myString returns with #""... why i am not been able to write data?
Best Regards
Whatever you want to do is not possible (or at least strongly discouraged) to update files in the app bundle.
If you’re downloading files and want to store them on the device you should use the Documents directory. You can get the path to this directory with:
- (NSString *)getDocumentsDirectory {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return [paths objectAtIndex:0];
}
First check weather the contents are getting written to file. You can view the file in xcode.
What i suggest is when you run the app create this empty file. Then you can Read and write to that file easily.
Try Using Below code:
NSError* error = nil;
NSString* jsonFileName = [NSString stringWithFormat:#"myfile"];
NSString* jsonPath = [[NSBundle mainBundle] pathForResource:jsonFileName
ofType:#"txt"];
NSString* jsonString = [NSString stringWithContentsOfFile:jsonPath
encoding:NSUTF8StringEncoding error:&error];
Documents directory:
Write to file
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//make a file name to write the data to using the documents directory:
NSString *fileName = [NSString stringWithFormat:#"%#/textfile.txt",
documentsDirectory];
//create content - four lines of text
NSString *content = #"One\nTwo\nThree\nFour\nFive";
//save content to the documents directory
[content writeToFile:fileName
atomically:NO
encoding:NSStringEncodingConversionAllowLossy
error:nil];
Display content:
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//make a file name to write the data to using the documents directory:
NSString *fileName = [NSString stringWithFormat:#"%#/textfile.txt",
documentsDirectory];
NSString *content = [[NSString alloc] initWithContentsOfFile:fileName
usedEncoding:nil
error:nil];

How to save pdf locally from image view

How to save the pdf locally
I have loaded the pdf doc into imageView. Now i need to save into locally into iPhone device.
Any one advice me how to save the pdf file from image view
#All
Thanks in advance
For saving the pdf in document directory use below code.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:#"data.pdf"];
NSData *thedata = [NSData dataWithContentsOfURL:[NSURL URLWithString:`ADD URL OF PDF DATA`]];
[thedata writeToFile:localFilePath atomically:YES];
For retrieving the pdf use below code.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:#"data.pdf"];
if ( [[NSFileManager defaultManager] fileExistsAtPath:localFilePath] ) {
NSData *data = [NSData dataWithContentsOfFile:localFilePath];
}

I'm having trouble cacheing the path of an existing file

Here is my code, I was using a QLPreviewController for display a detail view with a pdf file which I download and stock at the beginning of the program.
It throw a exception with telling the path was null.
-(id<QLPreviewItem>)previewController:(QLPreviewController *)previewController previewItemAtIndex:(NSInteger)index{
NSString *name=[selectedItem.source lastPathComponent];
NSFileManager *fileManager=[NSFileManager defaultManager];
if([fileManager fileExistsAtPath:name] ){
NSLog(#"%# exsit! ",name); //In!!!
}
name = [name stringByDeletingPathExtension];
NSLog(#"name for QL: %#",name); //Name ok!!!
NSString *path = [[NSBundle mainBundle]pathForResource:name ofType:#"pdf"];
NSLog(#"path: %#",path); //Path: null !!!
return [NSURL fileURLWithPath:path]; //error!!
}
Files don't download to [NSBundle mainBundle]. Try changing that line to
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePathString = [documentsDirectory stringByAppendingPathComponent:fileName];`
rather than reading from [NSBundle mainBundle]. You can't download files to mainBundle. It's read only.

saving data in iphone

I am trying to download an image and audio file from the url.. i want to save the downloaded files in specific path is it possible..
please suggest me...
thanks...
save the files in the documents directory of your app.
NSString *documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [documentsDirectoryPath stringByAppendingPathComponent:#"myfile.mp3"];
BOOL success = [myMp3Data writeToFile:path atomically:YES];
Yes, it is possible.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath2 = [NSString stringWithFormat:#"%#/%#.jpg", documentsDirectory, fileName];
NSData* imageData = UIImageJPEGRepresentation(drawImage.image, 1.0);
[imageData writeToFile:filePath2 atomically:NO];

Saving a video in the form of an NSData object to file

I am calling –writeToFile:atomically: on an NSData object which contains a video I just shot.
The file is not being written and if I use the version that returns an erorr object, the error is nil.
My code is:
if ([[info valueForKey:UIImagePickerControllerMediaType] isEqualToString:#"public.movie"]) {
NSURL* url = [info valueForKey:UIImagePickerControllerMediaURL];
NSData *videoData = [NSData dataWithContentsOfURL:url];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fPath = [documentsDirectory stringByAppendingPathComponent:#"capturedVideo.MOV"];
[videoData writeToFile:fPath atomically:YES];
}
The return value is NO and when I check the existence of the written file, it is not there.
Any ideas?
Try creating file with NSFileManager before writing to it.