如何以附加模式写入文本文件

发布于 2024-10-13 18:27:40 字数 837 浏览 3 评论 0原文

我的应用程序基于导航。 UItextView 用于注释 UIViewController。我正在将文本数据写入文件。现在我需要在附加模式下编写,我正在尝试以下代码,但每次都使用相同的文本数据写入两次,并且不附加下一个文本数据到文件。

- (void)saveText:(id)sender
{
    [self.textview resignFirstResponder];
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [documentPaths objectAtIndex:0];
    NSString *documentTXTPath = [documentsDirectory stringByAppendingPathComponent:@"Notes.txt"];
    NSString *savedString = textview.text;
    [savedString writeToFile:documentTXTPath atomically:YES];


    NSFileHandle *myHandle = [NSFileHandle fileHandleForUpdatingAtPath:documentTXTPath ];
    [myHandle seekToEndOfFile];
    [myHandle writeData:  [savedString dataUsingEncoding:NSUTF8StringEncoding]];
    [myHandle closeFile];

}

My application navigation based. UItextView for notes UIViewController. I am writing data for text to file. Now i need to write in append mode , the below code i am trying but every time is writing to two time with same text data, and not appending if next text data to file.

- (void)saveText:(id)sender
{
    [self.textview resignFirstResponder];
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [documentPaths objectAtIndex:0];
    NSString *documentTXTPath = [documentsDirectory stringByAppendingPathComponent:@"Notes.txt"];
    NSString *savedString = textview.text;
    [savedString writeToFile:documentTXTPath atomically:YES];


    NSFileHandle *myHandle = [NSFileHandle fileHandleForUpdatingAtPath:documentTXTPath ];
    [myHandle seekToEndOfFile];
    [myHandle writeData:  [savedString dataUsingEncoding:NSUTF8StringEncoding]];
    [myHandle closeFile];

}

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

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

发布评论

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

评论(3

始终不够爱げ你 2024-10-20 18:27:41

删除下面这行代码

[savedString writeToFile:documentTXTPath atomically:YES];

,剩下的代码就都没有问题了。无论如何也检查我的代码,

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *documentTXTPath = [documentsDirectory stringByAppendingPathComponent:@"Notes.txt"];
    NSString *savedString = textview.text;
    NSFileHandle *myHandle = [NSFileHandle fileHandleForWritingAtPath:documentTXTPath];
    [myHandle seekToEndOfFile];
    [myHandle writeData:[savedString dataUsingEncoding:NSUTF8StringEncoding]];

Remove the following line of code

[savedString writeToFile:documentTXTPath atomically:YES];

And remaining code are all fine. Anyway check my code also,

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *documentTXTPath = [documentsDirectory stringByAppendingPathComponent:@"Notes.txt"];
    NSString *savedString = textview.text;
    NSFileHandle *myHandle = [NSFileHandle fileHandleForWritingAtPath:documentTXTPath];
    [myHandle seekToEndOfFile];
    [myHandle writeData:[savedString dataUsingEncoding:NSUTF8StringEncoding]];
影子是时光的心 2024-10-20 18:27:41

尝试一下它效果很好

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *documentTXTPath = [documentsDirectory stringByAppendingPathComponent:@"Notes.txt"];
NSString *savedString = textview.text;

如果文件不存在创建它并写入文件,否则它已经创建附加到文件末尾

NSFileManager *fileManager = [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath:documentTXTPath])
{
  [savedString writeToFile:documentTXTPath atomically:YES];
}
else
{
  NSFileHandle *myHandle = [NSFileHandle fileHandleForWritingAtPath:documentTXTPath];
  [myHandle seekToEndOfFile];
  [myHandle writeData:[savedString dataUsingEncoding:NSUTF8StringEncoding]];
}

Try this it works great

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *documentTXTPath = [documentsDirectory stringByAppendingPathComponent:@"Notes.txt"];
NSString *savedString = textview.text;

If file Does not exist create it and write to file or else its already created append to end of the file

NSFileManager *fileManager = [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath:documentTXTPath])
{
  [savedString writeToFile:documentTXTPath atomically:YES];
}
else
{
  NSFileHandle *myHandle = [NSFileHandle fileHandleForWritingAtPath:documentTXTPath];
  [myHandle seekToEndOfFile];
  [myHandle writeData:[savedString dataUsingEncoding:NSUTF8StringEncoding]];
}
初熏 2024-10-20 18:27:41

您还可以

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *documentTXTPath = [documentsDirectory stringByAppendingPathComponent:@"Notes.text"];
NSString *content = [NSString stringWithContentsOfFile:documentTXTPath encoding:NSUTF8StringEncoding error:nil];
content = [content stringByAppendingString:data];

再次将内容写入特定文件路径

[content writeToFile:documentTXTPath atomically:YES encoding:NSUTF8StringEncoding error:nil];

You can also do

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *documentTXTPath = [documentsDirectory stringByAppendingPathComponent:@"Notes.text"];
NSString *content = [NSString stringWithContentsOfFile:documentTXTPath encoding:NSUTF8StringEncoding error:nil];
content = [content stringByAppendingString:data];

And again write contents to particular file path

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