IOS写文件时需要注意得小地方

本文介绍了一个在iOS 6.0系统中解决文件写入问题的方法。通过更改当前目录路径到目标文件所在目录,并确保文件路径正确设置,可以避免文件写入失败的情况。文中提供了一个具体的Objective-C实现示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

系统版本为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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值