IOS. OC.01 NSFileManager-file management, nsfilemanager
// Create a file
// Convert the string file into NSData before it can be written to the file
NSFileManager * manager = [NSFileManager defaultManager]; // create an object
NSString * homePath = NSHomeDirectory (); // shahe directory
NSString * path = [homePath stringByAppendingString: @ "file. text"]; // file storage path
NSString * text = @ "OMG"; // content written to the file
NSData * data = [text dataUsingEncoding: NSUTF8StringEncoding]; // converts a string to NSData
BOOL success = [manager createFileAtPath: path contents: data attributes: nil]; // write a file
// Read the file
NSFileManager * fileManager = [NSFileManager defaultManager];
NSData * fileData = [fileManager contentsAtPath: path]; // Read File Content Based on path
NSString * fileString = [[NSString alloc] initWithData: fileData encoding: NSUTF8StringEncoding]; // convert NSData to NSString
// Get the file size
NSFileManager * fm = [NSFileManager defamanager manager];
NSDictionary * arttriDic = [fm attributesOfItemAtPath: path error: nil]; // get the attribute Dictionary of the file
NSNumber * fileSize = [arttriDic objectForKey: NSFileSize]; // get the file size
// Write and read files
// NSArray, NSString, NSData, and NSDictionary all contain data. They all have a writeToFile method to write data to a file, and both have a method to read the file.
// Write
NSArray * array = @ [@ "Zhang San", @ "Li Si"];
NSString * path = [NSHomeDirectory () stringByAppendingString: @ "text"];
[Array writeToFile: path atomically: YES];
// Read
NSArray * arr = [[NSArray alloc] initWithContentsOfFile: path]; // alloc
NSArray * arr2 = [NSArray arrayWithContentsOfFile: path]; // Class Method
In case of any errors, please do not give me any further information.