Documents Directory
// For error information
NSError *error;
// Create file manager
NSFileManager *fileMgr = [NSFileManager defaultManager];
// Document directory
NSString *documentsDirectory = [NSHomeDirectory()
stringByAppendingPathComponent:@"Documents"];
Creating a File
// File we want to create in the documents directory
// Result is: /Documents/file1.txt
NSString *filePath = [documentsDirectory
stringByAppendingPathComponent:@"file1.txt"];
// String to write
NSString *str = @"Maheswaran.cm";
// Write the file
[str writeToFile:filePath atomically:YES
encoding:NSUTF8StringEncoding error:&error];
// Show contents of Documents directory
NSLog(@"Documents directory: %@",[fileMgr contentsOfDirectoryAtPath:
documentsDirectory error:&error]);
Renaming a File
// Rename the file, by moving the file
NSString *filePath2 = [documentsDirectory
stringByAppendingPathComponent:@"file2.txt"];
// Attempt the move
if ([fileMgr moveItemAtPath:filePath toPath:filePath2
error:&error] != YES)
NSLog(@"Unable to move file: %@", [error localizedDescription]);
// Show contents of Documents directory
NSLog(@"Documents directory: %@",[fileMgr contentsOfDirectoryAtPath:
documentsDirectory error:&error]);
Deleting a File
// Attempt to delete the file at filePath2
if ([fileMgr removeItemAtPath:filePath2 error:&error] != YES)
NSLog(@"Unable to delete file: %@", [error localizedDescription]);
// Show contents of Documents directory
NSLog(@"Documents directory: %@",[fileMgr contentsOfDirectoryAtPath:
documentsDirectory error:&error]);