2010-09-08 7 views
34

mon application iPad a un petit service de téléchargement, pour lequel je veux ajouter les données en utilisant un NSFileHandle. Le problème est que l'appel de création ne renvoie que les handles de fichiers null. Quel pourrait être le problème? Voici les trois lignes de code qui sont censés créer mon fichier poignée:NSFileHandle fileHandleForWritingAtPath: retourne null!

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

J'ai vérifié le chemin du fichier, et je ne voyais rien de mal.

TYIA

Répondre

83

fileHandleForWritingAtPath est pas un appel « de création ». La documentation indique explicitement: "Valeur de retour: le handle de fichier initialisé, ou nul si aucun fichier n'existe au chemin" (soulignement ajouté). Si vous souhaitez créer le fichier si elle n'existe pas, vous auriez à utiliser quelque chose comme ceci:

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

Si vous voulez ajouter au fichier si elle existe déjà, utilisez quelque chose comme [output seekToEndOfFile]. Votre code complet serait alors se présenter comme suit:

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

Mon fichier existe à «/var/mobile/Conteneurs/Données/Application/E914726C-34B7-4B92- A740-90E31131D75E/Bibliothèque/Caches/"mais je reçois toujours rien. – Nilesh

0

Obtenir le chemin du répertoire documents

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

Enregistrer le texte à la fin du fichier

si le fichier ne marche pas existe créer et écrire données

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

obtenir le texte du fichier avec nom de fichier spécifié

+(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 @""; 
    } 
} 

Supprimer le fichier

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

} 
Questions connexes