直接上代码:
NSData *data = [[NSString stringWithFormat:@"%@\n", self.textField.text] dataUsingEncoding:NSUTF8StringEncoding]; NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; NSString *filePath = [document stringByAppendingPathComponent:@"file.txt"]; NSLog(@"file path %@", filePath); NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:filePath]; if (!fileHandle) { NSLog(@"no file handle!"); [self.textField.text writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil]; } [fileHandle seekToEndOfFile]; [fileHandle writeData:data]; [fileHandle closeFile];