2010-09-08 16 views
34

l'app per iPad ha una piccola funzione di download, per cui desidero aggiungere i dati utilizzando un NSFileHandle. Il problema è che la chiamata alla creazione restituisce solo handle di file nulli. Quale potrebbe essere il problema? Ecco le tre righe di codice che dovrebbero creare il mio gestore di file:NSFileHandle fileHandleForWritingAtPath: return null!

NSString *applicationDocumentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; 
self.finalPath = [applicationDocumentsDirectory stringByAppendingPathComponent: self.fileName]; 
NSFileHandle *output = [NSFileHandle fileHandleForWritingAtPath:self.finalPath]; 

ho controllato il percorso del file, e ho potuto vedere nulla di male.

TYIA

risposta

83

fileHandleForWritingAtPath non è una chiamata “creazione”. La documentazione afferma esplicitamente: "Valore restituito: l'handle di file inizializzato o nil se non esiste alcun file sul percorso" (corsivo aggiunto). Se si desidera creare il file se non esiste, che avrebbe dovuto usare qualcosa di simile:

NSFileHandle *output = [NSFileHandle fileHandleForWritingAtPath:self.finalPath]; 
if(output == nil) { 
     [[NSFileManager defaultManager] createFileAtPath:self.finalPath contents:nil attributes:nil]; 
     output = [NSFileHandle fileHandleForWritingAtPath:self.finalPath]; 
} 

Se si desidera aggiungere al file se già esistente, usa qualcosa come [output seekToEndOfFile]. Il tuo codice completo sarebbe poi apparire come segue:

NSString *applicationDocumentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; 
self.finalPath = [applicationDocumentsDirectory stringByAppendingPathComponent: self.fileName]; 
NSFileHandle *output = [NSFileHandle fileHandleForWritingAtPath:self.finalPath]; 
if(output == nil) { 
     [[NSFileManager defaultManager] createFileAtPath:self.finalPath contents:nil attributes:nil]; 
     output = [NSFileHandle fileHandleForWritingAtPath:self.finalPath]; 
} else { 
     [output seekToEndOfFile]; 
} 
+0

Il mio file esistere "/ var/mobile/Contenitori/dati/applicazioni/E914726C-34B7-4B92- A740-90E31131D75E/Library/Caches/"ma sto ancora ottenendo zero. – Nilesh

0

Get documenti percorso della directory

+(NSURL *)getDocumentsDirectoryPath 
{ 
    return [[[NSFileManager defaultManager]URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask]lastObject]; 
} 

Salva testo alla fine del file

se il file doesnt esiste crearlo e scrivere dati

+(void)saveText:(NSString *)textTobeSaved atPath:(NSString*)fileName 
{ 
    NSString *filePath = [NSString stringWithFormat:@"%@.text",fileName]; 

    NSString *path = [[self getDocumentsDirectoryPath].path 
         stringByAppendingPathComponent:filePath]; 
    NSFileHandle *fileHandler = [NSFileHandle fileHandleForWritingAtPath:path]; 
    if(fileHandler == nil) { 
     [[NSFileManager defaultManager] createFileAtPath:path contents:nil attributes:nil]; 
     fileHandler = [NSFileHandle fileHandleForWritingAtPath:path]; 
    } else { 
     textTobeSaved = [NSString stringWithFormat:@"\n-----------------------\n %@",textTobeSaved]; 
     [fileHandler seekToEndOfFile]; 
    } 

    [fileHandler writeData:[textTobeSaved dataUsingEncoding:NSUTF8StringEncoding]]; 
    [fileHandler closeFile]; 
} 

ottenere il testo da file con il nome del file specificato

+(NSString *)getTextFromFilePath:(NSString *)fileName 
{ 
    NSString *filePath = [NSString stringWithFormat:@"%@.text",fileName]; 

    NSString *path = [[self getDocumentsDirectoryPath].path 
         stringByAppendingPathComponent:filePath]; 
    NSLog(@"%@",path); 
    if(path!=nil) 
    { 
    NSString *savedString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; 

    return savedString; 
    }else{ 
    return @""; 
    } 
} 

Elimina file

+(void)deleteFile:(NSString *)fileName 
{ 
    NSString *filePath = [NSString stringWithFormat:@"%@.text",fileName]; 

    NSString *path = [[self getDocumentsDirectoryPath].path 
         stringByAppendingPathComponent:filePath]; 

    NSFileHandle *fileHandler = [NSFileHandle fileHandleForWritingAtPath:path]; 
    if(fileHandler != nil) { 
     [[NSFileManager defaultManager]removeItemAtPath:path error:nil]; 
    } 

}