附加到用的NSMutableString一个文件结束 [英] Appending to the end of a file with NSMutableString

查看:196
本文介绍了附加到用的NSMutableString一个文件结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我尝试将数据追加到年底的日志文件。我有一个的NSMutableString textToWrite 变量,和我做以下内容:

I have a log file that I'm trying to append data to the end of. I have an NSMutableString textToWrite variable, and I am doing the following:

[textToWrite writeToFile:filepath atomically:YES 
                                    encoding: NSUnicodeStringEncoding error:&err];

然而,当我做这一切的文件中的文本替换在textToWrite文本。我该如何,而不是附加到文件的结尾? (甚至更好,我怎么能追加到一个新行的文件结尾?)

However, when I do this all the text inside the file is replaced with the text in textToWrite. How can I instead append to the end of the file? (Or even better, how can I append to the end of the file on a new line?)

推荐答案

我想你可以做两件事情:

I guess you could do a couple of things:

NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:aPath];
[fileHandle seekToEndOfFile];
[fileHandle writeData:[textToWrite dataUsingEncoding:NSUTF8StringEncoding]];
[fileHandle closeFile];

请注意,这将NSData的追加到您的文件 - 不是一个NSString。请注意,如果您使用NSFileHandle,你必须确保该文件前手存在。 fileHandleForWritingAtPath 将返回nil,如果不存在文件的路径。请参阅<一个href=\"https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsfilehandle_Class/Reference/Reference.html\">NSFileHandle类引用。

Note that this will append NSData to your file -- NOT an NSString. Note that if you use NSFileHandle, you must make sure that the file exists before hand. fileHandleForWritingAtPath will return nil if no file exists at the path. See the NSFileHandle class reference.

或者,你可以这样做:

NSString *contents = [NSString stringWithContentsOfFile:filepath];
contents = [contents stringByAppendingString:textToWrite];
[contents writeToFile:filepath atomically:YES encoding: NSUnicodeStringEncoding error:&err];

相信第一种方法是最有效的,因为第二方法涉及写入新内容文件之前读取文件的内容到一个NSString。但是,如果你不想让你的文件包含的NSData和preFER保持它的文本,第二个选项会更适合你。

I believe the first approach would be the most efficient, since the second approach involves reading the contents of the file into an NSString before writing the new contents to the file. But, if you do not want your file to contain NSData and prefer to keep it text, the second option will be more suitable for you.

[更新]
由于stringWithContentsOfFile是<一个href=\"https://developer.apple.com/library/mac/documentation/cocoa/reference/foundation/classes/NSString_Class/De$p$pcationAppendix/AppendixADe$p$pcatedAPI.html\">de$p$pcated您可以修改第二种方式:

[Update] Since stringWithContentsOfFile is deprecated you can modify second approach:

NSError* error = nil;
NSString* contents = [NSString stringWithContentsOfFile:filepath
                                               encoding:NSUTF8StringEncoding
                                                  error:&error];
if(error) { // If error object was instantiated, handle it.
    NSLog(@"ERROR while loading from file: %@", error);
    // …
}
[contents writeToFile:filepath atomically:YES
                                 encoding:NSUnicodeStringEncoding
                                    error:&err];

<一个href=\"http://stackoverflow.com/questions/2954892/if-nsstring-stringwithcontentsoffile-is-de$p$pcated-what-is-its-replacement\">See在计算器

这篇关于附加到用的NSMutableString一个文件结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆