In un applicativo iPhone può essere utile creare dei file, ad esempio di testo, ma non solo e magari poterli inviare via posta. Per questo scopo ci vengono in aiuto due classi di Cocoa (sia desktop che mobile) NSFileManager ed NSFileHandle.
Per usare filemanager basta scrivere:
NSFileManager *filemgr;
filemgr = [NSFileManager defaultManager];Da qui le possibilità sono varie:
Verificare l’esistenza di un file
NSFileManager *filemgr;
filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath: @"/tmp/myfile.txt" ] == YES)
 NSLog (@"File exists");
else
 NSLog (@"File not found");Comparare il contenuto di due file
NSFileManager *filemgr;
filemgr = [NSFileManager defaultManager];
if ([filemgr contentsEqualAtPath: @"/tmp/myfile.txt" andPath: @"/tmp/sales.txt"] == YES)
 NSLog (@"File contents match");
else
 NSLog (@"File contents do not match");Verificare i permessi di scrittura e lettura di un file
NSFileManager *filemgr;
filemgr = [NSFileManager defaultManager];
if ([filemgr isWritableFileAtPath: @"/tmp/myfile.txt"] == YES)
 NSLog (@"File is writable");
else
 NSLog (@"File is read only");Muovere/rinominare un file
NSFileManager *filemgr;
filemgr = [NSFileManager defaultManager];
if ([filemgr moveItemAtPath: @"/tmp/myfile.txt" toPath: @"/tmp/newfile.txt" error: NULL] == YES)
 NSLog (@"Move successful");
else
 NSLog (@"Move failed");Creare una copia di un file
if ([filemgr copyItemAtPath: @"/tmp/myfile.txt" toPath: @"/Users/demo/newfile.txt" error: NULL] == YES)
 NSLog (@"Copy successful");
else
 NSLog (@"Copy failed");Eliminare un file
NSFileManager *filemgr;
filemgr = [NSFileManager defaultManager];
if ([filemgr removeItemAtPath: @"/tmp/myfile.txt" error: NULL] == YES)
 NSLog (@"Remove successful");
else
 NSLog (@"Remove failed");Trovare l’offset
NSFileHandle serve per gestire il contenuto dei file.
file = [NSFileHandle fileHandleForUpdatingAtPath: @"/tmp/myfile.txt"];
if (file == nil)
 NSLog(@"Failed to open file");
NSLog (@"Offset = %llu", [file offsetInFile]);
[file seekToEndOfFile];
NSLog (@"Offset = %llu", [file offsetInFile]);
[file seekToFileOffset: 30];
NSLog (@"Offset = %llu", [file offsetInFile]);
[file closeFile];Leggere i dati da un file
NSData *databuffer;
file = [NSFileHandle fileHandleForReadingAtPath: @"/tmp/myfile.txt"];
if (file == nil)
 NSLog(@"Failed to open file");
[file seekToFileOffset: 10]; //10 byte alla volta
databuffer = [file readDataOfLength: 5];
[file closeFile];Scrivere dati in un file
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
 NSFileHandle *file;
 NSMutableData *data;
 const char *bytestring = "black dog";
 data = [NSMutableData dataWithBytes:bytestring length:strlen(bytestring)];
 file = [NSFileHandle fileHandleForUpdatingAtPath: @"/tmp/quickfox.txt"];
 if (file == nil)
 NSLog(@"Failed to open file");
 [file seekToFileOffset: 10];
 [file writeData: data];
 [file closeFile];
 [pool drain];
 return 0;
}Fonte: Techotopia
