Basic ios sandbox operations

Source: Internet
Author: User
// For the error message NSError * error; // create the File Manager NSFileManager * fileMgr = [NSFileManager defaultManager]; // point to the file directory NSString * documentsDirectory = [NSHomeDirectory () stringByAppendingPathComponent: @ "Documents"]; // create a directory [[NSFileManager defaultManager] createDirectoryAtPath: [NSString stringWithFormat: @ "% @/myFolder", NSHomeDirectory ()] withIntermediateDirectories: YES attributes: nil error: & error];/** <create a file *// /File we want to create in the documents directory the File we want to create will appear in the File directory // Result is:/Documents/file1.txt and the Result is: /Documents/file1.txt NSString * filePath = [documentsDirectory stringByAppendingPathComponent: @ "file1.txt"]; // the string to be written NSString * str = @ "iPhoneDeveloper Tips \ nhttp: // iPhoneDevelopTips, com "; // write the file [str writeToFile: filePath atomically: YES encoding: NSUTF8StringEncoding error: & error]; // display the file directory Rong NSLog (@ "Documentsdirectory: % @", [fileMgr contentsOfDirectoryAtPath: documentsDirectory error: & error]); /** <rename a file * // rename the file NSString * filePath2 = [documentsDirectory stringByAppendingPathComponent: @ "file2.txt"] by moving the file; // determine whether to move if ([fileMgr moveItemAtPath: filePath toPath: filePath2 error: & error]! = YES) NSLog (@ "Unable to move file: % @", [error localizedDescription]); // displays the NSLog (@ "Documentsdirectory: % @", [fileMgr contentsOfDirectoryAtPath: documentsDirectory error: & error]);/** <delete a file * // determine whether to delete the file in filePath2 if ([fileMgr removeItemAtPath: filePath2 error: & error]! = YES) NSLog (@ "Unable to delete file: % @", [error localizedDescription]); // displays the NSLog (@ "Documentsdirectory: % @", [fileMgr contentsOfDirectoryAtPath: documentsDirectory error: & error]);/** <obtains the list of files and folders in a directory */NSFileManager * fileManager = [NSFileManager defamanager]; // obtain the list of files and folders in the application Documents folder NSArray * documentPaths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); NSString * documentDir = [documentPaths objectAtIndex: 0]; NSError * error2 = nil; NSArray * fileList = [[NSArray alloc] init]; // fileList is the array fileList = [fileManager contentsOfDirectoryAtPath: documentDir error: & error2] that contains the file names and folder names of all files in the folder. /** <list all subfolders in a given folder */NSMutableArray * dirArray = [[NSMutableArray alloc] init]; BOOL isDir = NO; // list the folder name for (NSString * file in fileList) {NSString * path = [documentDir stringByAppendingPathComponent: file]; [fileManager fileExistsAtPath: path isDirectory :( & isDir)]; if (isDir) {[dirArray addObject: file];} isDir = NO;} NSLog (@ "Every Thing in the dir: % @", fileList); NSLog (@ "All folders: % @", dirArray );

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.