OBJ-C file read method combination

Source: Internet
Author: User

 

Method 1:

Plist files are standard XML files that can be easily used in cocoa. The following code applies to both Mac and iPhone.

 

Method for writing plist files:

Nsmutabledictionary * dict = [nsmutabledictionaryalloc]
Initwithcontentsoffile: @ "/test. plist"];

[Dict
Setobject: @ "yes" forkey: @ "start"];

[Dict
Writetofile: @ "/test. plist" atomically: Yes];

Methods for reading plist files:

Nsmutabledictionary * dict = [nsmutabledictionaryalloc]
Initwithcontentsoffile: @ "/test. plist"];

Nsstring * object =
[Dictobjectforkey: @ "start"];

 

Method 2:

# Import "managerfile. H"

@ Implementation managerfile

-(Void) writefile :( nsstring *) File
{
// Create a file manager
Nsfilemanager * filemanager = [nsfilemanager defaultmanager];
// Obtain the path
// Obtain the path of the nsdocumentdirectory Parameter
Nsarray * paths = nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes );
Nsstring * documentsdirectory = [paths objectatindex: 0]; // The path required for the path.
// Change to the directory to be operated
[Filemanager changecurrentdirectorypath: [documentsdirectory stringbyexpandingtildeinpath];
// Create the file filename file name and the content of the contents file. If there is no content at the beginning, you can set it to nil, the attribute of the attributes file, initially Nil
// Obtain the file path
[Filemanager removeitematpath: @ "username" error: Nil];
Nsstring * Path = [documentsdirectory stringbyappendingpathcomponent: @ "username"];
// Create a data buffer
Nsmutabledata * Writer = [nsmutabledata alloc] init];
// Add the string to the buffer
[Writer appenddata:];
// Add other data to the buffer
// Write the buffered data to the file
[Writer writetofile: path atomically: Yes];
[Writer release];
}
-(Nsstring *) readfile
{
// Create a file manager
Nsfilemanager * filemanager = [nsfilemanager defaultmanager];
// Obtain the path
// Obtain the path of the nsdocumentdirectory Parameter
Nsarray * paths = nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes );
Nsstring * documentsdirectory = [paths objectatindex: 0]; // The path required for the path.
// Change to the directory to be operated
[Filemanager changecurrentdirectorypath: [documentsdirectory stringbyexpandingtildeinpath];
// Obtain the file path
Nsstring * Path = [documentsdirectory stringbyappendingpathcomponent: @ "username"];
Nsdata * reader = [nsdata datawithcontentsoffile: path];
Return [[nsstring alloc] initwithdata: Reader
Encoding: nsutf8stringencoding];
}
@ End

Method 3:

To obtain a bundle file path, you must use the nsbundlepathForResource:ofType:

NSBundle *mainBundle = [NSBundle mainBundle]; NSString *filePath = [mainBundle pathForResource:@"filename" ofType:@"txt"];

Then you can read the nsstring of a file and use it directly on this path.initWithContentsOfFile:usedEncoding:error:

NSStringEncoding encoding; NSError *error; NSString *fileContents = [[[NSString alloc] initWithContentsOfFile:filePath                                                       usedEncoding:&encoding                                                              error:&error]                           autorelease];

 

I will sort out some file operations if I have time. "];

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.