Simple File Processing for iOS Data Processing

Source: Internet
Author: User

1. Obtain the file path

-(Void) viewdidload
{
[Super viewdidload];

Nsstring * homedirectory = nshomedirectory (); // obtain the home path, full application path
Nsstring * filedirectory = [homedirectory stringbyappendingpathcomponent: @ "Temp/app_data.plist"];

// Nssearchpathfordirectoriesindomains
Nsarray * patharray = nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes );
Nsstring * documentdirectory = [patharray objectatindex: 0];
Nsstring * filedirectory2 = [documentdirectory stringbyappendingpathcomponent: @ "file.txt"];


Nsstring * tempdirectory = nstemporarydirectory ();
Nsstring * file = [tempdirectory stringbyappendingpathcomponent: @ "file.txt"];

}

2. File Operations

Common functions of nsfilemanager:

Create a new file.

-(Bool) createfileatpath :( nsstring *) path contents :( nsdata *) contents attributes :( nsdictionary *) attributes;

Path: Specifies the full path of the file; contents: Specifies the file content; attributes: Specifies the attributes of the file. The return value of this function is Boolean, indicating that the creation is successful or fails.

 

Create a path.

-(Bool) createdirectoryatpath :( nsstring *) path attributes :( nsdictionary *) attributes;

 

Delete an object.

-(Bool) removefileatpath :( nsstring *) path handler :( ID) Handler

Handler: Execute the filemanager: willprocesspath: And filemanager: shouldproceedaftererror callback functions. Handler can also be set to nil. In this way, when a file deletion error occurs, the operation is terminated and no is returned.

 

 

Taking saving images as an example, nsfilemanager is mainly used (this class allows you to easily access files. Creates, deletes, copies, and Pastes data)

Note: 1. Use textfield's delegate method textfiledshouldreturn:. The protocol is uitextfielddelegate.

2. Set the delegate attribute of textfileld

3. Set the return key of textfileld to go.

 

-(Bool) textfieldshouldreturn :( uitextfield *) textfield
{
Nsarray * paths = nssearchpathfordirectoriesindomains (nscachesdirectory, nsuserdomainmask, yes );
Nsstring * filepath = [paths objectatindex: 0];
Filepath = [filepath stringbyappendingpathcomponent: [textfield. Text lastpathcomponent];

Nslog (@ "filepath: % @", filepath );

// Stores the full path and data of a file.
Nsdata * imagedata = [[nsdata alloc] initwithcontentsofurl: [nsurl urlwithstring: textfield. Text];

If (imagedata)
{
Uiimage * image = [uiimage imagewithdata: imagedata];
[Imageview setimage: Image];
[Image release];

// Use createfileatpath: Contents: attributes of the nsfilemanager object to create an image file using data at a specified position. If you need to change the file attributes, create an nsdictionary object containing the corresponding key value, to attributes.
[[Nsfilemanager defaultmanager] createfileatpath: filepath contents: imagedata attributes: Nil];

}

[Textfield resignfirstresponder];
Return yes;

}

Files are stored in the caches path:

 

3. file read/write

(1) read and write plist files (with limitations, only the data types supported by plist can be serialized)

[Testarray writetofile: filepath atomically: Yes];

Note: If yes is the last parameter, data will not be directly written to the specified path when the file is written, but will be written to a "secondary File". After the data is successfully written, then copy the auxiliary file to the specified path. In this way, auxiliary files can be used to operate objects, so as to avoid damage to the original file data. Although the system overhead may be increased, it is worthwhile to ensure data correctness. We recommend that you always set the value to yes.

(2) reading and writing archiver archive data (complex data objects can be stored)

Similar to "encoding" and "decoding"

 

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.