文件写到本地

-(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);

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 27、ViewController的didReceiveMemoryWarning是在什么时候调用的?默认的操作是...
    烟雨平生花飞舞阅读 647评论 0 1
  • 一、iOS中的沙盒机制 iOS应用程序只能对自己创建的文件系统读取文件,这个独立、封闭、安全的空间,叫做沙盒。它一...
    1d5cb7cff98d阅读 1,801评论 0 0
  • 一、iOS中的沙盒机制 iOS应用程序只能对自己创建的文件系统读取文件,这个独立、封闭、安全的空间,叫做沙盒。它一...
    tzhtodd阅读 1,309评论 0 2
  • 218.241.181.202 wxhl60 123456 192.168.10.253 wxhl66 wxhl6...
    CYC666阅读 1,489评论 0 6
  • iOS开发-文件管理(一) 一、iOS中的沙盒机制 iOS应用程序只能对自己创建的文件系统读取文件,这个独立、封闭...
    MacShare阅读 1,821评论 0 6