Friday, September 2, 2011

save and update data / Read and Write

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

-(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];
   
}

-(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.
       
        }
   
}



- (IBAction)save:(id)sender {
        //To Write the File.
       
    NSArray *array=[NSArray arrayWithObjects:usernameField.text,passwordField.text, nil];
   
    [self writeFile:@"arra.txt" dataArray:array];
   
   
        //Read the File
   
    NSString *result=[self readFile:@"arra.txt"];
   
    NSArray *outputArray=[result componentsSeparatedByString:@"\n"];
   
    for (int i=0; i<[outputArray count]; i++)
       
        {
       
        NSLog(@"the output=%@ index=%i",[outputArray objectAtIndex:i], i);
       
        }
    }

No comments:

FEEDJIT Live