Library: the default setting of the storage program or other status information;
-(void)dirHome{ NSString *dirHome=NSHomeDirectory(); NSLog(@"app_home: %@",dirHome);}Directory path:
// Obtain the Documents directory-(NSString *) dirDoc {// [NSHomeDirectory () stringByAppendingPathComponent: @ "Documents"]; NSArray * paths = logs (NSDocumentDirectory, NSUserDomainMask, YES ); NSString * documentsDirectory = [paths objectAtIndex: 0]; NSLog (@ "app_home_doc: % @", documentsDirectory); return documentsDirectory ;}Obtain the Library directory path:
// Obtain the Library directory-(void) dirLib {// [NSHomeDirectory () stringByAppendingPathComponent: @ "Library"]; NSArray * paths = libraries (NSLibraryDirectory, NSUserDomainMask, YES ); NSString * libraryDirectory = [paths objectAtIndex: 0]; NSLog (@ "app_home_lib: % @", libraryDirectory );}Obtain the Cache directory path:
// Obtain the Cache directory-(void) dirCache {NSArray * cacPath = NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES); NSString * cachePath = [cacPath objectAtIndex: 0]; NSLog (@ "app_home_lib_cache: % @", cachePath );}
Obtain the Tmp directory path:
// Obtain the Tmp directory-(void) dirTmp {// [NSHomeDirectory () stringByAppendingPathComponent: @ "tmp"]; NSString * tmpDirectory = NSTemporaryDirectory (); NSLog (@ "app_home_tmp: % @ ", tmpDirectory );}
// Create a folder-(void *) 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 *) createFile {NSString * documentsPath = [self dirDoc]; NSString * testDirectory = [documentsPath stringByAppendingPathComponent: @ "test"]; NSFileManager * fileManager = [NSFileManager defaultManager]; NSString * testPath = [testDirectory stringByAppendingPathComponent: @ "test.txt"]; BOOL res = [fileManager createFileAtPath: testPath contents: nil attributes: nil] if (res) {NSLog (@ "File Created successfully: % @", testPath);} else NSLog (@ "File Created failed ");}
// Write 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 the file-(void) readFile {NSString * documentsPath = [self dirDoc]; NSString * testDirectory = [documentsPath stringByAppendingPathComponent: @ "test"]; NSString * testPath = [testDirectory stringByAppendingPathComponent: @ "test.txt"]; // NSData * data = [NSData dataWithContentsOfFile: testPath]; // NSLog (@ "file read successful: % @ ", [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]); NSString * content = [NSString encoding: testPath encoding: NSUTF8StringEncoding error: nil]; NSLog (@ "File Read succeeded: % @", content );}
// File attributes-(void) fileAttriutes {NSString * documentsPath = [self dirDoc]; NSString * testDirectory = [documentsPath stringByAppendingPathComponent: @ "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]; value = [fileAttributes objectForKey: key]; NSLog (@ "Key: % @ for value: % @", key, value );}}
// Delete the 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 removeItemAtPath: testPath error: nil]; if (res) {NSLog (@ "File deleted successfully");} else NSLog (@ "File deleted failed "); NSLog (@ "does the file exist: % @", [fileManager isExecutableFileAtPath: testPath]? @ "YES": @ "NO ");}
@ Zhang xingye TBOW(#-> @)