简体   繁体   English

Obj-C,如何使用NSSearchPathForDirectoriesInDomains复制文件?

[英]Obj-C, how do I copy a file with NSSearchPathForDirectoriesInDomains?

I want to copy some files as part of a backup routine. 我想复制一些文件作为备份例程的一部分。

I have the following function which gives the location of my files. 我具有以下功能,该功能提供了文件的位置。

- (NSString *)getLocalDocumentPath:(NSString*)strFile {

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
           NSUserDomainMask, YES);
    NSString *path = [[paths objectAtIndex:0] 
           stringByAppendingPathComponent:strFile];
    return path;
}

Heres my database 这是我的数据库

    NSString *filePath = [self getLocalDocumentPath:@"mydatabase.db"];

I want to copy this to a new file called upload.txt 我想将此复制到一个名为upload.txt的新文件中

    NSString *filePath = [self getLocalDocumentPath:@"upload.txt"];

Then later on I download a file. 然后稍后下载文件。

    NSString *filePath = [self getLocalDocumentPath:@"download.txt"];

Which I want to copy on top of my original database file. 我想复制到我的原始数据库文件上。

    NSString *filePath = [self getLocalDocumentPath:@"mydatabase.db"];

Sounds like you want to get to know NSFileManager very well (documentation is linked for you). 听起来您想非常了解NSFileManager (为您链接了文档)。

The method you'd most likely be most interested in is: 您最可能感兴趣的方法是:

moveItemAtPath:toPath:error:

one method could be: 一种方法可以是:
you can read the file contents you want to save in NSData then can use: 您可以读取要保存在NSData的文件内容,然后可以使用:

- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile

in which you can replace the path with one your relative file paths you have mentioned for various files. 在其中,您可以将路径替换为您提到的各种文件的相对文件路径之一。

NSFileManager *fileManager = [NSFilemanager defaultManager];
[fileManager copyItemAtPath:databasePath toPath:uploadFilePath error:nil];

and if you want to overwrite the database using the download.txt, make an instance of NSData and use - (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile 如果要使用download.txt覆盖数据库,请创建一个NSData实例,并使用- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Obj-C,如何将iPhone模拟器转到横向? - Obj-C, how do I turn the iphone simulator to landscape? 如何在Obj-C中的标签上隐藏文本? - How do I hide the text on a label in Obj-C? 对象:参考或副本? - Obj-C: A reference or a copy? Obj-C,如何删除以前设置的本地推送通知? - Obj-C, how do I delete a local push notification set previously? 如何为视图中的多个UIScrollView对象(iPhoneSDK obj-C)进行缩放以选择正确的视图? - How do I choose the correct view for zooming with multiple UIScrollView objects in a view (iPhoneSDK obj-C)? Obj-C,如何在所有iPhone尺寸上缩放子视图的高度? - Obj-C, how do I get the height of a sub view to scale, across all iPhone sizes? Obj-C,如何处理包含4个部分的版本号?大于等 - Obj-C, How do I handle version number with 4 parts?, greater than etc Obj-C,iOS,如何按值而不是键排序,sortedArrayUsingSelector,目前@selector(比较:)] - Obj-C, iOS, How do I sort by value and not key, sortedArrayUsingSelector, currently @selector(compare:)] 在obj-c / iPhone SDK中,如何获取自称的方法? - In obj-c/iPhone SDK how do I get a mthod to call itself? Obj-C,使用[button setImage:image]; 如何使质量最大化? - Obj-C, Using [button setImage:image]; how do I maximize the quality?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM