Using the below line,
使用以下行,
[fileManager copyItemAtPath:sourcePath toPath:targetPath error:&error];
We can copy a folder but if the folder already exists it throws an exception "File Exists". In order to overwrite a single file, we can achieve it through the following lines:
我们可以复制文件夹,但如果文件夹已经存在,则会抛出异常“文件存在”。为了覆盖单个文件,我们可以通过以下几行来实现:
NSData *myData = [NSData dataWithContentsOfURL:FileURL]; /fetch single file
[myData writeToFile:targetPath atomically:YES];
But I want to copy an already existing folder i.e, overwrite.
但我想复制一个已经存在的文件夹,即覆盖。
Edit : Simple Possibility , I can remove the items before copying them.
编辑:简单的可能性,我可以在复制之前删除这些项目。
Please suggest any more possibilities.
请提出更多可能性。
4 个解决方案
#1
10
The default behavior of NSFileManager
method is to throw an exception/error "File Exists." when the file exists. But still if you want to overwrite using NSFileManager
then it provides one api for that which is mentioned below replaceItemAtURL as well in first solution:-
NSFileManager方法的默认行为是抛出异常/错误“文件存在”。当文件存在时。但是如果你想使用NSFileManager覆盖它,那么它为第一个解决方案提供了一个api,在下面提到了replaceItemAtURL: -
Also there are three solutions to achieve that
还有三种解决方案可以实现这一目标
First Solution
第一解决方案
[filemanger replaceItemAtURL:url1
withItemAtURL:url2
backupItemName:@"/Users/XYZ/Desktop/test.xml"
options:NSFileManagerItemReplacementUsingNewMetadataOnly
resultingItemURL:nil error:nil];
Using the above API you can overwrite the file contents. But before that you have to take the backup of your source path in your temporary directory.
使用上述API,您可以覆盖文件内容。但在此之前,您必须在临时目录中备份源路径。
Second Solution
二解决方案
Already you have mentioned in your question using NSData writeToUrl
.
您已经在使用NSData writeToUrl的问题中提到过。
Third Solution
第三解决方案
*foe has mentioned in their answer. i.e. remove the item being overwritten beforehand.
特洛伊福福在答案中提到过。即删除事先被覆盖的项目。
#2
6
I would like to add one more using delegate, in order to override files with copyItemAtPath (NSFileManager) function use:
我想再使用委托添加一个,以便用copyItemAtPath(NSFileManager)函数覆盖文件使用:
[[NSFileManager defaultManager] setDelegate:self];
[[NSFileManager defaultManager] copyItemAtPath:fileOrigin toPath:fileDestin error:&error];
and implement the delegates optional function:
并实现委托可选功能:
- (BOOL)fileManager:(NSFileManager *)fileManager shouldProceedAfterError:(NSError *)error copyingItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath{
if ([error code] == NSFileWriteFileExistsError) //error code for: The operation couldn’t be completed. File exists
return YES;
else
return NO;
}
#3
3
Remove the item first, with:
首先删除项目,使用:
[fileManager removeItemAtPath:targetPath error:NULL];
(i.e. ignoring any error)
(即忽略任何错误)
#4
0
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])
[[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
#1
10
The default behavior of NSFileManager
method is to throw an exception/error "File Exists." when the file exists. But still if you want to overwrite using NSFileManager
then it provides one api for that which is mentioned below replaceItemAtURL as well in first solution:-
NSFileManager方法的默认行为是抛出异常/错误“文件存在”。当文件存在时。但是如果你想使用NSFileManager覆盖它,那么它为第一个解决方案提供了一个api,在下面提到了replaceItemAtURL: -
Also there are three solutions to achieve that
还有三种解决方案可以实现这一目标
First Solution
第一解决方案
[filemanger replaceItemAtURL:url1
withItemAtURL:url2
backupItemName:@"/Users/XYZ/Desktop/test.xml"
options:NSFileManagerItemReplacementUsingNewMetadataOnly
resultingItemURL:nil error:nil];
Using the above API you can overwrite the file contents. But before that you have to take the backup of your source path in your temporary directory.
使用上述API,您可以覆盖文件内容。但在此之前,您必须在临时目录中备份源路径。
Second Solution
二解决方案
Already you have mentioned in your question using NSData writeToUrl
.
您已经在使用NSData writeToUrl的问题中提到过。
Third Solution
第三解决方案
*foe has mentioned in their answer. i.e. remove the item being overwritten beforehand.
特洛伊福福在答案中提到过。即删除事先被覆盖的项目。
#2
6
I would like to add one more using delegate, in order to override files with copyItemAtPath (NSFileManager) function use:
我想再使用委托添加一个,以便用copyItemAtPath(NSFileManager)函数覆盖文件使用:
[[NSFileManager defaultManager] setDelegate:self];
[[NSFileManager defaultManager] copyItemAtPath:fileOrigin toPath:fileDestin error:&error];
and implement the delegates optional function:
并实现委托可选功能:
- (BOOL)fileManager:(NSFileManager *)fileManager shouldProceedAfterError:(NSError *)error copyingItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath{
if ([error code] == NSFileWriteFileExistsError) //error code for: The operation couldn’t be completed. File exists
return YES;
else
return NO;
}
#3
3
Remove the item first, with:
首先删除项目,使用:
[fileManager removeItemAtPath:targetPath error:NULL];
(i.e. ignoring any error)
(即忽略任何错误)
#4
0
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])
[[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];