IOS寫文件時需要注意得小地方

系統版本爲ios 6.0下:如果碰到寫文件不成功的情況,可能會是以下問題引起的

+(NSString*)writeData:(id)data InFile:(NSString*)fileName

{
if (data == nil) {
return nil;
}

NSString *filePath = [[Utilities documentPath] stringByAppendingPathComponent:fileName];
NSFileManager *mgr = [NSFileManager defaultManager];
NSLog(@"curDictionary path :%@",[mgr currentDirectoryPath]);//"/"
[mgr changeCurrentDirectoryPath:[[Utilities documentPath] stringByExpandingTildeInPath]];//更改當前目錄爲待操作目錄下,沒有這一步無法寫文件
NSLog(@"curDictionary path :%@",[mgr currentDirectoryPath]);//"Documents"
if (![mgr fileExistsAtPath:filePath]) {
[mgr createFileAtPath:filePath contents:nil attributes:nil];
}else{
[mgr removeItemAtPath:filePath error:nil];
}
//[mutData appendBytes:&data length:sizeof(data)];
[data writeToFile:fileName atomically:YES];

NSLog(@"data :%@",data);
NSLog(@"filePath :%@",filePath);
return filePath;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章