I am writing content in iPhone. I used this code
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0]stringByAppendingPathComponent:@"data.txt"];
NSString *temp = [[NSString alloc] initWithString:@"my name is gaurav"];
[temp writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
[temp release];
While I am trying to over write this file with new content with this code
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0]stringByAppendingPathComponent:@"data.txt"];
NSString *matter = [[NSString alloc] initWithString:@"Hello iPhone"];
[matter writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
[matter release];
If I passed YES in atomically then file is not overwritten. and If I placed No there then file will overwrite but program is also halted and exit.
[matter writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
I have tried. it is working. you just create test project. and write following code in viewDidLod method. and see text file content from document directory. it will get overwrite.