Ios Mobile file method summary

Source: Internet
Author: User

Ios Mobile file method summary

This article mainly summarizes the mobile file methods in ios, from simple to complex, very practical. If you need it, you can refer to it.

This objective c code is used to move objects in a specified path.

The Code is as follows:

If ([fileManager copyItemAtPath: @ "FilePath1"

ToPath: @ "FilePath2" error: NULL]) {

NSLog (@ "Copied successfully ");

}

Method 2:

Use NSFileManager:

Make your document path and your cache path. Traverse all files and move them using NSFileManager

The Code is as follows:

-(Void) moveAllDocs {

NSFileManager * fileManager = [NSFileManager defaultManager];

NSError * error = nil;

NSString * sourceDirectory = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

NSString * destinationDirectory = [NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES) lastObject];

NSArray * contents = [fileManager contentsOfDirectoryAtPath: sourceDirectory error: & error];

For (NSString * sourceFileName in contents ){

NSString * sourceFile = [sourceDirectory stringByAppendingPathComponent: sourceFileName];

NSString * destFile = [destinationDirectory stringByAppendingPathComponent: sourceFileName];

If (! [FileManager moveItemAtPath: sourceFile toPath: destFile error: & error]) {

NSLog (@ "Error: % @", error );

}

}

}

Method 3:

FCFileManager is an iOS file management tool built on NSFileManager, which simplifies file management. It provides many static methods for executing the most common operations with several lines of code. Its working principle is the default file directory, which allows the use of relative paths, but it can work easily in any other directory.

Move file:

The Code is as follows:

[FCFileManager moveItemAtPath: @ "test.txt" toPath: @ "tests/test.txt"];

Remove file:

The Code is as follows:

// Remove file at the specified path

[FCFileManager removeItemAtPath: @ "test.txt"];

The above is all the content of this article. I hope you will like it.

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.