NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath;
if (str_FolderName!=nil){
filePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@/%@",str_FolderName,str_FileName]];
} else {
filePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@",str_FileName]];
}
NSFileManager *fileManager = [NSFileManager defaultManager];
NSMutableDictionary *data;
if ([fileManager fileExistsAtPath: filePath])
{
//file is exist
data = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
} else {
BOOL Success = [fileManager createFileAtPath:filePath contents:nil attributes:nil];
// sometime return YES, sometime return NO <<< this is my question
if(Success == YES){
data = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
}else{
data = [[NSMutableDictionary alloc] init];
}
}
for (uint32_t u32_Cnt=0;u32_Cnt<arr_Keys.count;u32_Cnt++) {
[data setValue:[NSString stringWithFormat:@"%@",[marr_Data objectAtIndex:u32_Cnt]] forKey:[arr_Keys objectAtIndex:u32_Cnt]];
}
if ([data writeToFile:filePath atomically: YES])
{
NSLog(@"[Save OK]");
return DEF_PASS;
} else {
NSLog(@"[Save Fail]"); <<<< now happened
}
如果我检查过文件不存在,那么我想创建文件作为文件路径
我的设备有时成功有时失败
文件路径如下:
@"/var/mobile/Containers/Data/Application/xxxxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx/Documents/mfolder/myfile"
有人可以帮助我吗?
我更新了你的一些代码。我为我正确分配了NSFilemanager及其工作。您可能在文件路径或文件管理器对象中遇到问题。
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath;
if (str_FolderName!=nil){
filePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@/%@",str_FolderName,str_FileName]];
}else{
filePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@",str_FileName]];
}
NSFileManager *fileManager = [NSFileManager defaultManager];
NSMutableDictionary *data;
if ([fileManager fileExistsAtPath: filePath])
{
//file is exist
data = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
}
else
{
if (str_FolderName!=nil){
NSString *strDicPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@",str_FolderName]];
// You need to create director before creating file.
if (![[NSFileManager defaultManager] fileExistsAtPath:strDicPath])
{
NSError *error;
[[NSFileManager defaultManager] createDirectoryAtPath:strDicPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder
}
}
BOOL Success = [fileManager createFileAtPath:filePath contents:nil attributes:nil];
// sometime return YES, sometime return NO <<< this is my question
if(Success == YES){
data = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
}else{
data = [[NSMutableDictionary alloc] init];
}
}
我还在模拟器和设备中检查了这段代码。
将字符串写入文件:
NSError *error;
NSString *stringToWrite = @"1\n2\n3\n4";
[stringToWrite writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句