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.