Quantcast
Channel: Developer Feed - Snippet
Viewing all articles
Browse latest Browse all 178

How to read and write file from NSString and viceversa in iOS?

$
0
0

Snippet

  1. // Write to Text File as String
  2. +(void) writeFile :(NSString*) data asFileName:(NSString*) filename inDirectory:(NSString*) directory onError:(NSError*) error{
  3.    
  4.     // create the full file name path
  5.     NSString *fileNameWithPath =[NSString stringWithFormat:@"%@/%@",  directory,filename];
  6.    
  7.     debugLog(@"Write to file %@",fileNameWithPath);
  8.  
  9.     // write the content
  10.     [data writeToFile:fileNameWithPath              atomically:NO                 encoding:NSUTF8StringEncoding                   error:&error];
  11.    
  12. }
  13.  
  14.  
  15.  
  16. // Read contents of a file
  17. +(NSString *) readFile :(NSString*) filename inDirectory:(NSString*) directory onError:(NSError*) error{
  18.     //get the documents directory:
  19.    
  20.     // create the full file name path
  21.     NSString *fileNameWithPath =[NSString stringWithFormat:@"%@/%@",  directory,filename];
  22.  
  23.     debugLog(@"Reading  file %@",fileNameWithPath);
  24.    
  25.     return [[NSString alloc] initWithContentsOfFile:fileNameWithPath    usedEncoding:NSUTF8StringEncoding       error:&error];
  26.    
  27. }

<!--break-->


Viewing all articles
Browse latest Browse all 178

Trending Articles