2013-10-11 135 views
0

我想在我的應用程序文檔文件夾中創建一個文件,並且我必須存儲一些內容。該內容來自我的本地主機文件。所以我寫了一個下面的代碼。但是這段代碼並未在我的應用程序文檔文件夾中創建一個文件。創建文件到應用程序文檔文件夾

- (void) createFile 
    { 
     NSString *strPath = [NSString stringWithFormat:@"http://192.168.5.117/~mac/banana.obj"]; 

     NSData *data =[NSData dataWithContentsOfURL:[NSURL URLWithString:strPath]]; 

     NSString *strLastpath = [strPath lastPathComponent]; 

     NSString *folderName = [@"Object File/" stringByAppendingString:strLastpath]; 

     NSLog(@"Path : %@ \n File Name : %@",strPath,folderName); 

     NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

     NSString *docsDir = [dirPaths objectAtIndex:0]; 

     databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent:folderName]]; 

     NSLog(@"Database Path : %@",databasePath); 

     NSFileManager *filemgr = [NSFileManager defaultManager]; 

     if ([filemgr fileExistsAtPath:databasePath] == YES) 
     { 
      NSLog(@"File Exists"); 
     } 
     else 
     { 
      NSLog(@"File not exists"); 
     } 

     NSString *content = [NSString stringWithFormat:@"%@",data]; 

     [content writeToFile:strLastpath atomically:NO encoding:NSStringEncodingConversionAllowLossy error:nil]; 

    } 

雖然build for running我得到了這個。

2013-10-11 11:36:38.833 SampleFileCreation[1321:c07] Path : http://192.168.5.117/~mac/banana.obj 

File Name : Object File/banana.obj 

2013-10-11 11:36:38.834 SampleFileCreation[1321:c07] Database Path : /Users/mac/Library/Application Support/iPhone Simulator/6.1/Applications/4FA749DF-D12D-4956-AF76-140D2F981F17/Documents/Object File/banana.obj 

2013-10-11 11:36:38.834 SampleFileCreation[1321:c07] File not exists 
+0

確定的NSData *數據= [NSData的dataWithContentsOfURL:[NSURL URLWithString:strPath的];這是給你下載的數據? – Tirth

+0

是的,我得到了所有的內容,我使用NSLog檢查它。 – Sabs

+0

你要下載哪種文件類型? – Tirth

回答

0

我找到了答案。文件已成功創建。

- (void) createFile { 

    NSString *strPath = [NSString stringWithFormat:@"http://-.-.-.-/~mac/banana.obj"];   
    NSData *data =[NSData dataWithContentsOfURL:[NSURL URLWithString:strPath]];    
    NSString *strLastpath = [strPath lastPathComponent];   
    NSString *folderName = [@"Object File/" stringByAppendingString:strLastpath];   
    NSLog(@"Path : %@ \n File Name : %@",strPath,folderName);   
    NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                 NSUserDomainMask, YES); 

    NSString *docsDir = [dirPaths objectAtIndex:0];   
    docsDir=[docsDir stringByAppendingString:@"/Object File"];   
    databasePath = [[NSString alloc] initWithString: 
        [docsDir stringByAppendingPathComponent:folderName]]; 

    NSLog(@"Database Path : %@",databasePath);   
    NSError *theError = nil;  
    NSFileManager *filemgr = [NSFileManager defaultManager]; 

    **//I added this line** 
    [filemgr createDirectoryAtPath:docsDir 
     withIntermediateDirectories:YES 
         attributes:nil 
          error:&theError]; 

    **//Changed this line** 
    [data writeToFile:[docsDir stringByAppendingPathComponent:strLastpath]    
       options:NSDataWritingAtomic 
       error:&theError]; 

    if ([filemgr fileExistsAtPath:[docsDir stringByAppendingPathComponent:strLastpath]]){ 
     NSLog(@"File Exists"); 
    } else { 
     NSLog(@"File not exists"); 
     NSLog(@"Tell Me error %@",[theError localizedDescription]); 
    }  
} 
1

你也可以試試這個代碼

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder 
NSString *fileName = @"yourFileName"; 
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/fileName"]; 

NSDate *data = [NSData dataWithContentsOfURL:YourUrl]; // get date form url 
[data writeToFile:dataPath atomically:YES]; // save data in file 
相關問題