Data processing file Read---preview 1.30

Source: Internet
Author: User
Tags tmp file

One, sandbox mechanism

The sandbox is a separate, closed space (as if it were not closed later, the outside application can also be accessed), and each application has a sandbox that stores the corresponding things (things: all non-code things, slices, sounds, attribute lists, databases, text files) (application sandbox, Is the directory of the application)

Every sandbox has three files underground. documents-document Library (caches,preferences) tmp-temporary files

Files persisted by the document application are stored in this folder.

Library/caches store cache files stored in large, less important files, itunes does not store content under this file

Library/preferences Save Preferences (you should not create a settings file directly) but instead use the Nsuserdefaults class to get and set your application's preferences (meaning not to set preferences, but to create something that can set preferences, Then let the user go to set preferences or whatever)

When TMP stores the zero file, itunes does not sync the directory, and when the iphone restarts (the TMP file is discarded when it restarts), it is possible that the TMP file under the program will be deleted when the application shuts down, and the files in that directory will be deleted

Get Directory of files

Method:

NSString *path = nssearchpathfordirectoriesindomains(nscachesdirectory ( Other types of file directories can be obtained by changing the parameters here), nsuserdomainmask, YES) [0]

The TMP file directory gets different

NSString *path = Nstemporarydirctory ();

Second, write and read the Simple object

1. Reading and inputting simple objects

String data

[NSString stringwithcontentsoffile:<# (nonnull nsstring *) #> encoding:<# (nsstringencoding) #> error:<# (Nserror * _nullable __autoreleasing * _nullable) #>]

Read type start + withcontectoffile:<# (nonnull nsstring *) #> encoding:<# (nsstringencoding) #>

Write WriteToFile:atomically:encoding:error:

Do not study NSData

Third, File Manager

File Manager Nsfilemanager (illustrated by example) to swap the location of the file, there are many ways to look at it slowly

NSString *documentpath = [self getdocument];

nsstring *pathfortext = [documentpath stringbyappendingstring:@ "/path"];

nsstring *contentpathfortext = [Pathfortext stringbyappendingstring:@ "/text.tet"];

nsstring *imagename = @ "boxer_dog_72px_1165292_easyicon.net-2";

[ImageName writetofile: Contentpathfortext atomically:YES encoding: Nsutf8stringencoding error:nil];

nsarray *cachespaths = nssearchpathfordirectoriesindomains(nscachesdirectory, Nsuserdomainmask, YES);

nsstring *cachespath = [cachespaths objectatindex:0];

nsstring *textdicpath = [Cachespath stringbyappendingstring:@ "/textdirectroy"];

nsfilemanager *filemanager = [nsfilemanager defaultmanager];

[FileManager moveitematpath: Contentpathfortext topath: Textdicpath error:nil];

File Docking Device Nsfilehandle

This class must use the Nsfilemanager method to create the file, so the file must be guaranteed and present in use, otherwise the return is nil

Objects and reads of complex objects

For complex classes of their own definition, we cannot use simple writetofile: to persist data, only by archiving and then writing files through WriteToFile.

Archive

First, the class to which the complex object belongs follows the <NSCoding> protocol, and then implements the two methods in the Protocol

-(void) Encodewithcoder: (Nscoder *) Acoder; serialization

-(void) Initwithcoder: (Nscoder *) Adecoder; de-serialization

// Create a nsmutabledata toSave the archived object

nsmutabledata *data = [nsmutabledata data];

// Create an archive public tool

nskeyedarchiver *archiver = [[nskeyedarchiver alloc] Initforwritingwithmutabledata:d ATA];

person *person = [[person alloc] init];

Person. Age = @ "+";

Person. name = @ "Kyrie";

Deposit Archive

[Archiver encodeobject:p erson forkey:@ "P1"];

End Archive

[Archiver finishencoding];

Sandbox path

nsstring *path = nssearchpathfordirectoriesindomains(nscachesdirectory, Nsuserdomainmask, YES) [0];

nsstring *personpath = [path stringbyappendingpathcomponent:@ "person"];

[Data writetofile:P ersonpath atomically:YES];

// return to archive

nsdata *getdata = [nsdata datawithcontentsoffile:P Ersonpath];

anti-archiving tools

nskeyedunarchiver *unarchiver = [[nskeyedunarchiver alloc] initforreadingwithdata: GetData];

person *person2 = [Unarchiver decodeobjectforkey:@ "P1"];

[Unarchiver finishdecoding];

NSLog(@ "%@", Person2. Name);

Data processing file Read---preview 1.30

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.