将数据写入文件

发布于 2024-12-11 17:16:33 字数 402 浏览 0 评论 0原文

我正在尝试使用我的应用程序中发生的操作创建一个 txt 日志文件。我想在应用程序连接到服务器或显示新信息时保存一些文本。

那么,如何写入文件呢?我尝试使用方法 writeToFile: 但它不起作用,因为 fileExistsAtPath: 返回 NO。

我的代码是:

NSString *file_path = @"mylog.txt"; 
NSString *log = @"Hello World!!\n"; 
[log writeToFile:file_path atomically:YES encoding:NSUnicodeStringEncoding error:nil]; 

谢谢!

PS:哦,插入 iPhone 后可以通过 Organizer 读取吗?

I'm trying to make a txt log file with the actions that happens in my app. I want to save some text whenever the app is connecting to the server o displaying new info.

Well, how do I write to a file? I've tried using the method writeToFile: but it's not working because fileExistsAtPath: is returning NO.

My code is:

NSString *file_path = @"mylog.txt"; 
NSString *log = @"Hello World!!\n"; 
[log writeToFile:file_path atomically:YES encoding:NSUnicodeStringEncoding error:nil]; 

Thanks!

PS: Oh, would it be readable through Organizer with the iPhone plugged in?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

∞觅青森が 2024-12-18 17:16:33

您应该使用 < App_Home >/Documents 文件夹用于存储文档

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *file_Path = [documentsDirectory stringByAppendingPathComponent:@"log.txt"];    
[log writeToFile:file_path atomically:YES encoding:NSUnicodeStringEncoding error:nil];

但通常如果您想在运行应用程序时查看和转储内容,
您可以使用 NSLog() 来将其输出到控制台中。

You should use the < App_Home >/Documents folder for storing Documents

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *file_Path = [documentsDirectory stringByAppendingPathComponent:@"log.txt"];    
[log writeToFile:file_path atomically:YES encoding:NSUnicodeStringEncoding error:nil];

But normally if you want to see and dump things while running the app,
you could just use NSLog() which outputs it in the console.

梦幻的心爱 2024-12-18 17:16:33

尝试我写的这个方法:

+(NSString*)createPath:(NSString*)fileName{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *localizedPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",fileName]];

    //NSLog(@"%@",localizedPath);
    return localizedPath;
}

它将返回您的文件的路径。您只需给它一个名称。

Try this method I wrote:

+(NSString*)createPath:(NSString*)fileName{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *localizedPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",fileName]];

    //NSLog(@"%@",localizedPath);
    return localizedPath;
}

It will return you a path for your file. You only need to give it a name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文