无法写入文本文件
我需要将字符串写入文件。为此,我的代码是:
-(void)writeToFile:(NSString *)fileName: (NSString *)data {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// the path to write file
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];
[data writeToFile:appFile atomically:YES];
}
我像这样调用这个方法
ownServices *obj = [[ownServices alloc]init];
[obj writeToFile:@"iphone.txt" :@"this is mahesh babu"];
,但它没有写入文本文件。
原因是什么?任何人都可以帮助我吗?
提前谢谢你。
I need to write a string into a file. For that, my code is:
-(void)writeToFile:(NSString *)fileName: (NSString *)data {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// the path to write file
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];
[data writeToFile:appFile atomically:YES];
}
I am calling this method like this
ownServices *obj = [[ownServices alloc]init];
[obj writeToFile:@"iphone.txt" :@"this is mahesh babu"];
but it didn't write into the text file.
What's the reason? Can anyone please help me.
Thank u in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最有可能的问题是文档目录不存在。如果不存在,则创建它,然后写入它:
更简单的是使用最新的 API,如果该目录尚不存在,它将为您创建该目录:
The most likely problem is that the documents directory does not exist. Create it if it doesn't, then write to it:
Even simpler would be to use the latest API, which will create the directory for you if it doesn't already exist:
首先,你调用你的方法很奇怪。将方法重命名为
并像这样调用它:
其次,不推荐使用
writeToFile:atomically:
,使用writeToFile:atomically:encoding:error:
:这样,您还可以看到什么错误是。
Firstly, you are calling your method strangely. Rename the method to
and call it like so:
Secondly,
writeToFile:atomically:
is deprecated, usewriteToFile:atomically:encoding:error:
:This way, you also see what the error is.
你的代码看起来没问题。使用调试器(或
NSLog
语句)验证data
和appFile
的值。如果data
为nil
,则不会发生任何事情(包括没有错误),因为向nil
发送消息是无操作。也有可能appFile
不是您认为的路径。检查您尝试写入的目录的权限 (
ls -la
)。在设备上不能,但在模拟器上可以。它对您来说是只读的吗?它是否属于其他用户?假设这不是问题,请尝试使用
atomically:NO
进行调用。原子文件写入是通过写入一个文件,然后重命名它以替换旧文件来执行的。如果问题确实存在,那么就会隔离问题。Bonus Style Critique
OwnServices
而不是ownServices
尽管writeToFile:string:
这样的名称会更好。NSData
以外的实例,请勿将其命名为data
。这很令人困惑,除了“数据”之外,您几乎可以使用一个更好(更具体)的词。Your code looks OK. Use the debugger (or an
NSLog
statement) to verify the values ofdata
andappFile
. Ifdata
isnil
, nothing will happen (including no errors) because sending a message tonil
is a no-op. It's also possible thatappFile
is not the path you think it is.Check the permissions of the directory you are trying to write to (
ls -la
). On the device you can't, but on the simulator you can. Is it read-only for you? Is it owned by another user?Assuming that isn't the problem, try calling with
atomically:NO
. Atomic file writing is performed by writing a file, then renaming it to replace the old one. If the problem is there, that will isolate the problem.Bonus Style Critique
OwnServices
instead ofownServices
writeToFile:string:
would be better.data
if it is meant to point to an instance of something other thanNSData
. It's confusing, and there's almost a better (more specific) word you can use beside "data".