//创建文件 NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"xxx.txt"]; NSLog(@"-----%@",filePath); if(![fileManager fileExistsAtPath:filePath]) //如果文件xxx不存在 { NSLog(@"xxx.txt is not exist"); if( [fileManager createFileAtPath:filePath contents:nil attributes:nil]) //创建文件 { // } } else { // } //数据写入文件 NSString *buf=@"数据"; [buf writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil]; //从文件读数据 NSString * buf = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];