Simple and practical Nsfilemanager

Source: Internet
Author: User

#import "ViewController.h"@interfaceViewcontroller ()@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //additional setup after loading the view, typically from a nib. //Nsfilemanager Simple to use, File management class//1. Create an object using the Defaultmanager class method, create a singleton object (the singleton of the system and Nsuserdefaults, Nsnotification, Uiapplication,nsurlcache);Nsfilemanager*filemanger =[Nsfilemanager Defaultmanager]; //Create a folder and stitch the/path1/path2/file you want to create into the path//Get Documents PathNsarray *paths =nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES); NSString*documentpath = [Paths Objectatindex:0]; NSLog (@"path:%@", documentpath); NSString*newpath1 = [Documentpath stringbyappendingpathcomponent:@"path1"];    [Filemanger createdirectoryatpath:newpath1 withintermediatedirectories:yes Attributes:nil Error:nil]; //create a file inside a folderNSString *filepath1 = [newPath1 stringbyappendingpathcomponent:@"Text.txt"]; BOOL file=[Filemanger createfileatpath:filepath1 contents:nil Attributes:nil]; if(file) {NSLog (@"file creation succeeded"); }Else{NSLog (@"File creation failed"); }        //write to file (can also write dictionary, array, etc.)NSString *strdata =@"want to write something"; BOOL Write=[strdata writetofile:filepath1 atomically:yes encoding:nsutf8stringencoding Error:nil]; if(write) {NSLog (@"Write Success"); }Else{NSLog (@"Write Failed"); }        //determine if a file exists    if([Filemanger fileexistsatpath:filepath1]) {NSLog (@"the file exists"); }Else{NSLog (@"The file does not exist"); }        //Deleting FilesBOOL Delete =[Filemanger removeitematpath:filepath1 Error:nil]; if(delete) {NSLog (@"file deletion succeeded"); }Else{NSLog (@"File deletion failed"); }    }- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}@end

Simple and practical Nsfilemanager

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.