SHARE
TWEET

Untitled

a guest Sep 18th, 2019 94 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. +(void)writefile:(NSString *)string fileName:(NSString *)fileName{
  2.    
  3.     NSLog(@"fileName==%@",fileName);
  4.     NSArray *paths  = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
  5.     NSString *homePath = [paths objectAtIndex:0];
  6.     NSString *filePath = [homePath stringByAppendingPathComponent:fileName];
  7.    
  8.     NSFileManager *fileManager = [NSFileManager defaultManager];
  9.     //如果不存在
  10.     if(![fileManager fileExistsAtPath:filePath]){
  11.         NSLog(@"-------文件不存在,写入文件----------");
  12.         NSError *error;
  13.         if([string writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error]){
  14.             NSLog(@"------写入文件------success");
  15.         }else{
  16.             NSLog(@"------写入文件------fail,error==%@",error);
  17.         }
  18.     }else{//追加写入文件,而不是覆盖原来的文件
  19.         NSLog(@"-------文件存在,追加文件----------");
  20.         NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:filePath];
  21.         [fileHandle seekToEndOfFile];  //将节点跳到文件的末尾
  22.         NSData* stringData  = [string dataUsingEncoding:NSUTF8StringEncoding];
  23.         [fileHandle writeData:stringData]; //追加写入数据
  24.         [fileHandle closeFile];
  25.     }
  26. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top