iphone:从文档目录文件夹中复制或移动文件

这是我的代码。

NSArray *pathSong = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *toPath = [[pathSong objectAtIndex:0] stringByAppendingPathComponent:@"Songs"]; NSString *fromPath=[[pathSong objectAtIndex:0] stringByAppendingPathComponent:@"abc"]; NSString *strdestination = [fromPath stringByAppendingPathComponent:@"sg.mp3"]; NSError *Error; if([[NSFileManager defaultManager]fileExistsAtPath:strdestination]){ if([[NSFileManager defaultManager]copyItemAtPath:strdestination toPath:toPath error:&Error]==NO){ UIAlertView *Alert=[[UIAlertView alloc]initWithTitle:@"copy" message:[NSString stringWithFormat:@"%@",Error] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; [Alert show]; } else{ UIAlertView *Alert=[[UIAlertView alloc]initWithTitle:@"Not copy" message:[NSString stringWithFormat:@"%@",Error] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; [Alert show]; } } 

我收到错误徽标:

Error Domain = NSCocoaErrorDominCode = 516“操作无法完成。(Cocoa arrow 516.)”userInfo = 0x681abf0

NSUnderlyingError = 0x681b920“操作无法完成.File exists”

abc文件夹没有歌曲名称“sg.mp3”,但我仍然得到文件存在错误。 我不知道我错在哪里?

您的代码中存在两个问题:

  1. 如果文件已存在,则需要删除该文件
  2. 您需要指定目标文件的名称,意味着如果您使用如下:

NSString *toPath = [[pathSong objectAtIndex:0] stringByAppendingPathComponent:@"Songs"];

 [[NSFileManager defaultManager]copyItemAtPath:strdestination toPath:toPath error:&Error]; 

然后,如果发生复制,它将Sg.mp3文件复制为没有任何类型的歌曲。

所以你需要写它:

 NSArray *pathSong = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *tempPath = [[pathSong objectAtIndex:0] stringByAppendingPathComponent:@"Songs"]; NSString *toPath = [tempPath stringByAppendingPathComponent:@"yourFileName.mp3"]; NSString *fromPath = [[pathSong objectAtIndex:0] stringByAppendingPathComponent:@"abc"]; NSString *strdestination = [fromPath stringByAppendingPathComponent:@"sg.mp3"]; NSError *Error = nil; if([[NSFileManager defaultManager]fileExistsAtPath:strdestination]) { if([[NSFileManager defaultManager]copyItemAtPath:strdestination toPath:toPath error:&Error]==NO) { UIAlertView *Alert=[[UIAlertView alloc]initWithTitle:@"copy" message:[NSString stringWithFormat:@"%@",Error] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; [Alert show]; } else { [fileManager removeItemAtPath:strdestination error:NULL]; UIAlertView *Alert=[[UIAlertView alloc]initWithTitle:@"Not copy" message:[NSString stringWithFormat:@"%@",Error] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; [Alert show]; } } 

如果存在于目的地,此代码将删除该文件,然后将sg.mp3abc文件夹复制到名为yourFileName.mp3 Songs文件夹中

根据Midhun MP的回答,这是一个帮手

 BOOL moveFile(NSString *srcPath, NSString *dstPath) { NSLog(@"moving %@ -> %@", srcPath, dstPath); NSFileManager *fm = [NSFileManager defaultManager]; if ([fm fileExistsAtPath:dstPath]) { // in my usecase this is a hard error, bolt to prevent overwriting return NO; } if ([fm fileExistsAtPath:srcPath]) { NSError *error = nil; NSString *destDir = [dstPath stringByDeletingLastPathComponent]; [fm createDirectoryAtPath:destDir withIntermediateDirectories:YES attributes:nil error:nil]; if ([[NSFileManager defaultManager] copyItemAtPath:srcPath toPath:dstPath error:&error]==NO) { NSLog(@"failure declassing %@", srcPath); return NO; } else { [fm removeItemAtPath:srcPath error:NULL]; // gr8t success return YES; } } return NO; } 

我认为这是因为你试图用你的副本覆盖一个文件。

检查您的权限掩码,尝试使用缓存而不是文档目录。

你的意思是if(!fileExistsAtPath)

您需要删除已存在的文件:

 NSArray *pathSong = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *toPath = [[pathSong objectAtIndex:0] stringByAppendingPathComponent:@"Songs"]; NSString *fromPath=[[pathSong objectAtIndex:0] stringByAppendingPathComponent:@"abc"]; NSString *strdestination = [fromPath stringByAppendingPathComponent:@"sg.mp3"]; NSError *Error; //DELETE THE FILE AT THE LOCATION YOU'RE COPYING TO NSFileManager *fileManager = [NSFileManager defaultManager]; [fileManager removeItemAtPath:strdestination error:NULL]; if([[NSFileManager defaultManager]copyItemAtPath:strdestination toPath:toPath error:&Error]==NO){ UIAlertView *Alert=[[UIAlertView alloc]initWithTitle:@"copy" message:[NSString stringWithFormat:@"%@",Error] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; [Alert show]; } else{ UIAlertView *Alert=[[UIAlertView alloc]initWithTitle:@"Not copy" message:[NSString stringWithFormat:@"%@",Error] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; [Alert show]; }