今天小編跟大家分享一下ios下移動文件方法有哪些?,感興趣的朋友跟小編一起來了解一下吧!
這段objective c代碼用于移動指定路徑下的文件
?
if ([fileManager copyItemAtPath:@"FilePath1"
? toPath:@"FilePath2"? error:NULL]) {
???? NSLog(@"Copied successfully");
? }
?
方法二:
使用 NSFileManager:
讓您的文檔的路徑和您的緩存路徑。遍歷所有的文件,并將它們移動使用 NSFileManager
?
- (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);
??????? }
??? }
}
?
方法三:
FCFileManager 是一個構建在 NSFileManager 之上的 iOS 文件管理工具,簡化了文件管理。它提供了許多靜態方法,用于執行最常用的操作用幾行代碼。它的工作原理是默認的文件目錄,允許使用相對路徑,但它可以在任何其他目錄中輕松工作。
Move file:
?
[FCFileManager moveItemAtPath:@"test.txt" toPath:@"tests/test.txt"];
?
Remove file:
?
//remove file at the specified path
[FCFileManager removeItemAtPath:@"test.txt"];
?
以上所述上就是本文的全部內容了,希望大家能夠喜歡。
以上所述就是ios下移動文件方法有哪些的介紹,希望大家能夠喜歡,更多內容請繼續關注武林技術頻道網站!