我想阅读并打印从dropbox下载的文件内容,但我的“readFile”方法打印为null。我确信文件已成功下载。
-(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)
我更新了捕获erorr的代码,我从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/Documents/File.txt, NSStringEncoding=4} 2013-08-05 22:06:03.229 DBApp[496:60b]
答案 0 :(得分:0)
看起来您的代码正在读取名为“File.txt”的文件,而不是从Dropbox下载的实际文件。我错过了什么吗?
修改强>
根据以下评论,看起来错误是261,与字符串编码有关。您可能希望尝试不同的编码或确保文本文件按照您期望的方式进行编码。