如何将文件放到沙盒目录下面
1个回答
推荐于2016-05-27 · 知道合伙人数码行家
huanglenzhi
知道合伙人数码行家
向TA提问 私信TA
知道合伙人数码行家
采纳数:117538
获赞数:517192
长期从事计算机组装,维护,网络组建及管理。对计算机硬件、操作系统安装、典型网络设备具有详细认知。
向TA提问 私信TA
关注
展开全部
IOS 从Resource文件夹下Copy文件到沙盒
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = @"拷贝文件到Sandbox";
//文件类型
NSString * docPath = [[NSBundle mainBundle] pathForResource:@"save1" ofType:@"dat"];
// 沙盒Documents目录
// NSString * appDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
// 沙盒Library目录
NSString * appDir = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
//appLib Library/Caches目录
NSString *appLib = [appDir stringByAppendingString:@"/Caches"];
BOOL filesPresent = [self copyMissingFile:docPath toPath:appLib];
if (filesPresent) {
NSLog(@"OK");
}
else
{
NSLog(@"NO");
}
// 创建文件夹
NSString *createDir = [NSHomeDirectory() stringByAppendingString:@"/test"];
[self createFolder:createDir];
// 把文件拷贝到Test目录
BOOL filesPresent1 = [self copyMissingFile:docPath toPath:createDir];
if (filesPresent1) {
NSLog(@"OK");
}
else
{
NSLog(@"NO");
}
}
/**
* @brief 把Resource文件夹下的save1.dat拷贝到沙盒
*
* @param sourcePath Resource文件路径
* @param toPath 把文件拷贝到XXX文件夹
*
* @return BOOL
*/
- (BOOL)copyMissingFile:(NSString *)sourcePath toPath:(NSString *)toPath
{
BOOL retVal = YES; // If the file already exists, we'll return success…
NSString * finalLocation = [toPath stringByAppendingPathComponent:[sourcePath lastPathComponent]];
if (![[NSFileManager defaultManager] fileExistsAtPath:finalLocation])
{
retVal = [[NSFileManager defaultManager] copyItemAtPath:sourcePath toPath:finalLocation error:NULL];
}
return retVal;
}
/**
* @brief 创建文件夹
*
* @param createDir 创建文件夹路径
*/
- (void)createFolder:(NSString *)createDir
{
BOOL isDir = NO;
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL existed = [fileManager fileExistsAtPath:createDir isDirectory:&isDir];
if ( !(isDir == YES && existed == YES) )
{
[fileManager createDirectoryAtPath:createDir withIntermediateDirectories:YES attributes:nil error:nil];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = @"拷贝文件到Sandbox";
//文件类型
NSString * docPath = [[NSBundle mainBundle] pathForResource:@"save1" ofType:@"dat"];
// 沙盒Documents目录
// NSString * appDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
// 沙盒Library目录
NSString * appDir = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
//appLib Library/Caches目录
NSString *appLib = [appDir stringByAppendingString:@"/Caches"];
BOOL filesPresent = [self copyMissingFile:docPath toPath:appLib];
if (filesPresent) {
NSLog(@"OK");
}
else
{
NSLog(@"NO");
}
// 创建文件夹
NSString *createDir = [NSHomeDirectory() stringByAppendingString:@"/test"];
[self createFolder:createDir];
// 把文件拷贝到Test目录
BOOL filesPresent1 = [self copyMissingFile:docPath toPath:createDir];
if (filesPresent1) {
NSLog(@"OK");
}
else
{
NSLog(@"NO");
}
}
/**
* @brief 把Resource文件夹下的save1.dat拷贝到沙盒
*
* @param sourcePath Resource文件路径
* @param toPath 把文件拷贝到XXX文件夹
*
* @return BOOL
*/
- (BOOL)copyMissingFile:(NSString *)sourcePath toPath:(NSString *)toPath
{
BOOL retVal = YES; // If the file already exists, we'll return success…
NSString * finalLocation = [toPath stringByAppendingPathComponent:[sourcePath lastPathComponent]];
if (![[NSFileManager defaultManager] fileExistsAtPath:finalLocation])
{
retVal = [[NSFileManager defaultManager] copyItemAtPath:sourcePath toPath:finalLocation error:NULL];
}
return retVal;
}
/**
* @brief 创建文件夹
*
* @param createDir 创建文件夹路径
*/
- (void)createFolder:(NSString *)createDir
{
BOOL isDir = NO;
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL existed = [fileManager fileExistsAtPath:createDir isDirectory:&isDir];
if ( !(isDir == YES && existed == YES) )
{
[fileManager createDirectoryAtPath:createDir withIntermediateDirectories:YES attributes:nil error:nil];
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询