2013-06-14 1 views
0

J'ai utilisé NSInputstream pour lire à partir d'un fichier. Après la lecture, le contenu nsInputStream sont empty.I utilisé le code (pour transférer un fichier txt à un serveur FTP)Lecture de fichier .txt dans NSInputstream retourne filestream n'a pas d'octets disponibles

- (void)startSend 

{ AppDelegate * mainDelegate = (AppDelegate *) [[UIApplication sharedApplication] délégué];

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]; 
} 

}

Il imprime, "FICHIER EXISTE" Mais dans la ligne suivante "Sommaire"

Le fichier n'est pas vide.

Répondre

2

J'ai juste eu le même problème, et la réponse est simple et stupide.

Après avoir créé votre flux:

self.fileStream = [NSInputStream inputStreamWithFileAtPath:filePath]; 

vous devez appeler ouvert sur le sujet:

[self.fileStream open]; 

Il est logique totale maintenant que je sais ce qui manque, mais je ne vois l'appel à ouvrir dans les exemples que j'ai trouvés d'utiliser NSInputStream.