Follow by Email

Search This Blog

Sunday, 27 November 2011

Read/Write text to File in iPhone/iPad app

Here we are sharing how to Create a file and write in it.
 Just copy and paste into .m file in your project

//This is our Writing part:

NSArray *filepath =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
   
    NSString *documentsdirectory=[filepath objectAtIndex:0];
    NSLog(@"Document directory>>>%@",documentsdirectory);
    NSString *filename=[NSString stringWithFormat:@"%@/myfile.txt",documentsdirectory];
    NSString *content=@"One\nTwo\nThree\nFour\nFive";
    [content writeToFile:filename atomically:YES encoding:NSStringEncodingConversionAllowLossy error:nil];

//Writting part end.

//This our Reading part
NSArray *path= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentdir=[path objectAtIndex:0];
    NSString *filename=[NSString stringWithFormat:@"%@/myfile.txt",documentdir];
    NSString *cont=[[NSString alloc]initWithContentsOfFile:filename usedEncoding:nil error:nil];
    NSLog(@"content>>%@",cont);

Reading part end.

5 comments:

  1. Iphone application development to extend the memory configuration for storing files in it.

    iphone app

    ReplyDelete
  2. Appreciated..!! Thank you for this code.

    iPhone App Developers

    Cheers

    ReplyDelete
  3. I have read this blog that provide good information regarding how to Read/Write text to File in iPhone or iPad app with the help of code and which is useful for me or other people.Thanks. .

    ReplyDelete
  4. Indeed! This method is really simple and it will be good for those who are new to iPhone App Development like me.Thanks

    ReplyDelete
  5. Thanks for complements all of you, if you need more so you can suggest me whatever i can share with you it will be my pleasure.

    ReplyDelete