iPhone:从文档目录文件夹复制或移动文件 [英] iphone: copy or move file from document directory folder
问题描述
这是我的代码.
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无法执行该操作 完成.(可可箭头516.)" userInfo = 0x681abf0
Error Domain=NSCocoaErrorDominCode=516 "The operation couldn't be completed .(Cocoa arrow 516.)" userInfo=0x681abf0
NSUnderlyingError = 0x681b920该操作无法完成 .文件存在"
NSUnderlyingError =0x681b920 "The operation couldn't be completed .File exists"
abc文件夹中没有歌曲名称"sg.mp3",但我仍然收到文件存在错误.我不知道我在哪里弄错了?
abc folder there is no song name "sg.mp3" but I'm still getting the file exists error. I don't know where I did mistake?
推荐答案
您的代码中存在两个问题:
There is two issues in your code:
- 如果文件已经存在,您需要将其删除
- 您需要为目标文件指定一个名称,这意味着如果您使用类似的话:
NSString *toPath = [[pathSong objectAtIndex:0] stringByAppendingPathComponent:@"Songs"];
和
[[NSFileManager defaultManager]copyItemAtPath:strdestination toPath:toPath error:&Error];
然后,如果发生复制,它将把Sg.mp3文件复制为没有任何类型的歌曲.
Then if copy occurs, it will copy the Sg.mp3 file as Songs without any type.
所以您需要这样写:
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.mp3
从abc
文件夹复制到名称为yourFileName.mp3
This code will remove the file if exist at destination and then copy the sg.mp3
from abc
folder to Songs
folder with the name yourFileName.mp3
这篇关于iPhone:从文档目录文件夹复制或移动文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!