文件写到本地

-(void)createFiel{

//创建文件管理器

NSFileManager *fileManager = [NSFileManager defaultManager];

//获取document路径,括号中属性为当前应用程序独享

NSArray *directoryPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,      NSUserDomainMask, YES);

NSString *documentDirectory = [directoryPaths objectAtIndex:0];

//定义记录文件全名以及路径的字符串filePath

filePath = [documentDirectory stringByAppendingPathComponent:@"history.txt"];

//查找文件,如果不存在,就创建一个文件

if (![fileManager fileExistsAtPath:filePath]) {

[fileManager createFileAtPath:filePath contents:nil attributes:nil];

}

}

//读写本地历史记录  isgetData == yes 获取数据,  写入数据

-(void)getHistoryData:(BOOL)isGetData {

if (isGetData == NO) {

//        NSString *str = [NSString stringWithContentsOfFile:filePath usedEncoding:nil error:nil];

NSString *historyStr = [ historyArry componentsJoinedByString:@","];

[historyStr writeToFile:filePath atomically:YES

encoding:NSUTF8StringEncoding error:nil];

//通过将writeToFile:atomically:encoding:error:方法发送给字符串对象完成字符串存储到文件内的功能

}else{

NSString *str = [NSString stringWithContentsOfFile:filePath usedEncoding:nil error:nil];

//[historyArry removeAllObjects];

//[historyArry addObject:@""];

if(str.length!=0){

//[historyArry addObjectsFromArray:[ str componentsSeparatedByString:@","]];

}

NSLog(@"%@",str);

}

}

你可能感兴趣的:(文件写到本地)