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.