0

我想读取并打印从保管箱下载的文件内容,但我的“readFile”方法打印为空。我确定文件已成功下载。

-(void)download
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@", @"File.txt"]];


    [[self restClient] loadFile:@"/File.txt" intoPath:filePath];

}
- (void)restClient:(DBRestClient*)client loadedFile:(NSString*)localPath
       contentType:(NSString*)contentType metadata:(DBMetadata*)metadata {

    [self readFile:@"File.txt"];
    NSLog(@"File loaded into path: %@", localPath);
}

-(void)readFile:(NSString *)fileName
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *fileNameData=[NSString stringWithFormat:@"%@",fileName];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileNameData];
    NSString *str = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];

    NSError *error;

    NSString *str = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];

    NSLog(@"%@", error);
}

这是输出:

---------******** CONTENT OF THE DOWNLOADED FILE *********------------(null)

我更新了捕获错误的代码,并且从 stringWithContentsOfFile 收到此错误:

Error Domain=NSCocoaErrorDomain Code=261 "The operation couldn’t be completed. (Cocoa error 261.)" UserInfo=0x1669c2b0 {NSFilePath=/var/mobile/Applications/11B10727-E372-1147-26BD-1D24S60B8E54/Docume‌​nts/File.txt, NSStringEncoding=4} 2013-08-05 22:06:03.229 DBApp[496:60b]
4

1 回答 1

0

看起来您的代码正在读取一个名为“File.txt”的文件,而不是从 Dropbox 下载的实际文件。我错过了什么吗?

编辑

根据下面的评论,错误似乎是 261,与字符串编码有关。您可能想尝试不同的编码或确保文本文件按照您期望的方式进行编码。

于 2013-08-05T18:49:13.980 回答