2013-06-14 6 views
0

Я использовал NSInputstream для чтения из файла. После прочтения, содержание NSInputstream являются empty.I используется код (Для передачи файла .txt на FTP-сервер)Чтение .txt файла в NSInputstream возвращает filestream не имеет байтов

- (void)startSend 

{ AppDelegate * mainDelegate = (AppDelegate *) [[UIApplication sharedApplication] делегат];

BOOL     success; 
NSURL *     url; 

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 

NSString *testsessionid=[defaults stringForKey:@"testsessionid"]; 
NSString *writeFileName=[NSString stringWithFormat:@"%@%@.txt",testsessionid,mainDelegate.studentID]; 
NSLog(@"Write file name %@",writeFileName); 
NSArray *searchPaths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentFolderPath = [searchPaths objectAtIndex: 0]; 

NSString *filePath= [documentFolderPath stringByAppendingPathComponent: writeFileName]; 
NSLog(@"Write folder name %@",filePath); 


[email protected]"/Users/sree/Desktop/ARATHY/BCLSTestApp/BCLSTest/Question2.txt"; 

assert(filePath != nil); 
assert([[NSFileManager defaultManager] fileExistsAtPath:filePath]); 
assert([filePath.pathExtension isEqual:@"txt"] ); 

assert(self.networkStream == nil);  // don't tap send twice in a row! 
assert(self.fileStream == nil); 
// First get and check the URL. 

url = [NSURL URLWithString:@"ftp://[email protected]/bclstest/243"]; 
success = (url != nil); 

if (success) { 
    // Add the last part of the file name to the end of the URL to form the final 
    // URL that we're going to put to. 

    url = CFBridgingRelease(
          CFURLCreateCopyAppendingPathComponent(NULL, (CFURLRef) url, (CFStringRef) [filePath lastPathComponent], false) 
          ); 
    success = (url != nil); 
} 

// If the URL is bogus, let the user know. Otherwise kick off the connection 
if (! success) { 
    NSLog(@"Invalid URL"); 
} else { 

    // Open a stream for the file we're going to send. We do not open this stream; 
    // NSURLConnection will do it for us. 

    self.fileStream = [NSInputStream inputStreamWithFileAtPath:filePath]; 

// self.fileStream=[[NSInputStream alloc] initWithFileAtPath:filePath]; 
    if(self.fileStream==nil) 

     NSLog(@"FILE DOESN'T EXIST"); 

    else 
     NSLog(@"FILE EXISTS"); 

    assert(self.fileStream != nil); 
    BOOL hasbyte=[self.fileStream hasBytesAvailable]; 

    if (hasbyte==YES) 

     NSLog(@"Has contents"); 

    else 
     NSLog(@"no contents"); 

    [self.fileStream open]; 
    // NSLog(@"SIZE OF STREAM IS >> %d",fi); 
    // Open a CFFTPStream for the URL. 

    self.networkStream = CFBridgingRelease(
              CFWriteStreamCreateWithFTPURL(NULL, (CFURLRef) url) 
              ); 
    assert(self.networkStream != nil); 

     success = [self.networkStream setProperty:@"edugame" forKey:(id)kCFStreamPropertyFTPUserName]; 
     assert(success); 
     success = [self.networkStream setProperty:@"[email protected]" forKey:(id)kCFStreamPropertyFTPPassword]; 
     assert(success); 


    self.networkStream.delegate = self; 
    [self.networkStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; 
    [self.networkStream open]; 

    // Tell the UI we're sending. 

    //[self sendDidStart]; 
} 

}

Он печатает, "FILE НЕ СУЩЕСТВУЕТ" Но в следующей строке "нет" содержания

Файл не является пустым.

ответ

2

У меня была такая же проблема, и ответ прост и глуп.

После создания потока:

self.fileStream = [NSInputStream inputStreamWithFileAtPath:filePath]; 

Вам нужно позвонить открыть на нем:

[self.fileStream open]; 

Это делает общий смысл теперь, когда я знаю, чего не хватает, но я не видел вызов для открытия в примерах, которые я нашел из использования NSInputStream.