iOS detects whether a file with a specified path exists

Source: Internet
Author: User
Tags bool create directory

This article to share is in the development of iOS to detect the existence of a specified file method, for everyone summed up 4 kinds, very practical, small partners according to their own needs free choice.

The code is as follows:

-(NSString *) DataPath: (NSString *) file

{

NSString *path = [[Nshomedirectory () stringbyappendingpathcomponent:@ "Documents"] stringbyappendingpathcomponent:@ " Badge "];

BOOL bo = [[Nsfilemanager Defaultmanager] Createdirectoryatpath:path withintermediatedirectories:yes attributes:nil Error:nil];

Nsassert (bo,@ "Create directory Failed");

NSString *result = [path stringbyappendingpathcomponent:file];

return result;

}

-(void) viewdidload

{

[Super Viewdidload];

The picture access path is first specified (written in the application sandbox by default)

Nsarray *paths = Nssearchpathfordirectoriesindomains (Nsdocumentdirectory,nsuserdomainmask, YES);

and give the file a filename

NSString *imagedir = [[[Paths objectatindex:0] stringbyappendingpathcomponent:@ "163"] stringbyappendingpathcomponent : @ "Songzi"];

folder where pictures are stored

NSString *imagepath =[imagedir stringbyappendingpathcomponent:@ "filename. png"];

NSData *data = nil;

Check to see if the picture has been saved locally

if ([self isexistsfile:imagepath]) {

Data=[nsdata Datawithcontentsoffile:imagepath];

}else{

data = [NSData datawithcontentsofurl:[nsurl urlwithstring: @ "url"]];

Create a folder path

[[Nsfilemanager Defaultmanager] Createdirectoryatpath:imagedir withintermediatedirectories:yes attributes:nil Error : nil];

Create a picture

[Uiimagepngrepresentation ([UIImage imagewithdata:data]) Writetofile:imagepath Atomically:yes];

}

Imageview.image = [UIImage imagewithdata:data];

}

Check to see if a file exists

The code is as follows:

NSString *path = [[NSBundle mainbundle] Pathforresource:filename oftype:@ "];

if (path==null)

Method Two:

The code is as follows:

Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager];

Get Documents Directory

Nsarray *directorypaths = Nssearchpathfordirectoriesindomains

(NSDocumentDirectory, Nsuserdomainmask, YES);

NSString *documentsdirectorypath = [Directorypaths objectatindex:0];

if ([FileManager fileexistsatpath:@ ""]==yes) {

NSLog (@ "File exists");

}

Method Three:

The code is as follows:

To determine whether a file exists

if (![ C judgefileexist:@ "User.plist"])

{

NSLog (Please confirm the file exists!);

Return

}

Method Four:

The code is as follows:

To determine whether a file exists

-(BOOL) Judgefileexist: (NSString *) fileName

{

Get file path

NSString *path = [[NSBundle mainbundle] Pathforresource:filename oftype:@ "];

if (path==null)

return NO;

Returnyes;

}

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.