-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchoptions {//Override point for customization after application launch. //Sandbox (sandbox)//Documents (document, user active data storage)//Libray (resources, commonly used for storing, some data that programmers want to store)// ?? //cache (cached files)//perferences (user information and some user settings, nsuserdefaults)//tmp (temporary directory, download temporary files are usually put here)[[Nsuserdefaults standarduserdefaults] setbool:yes forkey:@"IsLogin"]; [[Nsuserdefaults standarduserdefaults] synchronize]; //2. Get the Sandbox path//here are two shortcuts to the C-language function for the directory//root directory home directorynshomedirectory (); NSLog (@"Home------%@", Nshomedirectory ()); //TEMP directory tmp directorynstemporarydirectory (); NSLog (@"Temporary-----%@", Nstemporarydirectory ()); //C function//Parameter 1: Search Folder path Nssearchpathdirectory//freq used: nsdocumentdirectory nslibrarydirectory nscachesdirectory//Parameter 2: Search under User scope//parameter 3:yes or no YES represents absolute path (basically with absolute path), no represents relative path (~)Nsarray *patharray =nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES); NSLog (@"%@", Patharray); [Patharray Firstobject]; //NSBundle. app Files PackageNSLog (@"%@", [NSBundle Mainbundle]); //1> Simple file read and write Input OutputNSString *hello =@"Hello, I/O"; //General stitching Path, use stringbyappendingpathcomponent will automatically add slashNSString *writepath = [[Patharray firstobject] stringByAppendingPathComponent:@"Hello.txt"]; Nserror*error =Nil; [Hello Writetofile:writepath atomically:yes encoding:nsutf8stringencoding error:&ERROR]; if(Error) {NSLog (@"Storage failed"); } Else{NSLog (@"Storage Success"); } //2> Read the text of the pathNserror *readerror =Nil; NSString*readstring = [NSString stringwithcontentsoffile:writepath encoding:nsutf8stringencoding error:&Readerror]; NSLog (@"%@", readString); //3> writing an array to a local fileNsarray *array = @[@"Huang Hang",@"Cloning",@"Flower Burst",@"Baby"]; NSString*arraypath = [[Patharray firstobject] stringByAppendingPathComponent:@"name.plist"]; BOOL isarraywritesuccess=[Array Writetofile:arraypath atomically:yes]; if(isarraywritesuccess) {NSLog (@"Write Success"); } Else{NSLog (@"Write Failed"); } //4> to read an arrayNsarray *namearray =[Nsarray Arraywithcontentsoffile:arraypath]; NSLog (@"%@", NameArray); //5> writing dictionaries to localNsdictionary *dict = @{@"name":@"Mafeng", @" Age":@" at", @"Sex":@"Mans"}; NSString*dictpath = [[Patharray firstobject] stringByAppendingPathComponent:@"mafeng.plist"]; BOOL isdictwritesuccess=[Dict Writetofile:dictpath Atomically:yes]; if(isdictwritesuccess) {NSLog (@"Write Success"); } Else{NSLog (@"Write Failed"); } //6> read out the dictionaryNsdictionary *dic =[Nsdictionary Dictionarywithcontentsoffile:dictpath]; NSLog (@"%@", DIC); //7> writes the data type to the localUIImage *image = [UIImage imagenamed:@"User"]; NSString*datapath = [[Patharray firstobject] stringByAppendingPathComponent:@"ImageData"]; NSData*imagedata = uiimagejpegrepresentation (image,0.1); BOOL isdatawritesuccess=[ImageData Writetofile:datapath Atomically:yes]; NSLog (@"%@", ImageData); if(isdatawritesuccess) {NSLog (@"Write Success"); } Else{NSLog (@"Write Failed"); } NSData*imagenewdata =[NSData Datawithcontentsoffile:datapath]; UIImage*fileimage =[UIImage Imagewithdata:imagenewdata]; //2. Complex object file read/write, custom type//Archiving/Anti-archiving, serialization/ deserialization//1> archiving, storing objects locallyBook *book = [bookNew]; Book.bookname=@"give up iOS from me"; Book.booktype=@"Education"; Book.bookprice=@"988.5"; Book.bookauthor=@"Dangling"; Book.bookaddress=@"Evolution University"; NSString*bookpath = [[Patharray firstobject] stringByAppendingPathComponent:@"book.plist"]; BOOL issuccess=[Nskeyedarchiver Archiverootobject:book Tofile:bookpath]; if(issuccess) {NSLog (@"Write Success"); } //2> anti-archivingBook *huangbook =[Nskeyedunarchiver Unarchiveobjectwithfile:bookpath]; NSLog (@"%@", Huangbook.bookname); //if objects want to implement archiving and anti-archiving//1. Objects corresponding to the class need to sign Coding//2. Implement write a method//1> Initwithcoder for anti-filing//2> Encodewithcoder for archiving//3. Use Keyedarchiver when archiving//4. Use Keyedunarchiver when anti-archiving//Create a File ManagerNsfilemanager *manager =[Nsfilemanager Defaultmanager]; NSString*filepath = [[Patharray firstobject] stringByAppendingPathComponent:@"10101"]; //Create a folder[manager Createdirectoryatpath:filepath Withintermediatedirectories:yes Attributes:nil Error:nil]; //whether the file existsBOOL isexists =[manager Fileexistsatpath:filepath]; //Deleting FilesBOOL Isdele =[manager Removeitematpath:bookpath Error:nil]; if(Isdele) {NSLog (@"Delete succeeded"); } Else{NSLog (@"Delete Failed"); } if(isexists) {NSLog (@"folder exists"); //Copy FilesNSString *copypath = [FilePath stringbyappendingpathcomponent:@"dict.plist"];; BOOL iscopy=[manager Copyitematpath:dictpath Topath:copypath Error:nil]; if(iscopy) {NSLog (@"Copy succeeded"); } Else{NSLog (@"Copy failed"); } //Moving FilesNSString *movepath = [FilePath stringbyappendingpathcomponent:@"mov.plist"];; BOOL Ismove=[manager Moveitematpath:dictpath Topath:movepath Error:nil]; if(ismove) {NSLog (@"Mobile Success"); } Else{NSLog (@"Move failed"); } } Else{NSLog (@"folder does not exist"); } returnYES;}
iOS file and folder creation, deletion, movement, copy, presence and read/write of simple data types