IOS checks whether the file in the specified path exists.

Source: Internet
Author: User

IOS checks whether the file in the specified path exists.

This article will share with you how to check whether a specified file exists in IOS development. Four methods are provided for you, which are very practical. You can choose your preferred method based on your needs.

The Code is as follows:

-(NSString *) dataPath :( NSString *) file

{

NSString * path = [[NSHomeDirectory () stringByAppendingPathComponent: @ "events"] stringByAppendingPathComponent: @ "badge"];

BOOL bo = [[NSFileManager defaultManager] createDirectoryAtPath: path withIntermediateDirectories: YES attributes: nil error: nil];

NSAssert (bo, @ "failed to create directory ");

NSString * result = [path stringByAppendingPathComponent: file];

Return result;

}

-(Void) viewDidLoad

{

[Super viewDidLoad];

// Specify the image access path (written to the application sandbox by default)

NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES );

// Give the file a file name

NSString * imageDir = [[[paths objectAtIndex: 0] stringByAppendingPathComponent: @ "163"] stringByAppendingPathComponent: @ "songzi"];

// Folder for storing images

NSString * imagePath = [imageDir stringByAppendingPathComponent: @ "file name .png"];

NSData * data = nil;

// Check whether the image is saved locally

If ([self isExistsFile: imagePath]) {

Data = [NSData dataWithContentsOfFile: imagePath];

} Else {

Data = [NSData dataWithContentsOfURL: [NSURL URLWithString: @ ""];

// Create a folder path

[[NSFileManager defaultManager] createDirectoryAtPath: imageDir withIntermediateDirectories: YES attributes: nil error: nil];

// Create an image

[UIImagePNGRepresentation ([UIImage imageWithData: data]) writeToFile: imagePath atomically: YES];

}

ImageView. image = [UIImage imageWithData: data];

}

Check whether the file exists

The Code is as follows:

NSString * path = [[NSBundle mainBundle] pathForResource: fileName ofType: @ ""];

If (path = NULL)

Method 2:

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 3:

The Code is as follows:

// Determine whether a file exists

If (! [C judgeFileExist: @ "user. plist"])

{

NSLog (@ "check whether the file exists! ");

Return;

}

Method 4:

The Code is as follows:

// Determine whether a file exists

-(BOOL) judgeFileExist :( NSString *) fileName

{

// Obtain the 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.