[英]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.