2015-05-09 69 views
0

我正在開發一個iOS應用程序。我從URL下載文本文件並將其保存到一個目錄中,但我無法在目錄中找到該文件,因此代碼再次下載它。如何檢查文件?檢查目錄中的文本文件是否顯示直接

if ([[NSFileManager defaultManager] fileExistsAtPath:localfile]) { 
     content = [NSString stringWithContentsOfFile:localfile 
              encoding:NSUTF8StringEncoding 
               error:NULL]; 
    }else 
    { 
     NSURL* url = [NSURL URLWithString:@"http://webapp.opaxweb.net/books/gurugranthsahib.txt"]; 

    NSArray* pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                  NSUserDomainMask, YES); 
    NSString* documentsDir = [pathArray objectAtIndex:0]; 
    localfile =[documentsDir stringByAppendingPathComponent:@"data"]; 
     NSData* data = [NSData dataWithContentsOfURL:url]; 
    [data writeToFile:localfile atomically:YES]; 
    content = [NSString stringWithContentsOfFile:localfile 
             encoding:NSUTF8StringEncoding 
             error:NULL]; 
} 
_textfield.text=content; 
+0

您的代碼是正確的,但可能您沒有正確保留本地文件字符串。在檢查fileExistsAtPath方法之前,先檢查localfile字符串,並檢查實際的Documents目錄中是否存在文件。 –

+0

請停止使用雙點,並明確您的問題。我編輯了無用的談話。 – Schemetrical

回答

1

您的文本數據非常大,因此您應該使用nsurl連接!你的代碼是完全正確的,但這是一個更好的方法。

- (void)viewDidLoad 
    { 
     [super viewDidLoad]; 
     self.fileName = @"Data"; 
     self.fileData = [NSMutableData data]; 
     [self checkFileExitsorNOt]; 
    } 

    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
    { 
     [self.fileData setLength:0]; 
     self.totalFileSize = [NSNumber numberWithLongLong:[response expectedContentLength]]; 
    } 

    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
    { 
      [self.fileData appendData:data];   
    } 

    - (void)connectionDidFinishLoading:(NSURLConnection *)connection 
    { 
     NSArray *dirArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSLog(@"%@", [dirArray objectAtIndex:0]); 

     NSString *path = [NSString stringWithFormat:@"%@/%@", [dirArray objectAtIndex:0],self.fileName]; 

     if ([self.fileData writeToFile:path options:NSAtomicWrite error:nil] == NO) { 
      NSLog(@"writeToFile error"); 
     } 
     else { 
      NSLog(@"Written!"); 
     } 
    } 

-(void) checkFileExitsorNOt 
{ 
    NSArray *dirArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSLog(@"%@", [dirArray objectAtIndex:0]); 

    NSString *path = [NSString stringWithFormat:@"%@/%@", [dirArray objectAtIndex:0],self.fileName]; 

    if ([[NSFileManager defaultManager] fileExistsAtPath:path]) { 
     NSString *cont = [NSString stringWithContentsOfFile:localfile 
                encoding:NSUTF8StringEncoding 
                 error:NULL]; 
     NSLog(@"Content from file : %@",cont); 
    } 
    else 
    { 
     NSString *file = [NSString stringWithFormat:@"http://webapp.opaxweb.net/books/gurugranthsahib.txt"]; 
     NSURL *fileURL = [NSURL URLWithString:file]; 

     NSURLRequest *req = [NSURLRequest requestWithURL:fileURL]; 
     NSURLConnection *conn = [NSURLConnection connectionWithRequest:req delegate:self]; 
    } 

} 
+0

聽起來有點貴,但它可能是官方支持的從文件流式傳輸數據的方式(請說明)。更便宜的選擇是使用舊學校的C文件API。 – trojanfoe

+0

請澄清你的意思,「更便宜的選擇是使用舊學校的C文件API。」 –

+0

使用'fopen()','fgets()'等 – trojanfoe

相關問題