IOS File Operations Summary-continuous update ......

Source: Internet
Author: User

1. Obtain the absolute path of the file in the application package:

NSString *absoluteFileName = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"filename.ext"];

2. Obtain the absolute path of the file under the root directory of the document: (files and folders under this directory can be shared in itunes and added or removed)

// Obtain the root directory path of the document, that is, the NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) shared by itunes. // by default, there is only one file, take the first NSString * documentsDirectory = [paths objectAtIndex: 0]; // append a file name to the document root directory string, construct the complete path NSString * absoluteFileName = [documentsDirectory stringByAppendingPathComponent: @ "filename. ext "];

3. Determine whether the file in the specified path exists

// Obtain the root directory path of the document, that is, the NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) shared by itunes. // by default, there is only one file, take the first NSString * documentsDirectory = [paths objectAtIndex: 0]; // append a file name to the document root directory string, construct a complete file path NSString * absoluteFileName = [documentsDirectory stringByAppendingPathComponent: fileName]; // obtain NSFileManager * fileManager = [NSFileManager defamanager] for the file manager object Singleton; // Determine whether the file corresponding to the complete path constructed above exists if (! [FileManager fileExistsAtPath: absoluteFileName]) {// if the file does not exist, use the application package path to construct the full path of the file. absoluteFileName = [[[NSBundle mainBundle] bundlePath] stringbyappendpathcomponent: fileName];}

4. Multi-character encoding file content loading

// Read the file content using Utf-8 encoding NSString * fileContent = [NSString stringWithContentsOfFile: absoluteFileName encoding: NSUTF8StringEncoding error: nil];

// Use the encoding to read the file content NSStringEncoding stringEncoding = encoding (encoding); NSString * fileContent = [NSString stringWithContentsOfFile: absoluteFileName encoding: stringEncoding error: nil];

For more CFStringConvertEncodingToNSStringEncoding encoding parameters of the Conversion Function, refer to the relevant statements in CoreFoundation/CFStringEncodingExt. h. Try one by one and guess, you may find the desired one. Take the actual test as the standard.





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.