Adding string on existing txt file -iOS

后端 未结 1 1595
日久生厌
日久生厌 2021-01-15 21:01

As shown code below, It is outputting several individual .txt files. However, I am looking in a way to save everything into one txt file. How could I append new string at t

1条回答
  •  一整个雨季
    2021-01-15 21:39

    Ok, I think I got it.

    -(void)saveData:(NSString*)data
    {
        NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *documentDirectory=[paths objectAtIndex:0];
            NSString *fileName=[NSString stringWithFormat:@"%@/%d.txt",documentDirectory,1];
            NSString *content=data;
            NSFileHandle *fileHandler= [NSFileHandle fileHandleForWritingAtPath:fileName];
            [fileHandler seekToEndOfFile];
            [fileHandler writeData:[content dataUsingEncoding:NSUTF8StringEncoding]];
            [fileHandler closeFile];
            NSLog(@"%@",fileName);
    }
    

    0 讨论(0)
提交回复
热议问题