Monday, May 16, 2011

Read/Write txt File in iPhone SDK

To Write the file in iPhone SDK use Following Method:

-(void)writeFile:(NSString *)fileName data:(id)data

{

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

NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];

NSError *error=NULL;


[data writeToFile:appFile atomically:YES encoding:NSUTF8StringEncoding error:&error];


if (error != NULL)

{

//Check Error Here. if any.

}

}


To Read the file in iPhone SDK use Following Method:


-(NSString *)readFile:(NSString *)fileName

{

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

NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];

NSFileManager *fileManager=[NSFileManager defaultManager];

if ([fileManager fileExistsAtPath:appFile])

{

NSError *error= NULL;

id resultData=[NSString stringWithContentsOfFile:appFile encoding:NSUTF8StringEncoding error:&error];

if (error == NULL)

{

return resultData;

}

}

return NULL;

}


Example to Write the Array in txt file:


-(void)writeFile:(NSString *)fileName dataArray:(NSArray *)data

{

NSMutableString *dataString=[NSMutableString stringWithString:@""];

for (int i=0; i<[data count]; i++)

{

if (i == [data count]-1)

{

[(NSMutableString *)dataString appendFormat:@"%@",[data objectAtIndex:i]];

}

else

{

[(NSMutableString *)dataString appendFormat:@"%@\n",[data objectAtIndex:i]];

}

}

[self writeFile:fileName data:dataString];

}

//To Write the File.

NSString *obj1=[NSString stringWithFormat:@"APPLE"];

NSString *obj2=[NSString stringWithFormat:@"GOOGLE"];

NSString *obj3=[NSString stringWithFormat:@"HP"];

NSArray *array=[NSArray arrayWithObjects:obj1, obj2, obj3, nil];

[self writeFile:@"arrayTest.txt" dataArray:array];


//Read the File

NSString *result=[self readFile:@"arrayTest.txt"];

NSArray *outputArray=[result componentsSeparatedByString:@"\n"];

for (int i=0; i<[outputArray count]; i++)

{

NSLog(@"the output=%@ index=%i",[outputArray objectAtIndex:i], i);

}


Update if already file exist:


-(void)writeFile:(NSString *)fileName data:(id)data

{

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

NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];

NSError *error=NULL;

NSFileManager *fileManager=[NSFileManager defaultManager];

if ([fileManager fileExistsAtPath:appFile])

{

NSString *fileString=[NSString stringWithContentsOfFile:appFile encoding:NSUTF8StringEncoding error:&error];

data=[data stringByAppendingFormat:@"\n%@",fileString];

[data writeToFile:appFile atomically:YES encoding:NSUTF8StringEncoding error:&error];

}

else

{

[data writeToFile:appFile atomically:YES encoding:NSUTF8StringEncoding error:&error];

}

if (error != NULL)

{

//Check Error Here. if any.

}

}


Thats it!!!!

6 comments:

  1. it's very nice post for read and write data in a file... i used all this method for saving my application

    ReplyDelete
  2. thanks for your post. i want one more method for... if file have already data mean, new data will add after previous data...

    ReplyDelete
  3. @shun Thanks for your valuable comment i have updated according to your requirment.

    ReplyDelete
  4. it's work perfectly... thanks for quickly replay.

    ReplyDelete
  5. Hi, that post was great! Is there anyway for me to check what´s into my file... so i could check part of the new string and just update one line if it i consider the new line a "repeated line"? And if not then i do what you do (add the new line to the end of the file).

    So let´s say i have in my file,
    1 - PETER
    2 - STEVEN
    3 - HARRY

    if i add a "4-JOHN":
    1-PETER
    2-STEVEN
    3-HARRY
    4-JOHN

    but if i add a "3" index again like "3-JACK":
    1-PETER
    2-STEVEN
    3-JACK
    4-JOHN

    Do you have any ideas how to do that? Thanks

    ReplyDelete
  6. It’s never too late to develop your data and your contents really excite me a lot.

    iPhone developer in Pakistan

    ReplyDelete