2010-09-08 60 views
34

我的iPad應用程序有一個小的下載工具,爲此我想使用NSFileHandle追加數據。問題是創建調用只返回空文件句柄。可能是什麼問題呢?這裏是三行代碼,目的是爲了建立我的文件句柄:NSFileHandle fileHandleForWritingAtPath:return null!

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

我檢查的文件路徑,我認爲沒有錯。

TYIA

回答

83

fileHandleForWritingAtPath不是「創造」的呼叫。該文檔明確指出:「返回值:已初始化的文件句柄,或如果路徑中沒有文件存在,則爲零」(強調增加)。如果你希望創建的文件,如果它不存在,你不得不使用這樣的事情:

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

如果要追加到該文件,如果它已經存在,使用類似[output seekToEndOfFile]。你完整的代碼,然後將如下所示:

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

我的文件在「/無功/移動/集裝箱/數據/應用/ E914726C-34B7-4B92-存在A740-90E31131D75E/Library/Caches /「,但我仍然沒有。 – Nilesh 2017-02-15 06:07:52

0

獲取文件的目錄路徑

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

保存文本結束文件的

如果犯規存在的文件創建和寫入數據

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

得到文件文本與指定的文件名

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

刪除文件

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

}