Objective C Tutorial - Objective C File






Read File Content

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

        NSString *filePathName = @"/Users/Shared/textfile.txt";
        NSError *fileError;
        NSString *textFileContents = [NSString stringWithContentsOfFile:filePathName 
                                                                encoding:NSASCIIStringEncoding 
                                                                   error:&fileError];
        if(fileError.code == 0)
            NSLog(@"textfile.txt contents: %@", textFileContents);
        else
            NSLog(@"error(%ld): %@", fileError.code, fileError.description);

    
    return 0;
}





Write to a file

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]){

        NSString *filePathName = @"/Users/Shared/textfile.txt";
        NSError *fileError;
        NSString *textFileContents = @"Content generated from a Mac program.";
        
        [textFileContents writeToFile:filePathName
                           atomically:YES
                             encoding:NSStringEncodingConversionAllowLossy
                                error:&fileError];
        
        if(fileError.code == 0)
            NSLog(@"textfile.txt was written successfully with these contents: %@", textFileContents);
        else
            NSLog(@"error(%ld): %@", fileError.code, fileError.description);

        
    return 0;
}




Check File Status

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

        NSFileManager *fileManager = [NSFileManager defaultManager];
        
        NSString *filePathName = @"/Users/Shared/textfile.txt";
        
        BOOL fileExists = [fileManager fileExistsAtPath:filePathName];
        
        if(fileExists)
            NSLog(@"%@ exists", filePathName);
        else
            NSLog(@"%@ doesn't exist", filePathName);
        
        BOOL fileIsReadable = [fileManager isReadableFileAtPath:filePathName];
        
        if(fileIsReadable)
            NSLog(@"%@ is readable", filePathName);
        else
            NSLog(@"%@ isn't readable", filePathName);
        
        BOOL fileIsWriteable = [fileManager isWritableFileAtPath:filePathName];
        
        if(fileIsWriteable)
            NSLog(@"%@ is writable", filePathName);
        else
            NSLog(@"%@ isn't writable", filePathName);
        
        BOOL fileIsExecutable = [fileManager isExecutableFileAtPath:filePathName];
        
        if(fileIsExecutable)
            NSLog(@"%@ is an executable", filePathName);
        else
            NSLog(@"%@ isn't an executable", filePathName);
        
        BOOL fileIsDeleteable = [fileManager isDeletableFileAtPath:filePathName];
        
        if(fileIsDeleteable)
            NSLog(@"%@ is deletable", filePathName);
        else
            NSLog(@"%@ isn't an deletable", filePathName);
        
    return 0;
}

Changing File Attributes

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
    
        NSFileManager *fileManager = [NSFileManager defaultManager];
        
        NSString *filePathName = @"/Users/Shared/textfile.txt";
        
        NSError *error = nil;
        
        //Get the file attributes so you can compare later on:        
        NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:filePathName
                                                                     error:&error];
        
        if(!error)
            NSLog(@"%@ file attributes (before): %@",filePathName, fileAttributes);
        
        NSMutableDictionary *attributes = [[NSMutableDictionary alloc] init];
        
        [attributes setObject:[NSDate date] forKey:NSFileModificationDate];
        
        BOOL attributeChanged = [fileManager setAttributes:attributes
                                              ofItemAtPath:filePathName
                                                     error:&error];
        
        if(error)
            NSLog(@"There was an error: %@", error);
        else{
            NSLog(@"attributeChanged = %i", attributeChanged);
            
            //Get the file attributes to see the change:        
            NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:filePathName
                                                                         error:&error];
            
            if(!error)
                NSLog(@"%@ file attributes (after): %@",filePathName, fileAttributes);
        }
        
    return 0;
}

The code above generates the following result.

Save all kinds of Data into a File

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
    
        NSUInteger length = 3;
        
        char bytes1[length];
        bytes1[0] = 'A';
        bytes1[1] = 'B';
        bytes1[2] = 'C';
        
        for (int i=0;i<sizeof(bytes1);i++)
            NSLog(@"bytes1[%i] = %c", i, bytes1[i]);
        
        char bytes2[length];
        bytes2[0] = 'D';
        bytes2[1] = 'E';
        bytes2[2] = 'F';
        
        for (int i=0;i<sizeof(bytes2);i++)
            NSLog(@"bytes2[%i] = %c", i, bytes2[i]);
        
        NSMutableData *mutableData = [[NSMutableData alloc] init];
        
        [mutableData appendBytes:bytes1
                          length:length];
        
        [mutableData appendBytes:bytes2
                          length:length];
        
        NSLog(@"mutableData = %@", mutableData);
        
        char *bytesFromData = (char *)[mutableData bytes];
        
        for (int i=0;i<length*2;i++)
            NSLog(@"bytesFromData[%i] = %c", i, bytesFromData[i]);
        
        NSError *error = nil;
        
        BOOL dataSaved = [mutableData writeToFile:@"/Users/Shared/Book/datadump.txt"
                                          options:NSDataWritingAtomic
                                            error:&error];
        
        if(dataSaved)
            NSLog(@"mutableData successfully wrote contents to file system");
        else
            NSLog(@"mutableData was unsuccesful in writing out data because of %@", error);
        
    return 0;
}

The code above generates the following result.