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. "];