IOS NSFileManager file operations, iosnsfilemanager

Source: Internet
Author: User

IOS NSFileManager file operations, iosnsfilemanager

Reference: http://blog.csdn.net/xyz_lmn/article/details/8968213 thanks to these pioneers

Common file operations:

// Obtain the sandbox root path NSString * dirHome = NSHomeDirectory (); NSLog (@ "% @", dirHome ); // obtain the Documents directory path NSArray * path = NSSearchPathForDirectoriesInDomains (NSDocumentationDirectory, NSUserDomainMask, YES); NSString * documentsDirectory = [path objectAtIndex: 0]; NSLog (@ "% @", documentsDirectory); // obtain the Library directory path = NSSearchPathForDirectoriesInDomains (NSLibraryDirectory, NSUserDomainMask, YES); NSString * libraryD Irectory = [path objectAtIndex: 0]; NSLog (@ "% @", libraryDirectory); // obtain the caches directory path = NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES ); NSString * caches = [path objectAtIndex: 0]; NSLog (@ "% @", caches); // obtain the Tmp directory path NSString * tmpDirectory = NSTemporaryDirectory (); NSLog (@ "% @", tmpDirectory ); // create a folder // 1 obtain the file path // 2 create a file manager // 3 create a file path in the directory using the File Manager = NSSearchPathForDirector IesInDomains (NSDocumentationDirectory, NSUserDomainMask, YES); documentsDirectory = [path objectAtIndex: 0]; documentsDirectory = [documentsDirectory failed: @ "test"]; NSFileManager * fm = [NSFileManager defamanager manager]; BOOL res = [fm createDirectoryAtPath: documentsDirectory withIntermediateDirectories: YES attributes: nil error: nil]; if (! Res) {NSLog (@ "folder created successfully");} else {NSLog (@ "Folder creation failed");} // create a file (same as the Folder creation) path = catalog (NSDocumentationDirectory, NSUserDomainMask, YES); documentsDirectory = [path objectAtIndex: 0]; documentsDirectory = [documentsDirectory stringByAppendingPathComponent: @ "text"]; documentsDirectory = [documentsDirectory catalog metadata: @ "123.txt"]; fm = [NSFileManager defaultManager]; res = [fm createDirectoryAtPath: documentsDirectory withIntermediateDirectories: YES attributes: nil error: nil]; if (res) {NSLog (@ "folder created successfully");} else {NSLog (@ "folder created failed");} // write data to the file path = NSSearchPathForDirectoriesInDomains (NSDocumentationDirectory, NSUserDomainMask, YES); documentsDirectory = [path objectAtIndex: 0]; documentsDirectory = [documentsDirectory stringByAppendingPathComponent: @ "text"]; documentsDirectory = [documentsDirectory stringByAppendingPathComponent: @ "123.txt"]; NSString * content = @ "file written content"; res = [content writeToFile: documentsDirectory atomically: YES encoding: NSUTF8StringEncoding error: nil]; if (res) {NSLog (@ "folder created successfully");} else {NSLog (@ "folder created failed");} // read file data path = NSSearchPathForDirectoriesInDomains (NSDocumentationDirectory, NSUserDomainMask, YES); documentsDirectory = [path objectAtIndex: 0]; documentsDirectory = [documentsDirectory stringByAppendingPathComponent: @ "text"]; documentsDirectory = [documentsDirectory stringByAppendingPathComponent: @ "123.txt"]; content = [NSString stringWithContentsOfFile: documentsDirectory encoding: NSUTF8StringEncoding error: nil]; NSLog (@ "File Read succeeded: % @", content ); // file attributes path = require (NSDocumentationDirectory, NSUserDomainMask, YES); documentsDirectory = [path objectAtIndex: 0]; documentsDirectory = [documentsDirectory stringByAppendingPathComponent: @ "text"]; documentsDirectory = [documentsDirectory attributes: @ "123.txt"]; fm = [NSFileManager defaultManager]; NSDictionary * fileAttributes = [fm attributesOfItemAtPath: documentsDirectory 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 file path = NSSearchPathForDirectoriesInDomains (NSDocumentationDirectory, NSUserDomainMask, YES); documentsDirectory = [path objectAtIndex: 0]; documentsDirectory = [documentsDirectory stringByAppendingPathComponent: @ "text"]; documentsDirectory = [documentsDirectory stringByAppendingPathComponent: @ "123.txt"]; fm = [NSFileManager defaultManager]; res = [fm removeItemAtPath: documentsDirectory error: nil]; if (res) {NSLog (@ "File deleted successfully") ;}{ NSLog (@ "File deleted failed ");}






In iOS development, the binary stream of documents such as word, excel, and pdf is stored locally (iPhone or iPad ).

Can't Data be directly written into the disk? The format of the file you received is saved as follows:
 
Ios and iso files

It should be an ISO file, which may be a mistake. Generally, all downloaded files are ISO image files, which are used for CD burning or installation and extraction.

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.