IOS about file operations

Source: Internet
Author: User

Recently done something, encountered the use of file problems, took a little time to study the document!

one about the generation of file paths

The method I use is:

-(nsstring*) DataFilePath

{

Nsarray * paths = Nssearchpathfordirectoriesindomains (Nsdocumentationdirectory, Nsuserdomainmask, YES);

NSString * documentsdirectory = [paths objectatindex:0];

NSLog (@ " -0--%@", documentsdirectory);

return [documentsdirectory stringbyappendingpathcomponent:@ "cataog.db"];

}

But nsdocumentdirectory refers to the corresponding documents path in the program,

Nsdocumentationdiretory is corresponding to the program in the Library/documentation path, this path is not read and write permissions, so even if the file you are looking for does not exist, also do not see the automatic generation of files!

Therefore, the above should read:

-(nsstring*) DataFilePath

{

Nsarray * paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES);

NSString * documentsdirectory = [paths objectatindex:0];

NSString *path =[documentsdirectory stringbyappendingpathcomponent:@ "cataog.db"];

NSLog (@ "*****%@", Path);

return path;

}

That would be all right!

The following is a discussion of the operation of the file:

For the operation of the file, the core function is: Nssearchpathfordirectoriesindomains

IOS See development is developed in the sandbox, some parts of the file read and write restrictions, only a few directories to read and write files:

(1) Documents: The user data in the app can be stored here, and this directory will be included when the itunes data is backed up and restored

(2) TMP: Store temporary files, itunes will not back up and restore this directory, the files in this directory may be deleted after the app exits

(3) Library/caches: Store cache files, itunes does not back up this directory, files in this directory will no longer be applied to delete the operation of the file

two using Nssearchpathfordirectoriesindomains to create files and write data to the inside

Nsarray * paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES);

NSString * documentsdirectory = [paths objectatindex:0];

NSString *content[email protected]"write data into MyFile";

NSData *contentdata=[content datausingencoding:nsasciistringencoding];

If ([contentdata writetofile:fileName atomically:YES]) {
NSLog(@">>write OK.") );
}

If you want to write text inside the file, you need to change the encoding method:

NSString *contentchinese[email protected]"write Chinese character information to file" ;

NSData *contentchinesedata=[contentchinese datausingencoding: Nsunicodestringencoding];

If ([contentdata writetofile:fileName atomically:YES]) {
NSLog(@">> write succeeded.") );

}

If you want to specify a different file directory, such as the caches directory, you need to change the directory constant: nscachesdirectory

List for everyone:

Constant Directory
NSDocumentDirectory <application_home>/documents
Nscachesdirectory <application_home>/library/caches Nsapplicationsupportdirectory <application_home>/library/applicationsupport

three In addition, we also need to use some resource files in the development:

NSString*Path=[[NSBundleMainbundle]
Pathforresource:@"F"
OfType: @"TXT"];
NSString *Content=[nsstring stringwithcontentsoffile:Path encoding: Nsutf8stringencoding error:nil];
NSLog(@"path:%@ \nfile content:%@",path,content );

IOS about file operations

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.