[Code Note] iOS-NSFileManager, ios-nsfilemanager
I. Code.
# Import "ViewController. h "@ interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. // obtain the application sandbox root path [self dirHome]; // obtain the Documents directory [self dirDoc]; // obtain the Library directory [self dirLib]; // obtain the Cache directory [self dirCache]; // create a folder [self createDir]; // create a file [self createFile]; // write data to the file [self writeFile];/ /Read the file [self readFile]; // file attributes [self fileAttriutes]; // delete the file [self deleteFile];} # pragma-mark-funcitons // obtain the application sandbox root path-(void) dirHome {NSString * dirHome = NSHomeDirectory (); NSLog (@ "application sandbox root path: % @ ", dirHome);} // obtain the Documents directory-(NSString *) dirDoc {// [NSHomeDirectory () stringByAppendingPathComponent: @" Documents "]; NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); NS String * documentsDirectory = [paths objectAtIndex: 0]; NSLog (@ "Documents Directory: % @", documentsDirectory); return documentsDirectory;} // obtain the Library directory-(void) dirLib {// [NSHomeDirectory () stringByAppendingPathComponent: @ "Library"]; NSArray * paths = require (NSLibraryDirectory, NSUserDomainMask, YES); NSString * libraryDirectory = [paths objectAtIndex: 0]; NSLog (@ "Library Directory: % @", librar YDirectory);} // obtain the Cache directory-(void) dirCache {NSArray * cacPath = NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES); NSString * cachePath = [cacPath objectAtIndex: 0] NSLog (@ "Cache directory: % @", cachePath);} // obtain the Tmp directory-(void) dirTmp {// [NSHomeDirectory () stringByAppendingPathComponent: @ "tmp"]; NSString * tmpDirectory = NSTemporaryDirectory (); NSLog (@ "Tmp directory: % @", tmpDirectory);} // create a folder-(vo Id) createDir {NSString * documentsPath = [self dirDoc]; NSFileManager * fileManager = [NSFileManager defaultManager]; NSString * testDirectory = [documentsPath stringByAppendingPathComponent: @ "test"]; // create the directory BOOL res = [fileManager createDirectoryAtPath: testDirectory withIntermediateDirectories: YES attributes: nil error: nil]; if (res) {NSLog (@ "folder created successfully ");} else {NSLog (@ "Folder creation failed") ;}// create a file-(void) createF Ile {NSString * documentsPath = [self dirDoc]; NSString * testDirectory = [documentsPath stringByAppendingPathComponent: @ "test"]; NSFileManager * fileManager = [NSFileManager defamanager manager]; NSString * testPath = [testDirectory stringByAppendingPathComponent: @ "test.txt"]; BOOL res = [fileManager createFileAtPath: testPath contents: nil attributes: nil]; if (res) {NSLog (@ "File Created successfully: % @", testPath);} else NSLo G (@ "file creation failed");} // write data to the file-(void) writeFile {NSString * documentsPath = [self dirDoc]; NSString * testDirectory = [documentsPath stringByAppendingPathComponent: @ "test"]; NSString * testPath = [testDirectory stringByAppendingPathComponent: @ "test.txt"]; NSString * content = @ "test write content! "; BOOL res = [content writeToFile: testPath atomically: YES encoding: NSUTF8StringEncoding error: nil]; if (res) {NSLog (@" file written successfully ");} else NSLog (@ "file write failed");} // Read File-(void) readFile {NSString * documentsPath = [self dirDoc]; NSString * testDirectory = [documentsPath stringByAppendingPathComponent: @ "test"]; NSString * testPath = [testDirectory stringByAppendingPathComponent: @ "test.txt"]; // NSData * data = [NSDat A dataWithContentsOfFile: testPath]; // NSLog (@ "File Read succeeded: % @", [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]); NSString * content = [NSString stringWithContentsOfFile: testPath encoding: NSUTF8StringEncoding error: nil]; NSLog (@ "File Read succeeded: % @", content );} // file attributes-(void) fileAttriutes {NSString * documentsPath = [self dirDoc]; NSString * testDirectory = [documentsPath stringByAppendingPathComponen T: @ "test"]; NSFileManager * fileManager = [NSFileManager defaultManager]; NSString * testPath = [testDirectory attributes: @ "test.txt"]; NSDictionary * fileAttributes = [fileManager attributesOfItemAtPath: testPath error: nil]; NSArray * keys; id key, value; keys = [fileAttributes allKeys]; int count = [keys count]; for (int I = 0; I <count; I ++) {key = [keys objectAtIndex: I]; valu E = [fileAttributes objectForKey: key]; NSLog (@ "Key: % @ for value: % @", key, value) ;}// delete a file-(void) deleteFile {NSString * documentsPath = [self dirDoc]; NSString * testDirectory = [documentsPath stringByAppendingPathComponent: @ "test"]; NSFileManager * fileManager = [NSFileManager defaultManager]; NSString * testPath = [testDirectory stringByAppendingPathComponent: @ "test.txt"]; BOOL res = [fileManager r EmoveItemAtPath: testPath error: nil]; if (res) {NSLog (@ "File deleted successfully");} else NSLog (@ "File deleted failed "); NSLog (@ "does the file exist: % @", [fileManager isExecutableFileAtPath: testPath]? @ "YES": @ "NO");} @ end
2. output.
11:17:54. 335 NSFileManager [5578: 133206] Application sandbox root path:/Users/chenlihua/Library/Developer/CoreSimulator/Devices/78D1FCBF-9990-471F-9075-168F2CE949FE/data/Containers/Data/Application/startup 11:17:54. 336 NSFileManager [5578: 133206] Documents Directory:/Users/chenlihua/Library/Developer/CoreSimulator/Devices/78D1FCBF-9990-471F-9075-168F2CE949FE/data/Containers/Data /Application/19A5C4E6-8CDD-428A-B08C-FC68363F3980/Documents2015-10-23 11:17:54. 336 NSFileManager [5578: 133206] Library Directory:/Users/chenlihua/Library/Developer/CoreSimulator/Devices/78D1FCBF-9990-471F-9075-168F2CE949FE/data/Containers/Data/Application/author/Library2015-10-23 11:17:54. 337 NSFileManager [5578: 133206] Cache directory:/Users/chenlihua/Library/Developer/CoreSimulator /Devices/78D1FCBF-9990-471F-9075-168F2CE949FE/data/Containers/Data/Application/19A5C4E6-8CDD-428A-B08C-FC68363F3980/Library/Caches2015-10-23 11:17:54. 337 NSFileManager [5578: 133206] Documents Directory:/Users/chenlihua/Library/Developer/CoreSimulator/Devices/78D1FCBF-9990-471F-9075-168F2CE949FE/data/Containers/Data/Application/Documents/Documents2015-10-23 11:17:54. 337 NSFileMan Ager [5578: 133206] folder created successfully 11:17:54. 337 NSFileManager [5578: 133206] Documents Directory:/Users/chenlihua/Library/Developer/CoreSimulator/Devices/78D1FCBF-9990-471F-9075-168F2CE949FE/data/Containers/Data/Application/Documents/Documents2015-10-23 11:17:54. 349 NSFileManager [5578: 133206] File Created successfully:/Users/chenlihua/Library/Developer/CoreSimulator/Devices/78D1FCBF-9990-471F-907 5-168F2CE949FE/data/Containers/Data/Application/19A5C4E6-8CDD-428A-B08C-FC68363F3980/Documents/test/test.txt 11:17:54. 349 NSFileManager [5578: 133206] Documents Directory:/Users/chenlihua/Library/Developer/CoreSimulator/Devices/78D1FCBF-9990-471F-9075-168F2CE949FE/data/Containers/Data/Application/Documents/Documents2015-10-23 11:17:54. 350 NSFileManager [5578: 133206] File writing Success 11:17:54. 350 NSFileManager [5578: 133206] Documents Directory:/Users/chenlihua/Library/Developer/CoreSimulator/Devices/78D1FCBF-9990-471F-9075-168F2CE949FE/data/Containers/Data/Application/Documents/Documents2015-10-23 11:17:54. 351 NSFileManager [5578: 133206] File Read successful: Test Written content! 11:17:54. 351 NSFileManager [5578: 133206] Documents Directory:/Users/chenlihua/Library/Developer/CoreSimulator/Devices/78D1FCBF-9990-471F-9075-168F2CE949FE/data/Containers/Data/Application/Documents/Documents2015-10-23 11:17:54. 352 NSFileManager [5578: 133206] Key: NSFileOwnerAccountID for value: 5012015-10-23 11:17:54. 352 NSFileManager [5578: 133206] Key: NSFileSystemFileNumber for value: 132768632015-10-23 11:17:54. 353 NSFileManager [5578: 133206] Key: NSFileExtensionHidden for value: 02015-10-23 11:17:54. 353 NSFileManager [5578: 133206] Key: NSFileSystemNumber for value: 167772202015-10-23 11:17:54. 353 NSFileManager [5578: 133206] Key: NSFileSize for value: 212015-10-23 11:17:54. 353 NSFileManager [5578: 133206] Key: NSFileGroupOwnerAccountID for value: 802015-10-23 11:17:54. 353 NSFileManager [5578: 133206] Key: NSFilePosixPermissions for value: 4202015-10-23 11:17:54. 355 NSFileManager [5578: 133206] Key: NSFileCreationDate for value: 03:17:54 + 00002015-10-23 11:17:54. 355 NSFileManager [5578: 133206] Key: NSFileExtendedAttributes for value: {"com. apple. textEncoding "= <7574662d 383b3000034323137 393834>;} 11:17:54. 355 NSFileManager [5578: 133206] Key: NSFileType for value: NSFileTypeRegular2015-10-23 11:17:54. 355 NSFileManager [5578: 133206] Key: NSFileGroupOwnerAccountName for value: admin2015-10-23 11:17:54. 355 NSFileManager [5578: 133206] Key: NSFileReferenceCount for value: 12015-10-23 11:17:54. 355 NSFileManager [5578: 133206] Key: NSFileModificationDate for value: 03:17:54 + 00002015-10-23 11:17:54. 356 NSFileManager [5578: 133206] Documents Directory:/Users/chenlihua/Library/Developer/CoreSimulator/Devices/78D1FCBF-9990-471F-9075-168F2CE949FE/data/Containers/Data/Application/Documents/Documents2015-10-23 11:17:54. 356 NSFileManager [5578: 133206] File deleted successfully 11:17:54. 356 NSFileManager [5578: 133206] whether the file exists: NO