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


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!);



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;



Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.