IOS. OC.01 NSFileManager-file management, nsfilemanager

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.