NSFileHandle fileHandleForWritingAtPath: return null!
-
01-10-2019 - |
Frage
mein iPad App hat eine kleine Download-Möglichkeit, für die ich die Daten mit Hilfe eines NSFileHandle angehängt werden soll. Das Problem ist die Schaffung Aufruf gibt nur null Datei-Handles. Was könnte das Problem sein? Hier sind die drei Zeilen Code, die angeblich meine Datei-Handle erstellen:
NSString *applicationDocumentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
self.finalPath = [applicationDocumentsDirectory stringByAppendingPathComponent: self.fileName];
NSFileHandle *output = [NSFileHandle fileHandleForWritingAtPath:self.finalPath];
ich den Dateipfad überprüft, und ich kann nichts falsch sehen.
TYIA
Lösung
fileHandleForWritingAtPath
ist kein „Schöpfung“ nennen. Die Dokumentation heißt es ausdrücklich: „Rückgabewert: Die initialisierten Datei-Handle oder nil, wenn keine Datei im Pfad vorhanden ist “ (Hervorhebung hinzugefügt). Wenn Sie möchten, um die Datei zu erstellen, wenn es nicht vorhanden ist, dann würden Sie haben etwas zu verwenden:
NSFileHandle *output = [NSFileHandle fileHandleForWritingAtPath:self.finalPath];
if(output == nil) {
[[NSFileManager defaultManager] createFileAtPath:self.finalPath contents:nil attributes:nil];
output = [NSFileHandle fileHandleForWritingAtPath:self.finalPath];
}
Wenn Sie auf die Datei angehängt werden soll, wenn sie bereits vorhanden ist, verwenden Sie so etwas wie [output seekToEndOfFile]
. Ihr vollständiger Code würde dann wie folgt aussehen:
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];
}
Andere Tipps
Erhalten Sie Dokumente Verzeichnispfad
+(NSURL *)getDocumentsDirectoryPath
{
return [[[NSFileManager defaultManager]URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask]lastObject];
}
Speicher Text der Datei beenden
, wenn die Datei doesnt exist erstellen sie und Schreibdaten
+(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];
}
erhalten Text aus der Datei mit dem angegebenen Dateinamen
+(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 @"";
}
}
Datei löschen
+(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];
}
}