Чтение файла .txt в NSInputstream возвращает файловый поток, в котором нет доступных байтов

Я использовал NSInputstream для чтения из файла. После прочтения содержимое NSInputstream пусто. Я использовал код (для передачи файла .txt на ftp-сервер)

- (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);


filePath=@"/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:@"edu1@Game" 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];
}

}

Он печатает: «ФАЙЛ СУЩЕСТВУЕТ», но в следующей строке «нет содержимого».

Файл не пустой.


person ARATHY    schedule 14.06.2013    source источник


Ответы (1)


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

После того, как вы создадите свой поток:

self.fileStream = [NSInputStream inputStreamWithFileAtPath:filePath];

вам нужно позвонить по нему:

[self.fileStream open];

Теперь это имеет смысл, когда я знаю, чего не хватает, но я не видел вызова open в найденных мной примерах использования NSInputStream.

person Kelly    schedule 09.10.2013